Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from circuits import handler, Component , Debugger
  3. from circuits.web import Server, Controller, Sessions
  4. import MySQLdb
  5. from mako.template import Template
  6. home = Template(filename='home.html')
  7. user = Template(filename='user.html')
  8. db = MySQLdb.connect(host="localhost", user="root", passwd="!1#32@",db="twatter")
  9. cursor = db.cursor()
  10. def login(username,password):
  11.     cursor.execute("SELECT id, password FROM login WHERE username=%s", (username))
  12.     result = cursor.fetchall()
  13.     if result:
  14.         if result[0][1] == password:
  15.             return result[0][0]
  16.            
  17. showfollows = """SELECT updates.datetime, updates.message, login.username
  18. FROM following
  19. INNER JOIN updates ON following.followid = updates.ID
  20. INNER JOIN login ON following.followid = login.ID
  21. WHERE following.ID =%s
  22. ORDER BY updates.datetime DESC
  23. LIMIT 5"""
  24.  
  25. nametoid = """SELECT ID
  26. FROM login
  27. WHERE username=%s"""
  28.  
  29. showsuser = """SELECT updates.datetime, updates.message, login.username
  30. FROM updates
  31. INNER JOIN login ON updates.ID = login.ID
  32. WHERE login.username =%s
  33. ORDER BY updates.datetime DESC
  34. LIMIT 5"""
  35.  
  36. isfollow = """SELECT following.ID, following.followid , login.username
  37. FROM following
  38. INNER JOIN login on following.ID = login.ID
  39. WHERE following.ID =%s AND following.followid = login.ID"""
  40.  
  41. sqlupdate="INSERT INTO updates (ID, message, datetime) VALUES (%s, %s, CURRENT_TIMESTAMP);"
  42.  
  43. sqlfollowcheck="""SELECT *
  44. FROM following
  45. WHERE following.ID=%s AND following.followid=%s"""
  46.  
  47. sqlfollow="""INSERT INTO  following (ID, followid)
  48. VALUES (%s, %s)"""
  49.  
  50. sqlfollowdel = """DELETE FROM following
  51. WHERE ID=%s AND followid=%s"""
  52. class Root(Controller):
  53.     def index(self, **kwargs):
  54.         if "id" in self.session:
  55.             cursor.execute(showfollows, (self.session["id"]))
  56.             updates = cursor.fetchall()
  57.             return home.render(updates=updates ,name=self.session["name"])
  58.         else:
  59.             return home.render(logged=False)
  60.     def login(self,**kwargs):
  61.         if "name" in self.session:
  62.             return home.render(name=self.session["name"])
  63.         elif kwargs.get("username", None):
  64.             username = kwargs.get("username", None)
  65.             password = kwargs.get("password", None)
  66.             if login(username,password):
  67.                 self.session["name"] = username
  68.                 self.session["id"] =login(username,password)
  69.                 return Template("go home <a href='./index'>a</a>").render()
  70.             else:
  71.                 return home.render(name=None,message="Wrong password")
  72.  
  73.         else:
  74.             return home.render(name=None)
  75.     def user(self , username):
  76.         cursor.execute(showsuser, (username))
  77.         updates = cursor.fetchall()
  78.         if "id" in self.session:
  79.             cursor.execute(nametoid,(username))
  80.             userid= cursor.fetchall()[0][0]
  81.             cursor.execute(sqlfollowcheck, (self.session["id"],userid))
  82.             following = cursor.fetchall()
  83.             return user.render(logged=True,name=username,updates=updates,following=following)
  84.         else:
  85.             return user.render(name=username,updates=updates)
  86.    
  87.     def update(self , update=None):
  88.         if "id" in self.session and update != None:
  89.             cursor.execute(sqlupdate, (self.session["id"],update))
  90.             return "added"
  91.     def follow(self, username=None):
  92.         if "id" in self.session and user != None:
  93.             cursor.execute(nametoid,(username))
  94.             userid= cursor.fetchall()[0][0]
  95.             if cursor.execute(sqlfollowcheck,(self.session["id"],userid)):
  96.                 cursor.execute(sqlfollowdel, (self.session["id"],userid))
  97.                 return "Removed from following"
  98.             else:  
  99.                 cursor.execute(sqlfollow, (self.session["id"],userid))
  100.                 return "now following them =D"
  101.         else:
  102.             return "login to follow peeeps gggg"
  103. (Server(("0.0.0.0", 8000)) +Sessions() + Root()).run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement