Advertisement
Guest User

Untitled

a guest
Dec 14th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from flask import Flask, render_template, request, redirect, session, flash
  3. from uuid import uuid4
  4. import random
  5. import string
  6.  
  7. wszystkie = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm123456789'
  8.  
  9. def losuj():
  10. wynik = []
  11. i=0
  12. l = len(wszystkie)
  13. a=random.randint(1, 5)
  14. while(i<=a):
  15. wynik.append(wszystkie[random.randint(0, l - 1)])
  16. i = i+1
  17. return wynik
  18.  
  19. app_url = '/molasym/session'
  20. app = Flask(__name__)
  21. app.secret_key = 'a1s8dAS@#f=+D23d%^$$#*(T'
  22.  
  23. tab = {}
  24. tab2 = {}
  25.  
  26. #from werkzeug.debug import DebuggedApplication
  27. #app.debug = True
  28. #app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
  29.  
  30. @app.route(app_url + '/logout')
  31. def logout():
  32. session.pop('username', None)
  33. return redirect(app_url + '/login')
  34.  
  35. @app.route(app_url + '/')
  36. def index():
  37. if 'username' not in session:
  38. return redirect(app_url + '/login')
  39. username = session['username']
  40. return render_template('login_success.html')
  41.  
  42. @app.route(app_url + '/shorten', methods=['GET', 'POST'])
  43. def shorten():
  44. if request.method == 'POST':
  45. url = request.form.get('link')
  46. if 'username' not in session:
  47. kod = ''.join(losuj())
  48. tab2[url]=kod
  49. return render_template('login_form.html', link = kod, old = url)
  50. if 'username' in session:
  51. tab[url]=''.join(losuj())
  52. return render_template('login_success.html', link = tab)
  53. if request.method == 'GET':
  54. return 'Zła metoda - GET'
  55.  
  56.  
  57. @app.route(app_url + '/login', methods=['GET','POST'])
  58. def login():
  59. if request.method == 'GET':
  60. return render_template('login_form.html')
  61. if request.method == 'POST':
  62. username = request.form.get('username')
  63. password = request.form.get('password')
  64. if username == 'moli' and password == 'molasy':
  65. session['uid'] = uuid4()
  66. session['username'] = username
  67. return render_template('login_success.html', link = tab )
  68. return render_template('login_failure.html', username=username)
  69.  
  70. @app.route(app_url + '/error')
  71. def ret_err():
  72. return render_template('brak_linka.html')
  73.  
  74. @app.route(app_url + '/<url_shortened>')
  75. def url_shortened_redirect(url_shortened):
  76. print url_shortened
  77. if 'username' in session:
  78. for x, y in tab.iteritems():
  79. if y == url_shortened:
  80. return redirect(x, code = 301)
  81. return redirect("http://edi.zetis.pw/molasym/session/error", code=404)
  82. if 'username' not in session:
  83. for x, y in tab2.iteritems():
  84. if y == url_shortened:
  85. return redirect(x, code = 301)
  86. return redirect("http://edi.zetis.pw/molasym/session/error", code = 404)
  87.  
  88. if __name__ == '__main__':
  89. app.run(host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement