def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Google auth failed") self.user = user current_user = conn.get("SELECT * FROM users WHERE email = %s and type = 'g'",\ user["email"]) if current_user is None: self.user["id"] = conn.execute("INSERT INTO users (type, email, name) VALUES ('g', %s, %s)",\ user["email"], user["name"]) self.user["city"] = "" else: self.user["id"] = current_user["id"] self.user["city"] = current_user["city"] ip = self.request.headers.get("X-Forwarded-For", None) or self.request.remote_ip conn.execute("UPDATE users SET last_login_ip = %s WHERE id = %s",\ ip, self.user["id"]) self.set_secure_cookie("user", tornado.escape.json_encode(self.user)) if self.redirect_url == "/": self.redirect_url == "/" + self.user["city"] self.redirect(self.redirect_url)
def post(self): #post comment if self.current_user: collection_id = self.get_argument("id") comment = self.get_argument("comment") conn.execute("INSERT INTO comments (collection_id, comment, user_id, create_time) VALUES (%s,%s,%s,%s)", collection_id, comment,self.current_user["id"], int(time.time())) self.finish({"error":None})
def post(self): if not self.current_user: self.redirect("/login") return email = self.get_argument("email") user_exist = conn.get("SELECT * FROM index_login WHERE login = %s", email) invited = conn.get("SELECT * FROM invite WHERE email = %s", email) if user_exist or invited: self.redirect("/invite?status=exists") return #send email invite_code = ''.join( random.choice(string.digits + string.letters) for x in range(14)) conn.execute("INSERT INTO invite (email, code) VALUES(%s, %s)", email, invite_code) self.email = urllib.quote(email) self.invite_code = invite_code msg = EmailMessage() msg.subject = "Invite from Pythonic Info" msg.bodyHtml = self.render_string("../template/email_invite.html") self.send(settings["email_sender"], str(email), msg) print "url:", msg.bodyText self.redirect("/invite?status=success")
def ranking(): now = time.time() offset = 0 while True: index_posts = conn.query( "SELECT * FROM index_posts ORDER BY rank DESC LIMIT %s, 100", offset) if len(index_posts) == 0: break post_ids = [post["entity_id"] for post in index_posts] for post_id, post in nomagic._get_entities_by_ids(post_ids): period = (now - time.mktime( datetime.datetime.strptime( post["datetime"], "%Y-%m-%dT%H:%M:%S.%f").timetuple())) / 3600 points = len(post["likes"]) post_rank = rank(points, period) conn.execute( "UPDATE index_posts SET rank = %s WHERE entity_id = %s", post_rank, post_id) offset += 100
def post(self): country = self.get_argument("country") conn.execute("UPDATE users SET country = %s WHERE id = %s",\ country, self.current_user["id"]) self.current_user["country"] = country self.set_secure_cookie("user", tornado.escape.json_encode(self.current_user))
def post(self): if not self.current_user: self.redirect("/login") return email = self.get_argument("email") user_exist = conn.get("SELECT * FROM index_login WHERE login = %s", email) invited = conn.get("SELECT * FROM invite WHERE email = %s", email) if user_exist or invited: self.redirect("/invite?status=exists") return #send email invite_code = ''.join(random.choice(string.digits+string.letters) for x in range(14)) conn.execute("INSERT INTO invite (email, code) VALUES(%s, %s)", email, invite_code) self.email = urllib.quote(email) self.invite_code = invite_code msg = EmailMessage() msg.subject = "Invite from Pythonic Info" msg.bodyHtml = self.render_string("../template/email_invite.html") self.send(settings["email_sender"], str(email), msg) print "url:", msg.bodyText self.redirect("/invite?status=success")
def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Google auth failed") self.user = user current_user = conn.get("SELECT * FROM users WHERE email = %s and type = 'g'",\ user["email"]) if current_user is None: self.user["id"] = conn.execute("INSERT INTO users (type, email, name) VALUES ('g', %s, %s)",\ user["email"], user["name"]) self.user["city"] = "" else: self.user["id"] = current_user["id"] self.user["city"] = current_user["city"] ip = self.request.headers.get("X-Forwarded-For", None) or self.request.remote_ip conn.execute("UPDATE users SET last_login_ip = %s WHERE id = %s",\ ip, self.user["id"]) self.set_secure_cookie("user", tornado.escape.json_encode(self.user)) if self.redirect_url == "/": self.redirect_url == "/"+self.user["city"] self.redirect(self.redirect_url)
def post(self): if self.current_user: board_id = self.get_argument("board_id") name = self.get_argument("name") category_id = self.get_argument("category_id") conn.execute("UPDATE boards SET name = %s, category_id = %s WHERE id = %s and user_id = %s", name, category_id, board_id, self.current_user["id"]) self.finish({"error":None})
def delete(self): collection_id = self.get_argument("id") if self.current_user: rows = conn.execute_rowcount("DELETE FROM collection WHERE id = %s and user_id = %s", collection_id, self.current_user["id"]) print rows #need to delete all likes if rows == 1: conn.execute("DELETE FROM likes WHERE collection_id = %s", collection_id) self.finish({"error":None})
def put(self): if self.current_user: name = self.get_argument("name") category_id = self.get_argument("category_id") board = conn.get("SELECT * FROM boards WHERE user_id = %s and name = %s", self.current_user["id"], name) if board: self.finish({"error":"duplicated name"}) else: conn.execute("INSERT INTO boards (user_id, name) VALUES (%s,%s)", self.current_user["id"], name) self.finish({"error":None})
def post(self): if self.current_user: collection_id = self.get_argument("id") user_id = self.current_user["id"] conn.execute("UPDATE collection SET likes = likes + 1 \ WHERE id = %s",\ collection_id) conn.execute("INSERT INTO likes (user_id, collection_id)\ VALUES (%s, %s)",\ user_id, collection_id)
def _on_userinfo(self, user): #print user self.user["email"] = user["email"] if "email" in user else "" ip = self.request.headers.get("X-Forwarded-For", None) or self.request.remote_ip conn.execute("UPDATE users SET email = %s, last_login_ip = %s WHERE id = %s",\ self.user["email"], ip, self.user["id"]) self.set_secure_cookie("user", tornado.escape.json_encode(self.user)) if self.redirect_url == "/": self.redirect_url == "/"+self.user["city"] self.redirect(self.redirect_url)
def delete(self): if self.current_user: board_id = self.get_argument("board_id") name = self.get_argument("name") category_id = self.get_argument("category_id") board = conn.get("SELECT * FROM boards WHERE user_id = %s and name = %s and id= %s and category_id = %s", self.current_user["id"], name, board_id, category_id) if board: conn.execute("DELETE FROM boards WHERE id = %s and user_id = %s", board_id, self.current_user["id"]) self.finish({"error":None}) else: self.finish({"error":"something else changed"})
def _on_userinfo(self, user): #print user self.user["email"] = user["email"] if "email" in user else "" ip = self.request.headers.get("X-Forwarded-For", None) or self.request.remote_ip conn.execute("UPDATE users SET email = %s, last_login_ip = %s WHERE id = %s",\ self.user["email"], ip, self.user["id"]) self.set_secure_cookie("user", tornado.escape.json_encode(self.user)) if self.redirect_url == "/": self.redirect_url == "/" + self.user["city"] self.redirect(self.redirect_url)
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name): index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name) index_data = nomagic._unpack(index["data"] or "{}") index_connection = set(index_data.get(index_connection_name, [])) if entity_id in index_connection: index_connection.remove(entity_id) index_data[index_connection_name] = list(index_connection) index_data_updated = nomagic._pack(index_data) conn.execute("UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name) entity = nomagic._get_entity_by_id(entity_id) entity_connection = set(entity.get(entity_connection_name, [])) if index_name in entity_connection: entity_connection.remove(index_name) entity[entity_connection_name] = list(entity_connection) nomagic._update_entity_by_id(entity_id, entity)
def post(self): if self.current_user: title = self.get_argument("title","") body = self.get_argument("body","") width = self.get_argument("width") height = self.get_argument("height") image_url = self.get_argument("image_url") source_url = self.get_argument("source_url") board_id = self.get_argument("board_id") user_id = self.current_user["id"] likes = 0 conn.execute("INSERT INTO collection (title, body, width, height, likes, image_url, source_url, user_id, board_id)\ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",\ title, body, width, height, likes, image_url, source_url, user_id, board_id) self.finish({"error":None})
def ranking(): now = time.time() offset = 0 while True: index_posts = conn.query("SELECT * FROM index_posts ORDER BY rank DESC LIMIT %s, 100", offset) if len(index_posts) == 0: break post_ids = [post["entity_id"] for post in index_posts] for post_id, post in nomagic._get_entities_by_ids(post_ids): period = (now - time.mktime(datetime.datetime.strptime(post["datetime"], "%Y-%m-%dT%H:%M:%S.%f").timetuple())) / 3600 points = len(post["likes"]) post_rank = rank(points, period) conn.execute("UPDATE index_posts SET rank = %s WHERE entity_id = %s", post_rank, post_id) offset += 100
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name): index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name) index_data = nomagic._unpack(index["data"] or "{}") index_connection = set(index_data.get(index_connection_name, [])) if entity_id in index_connection: index_connection.remove(entity_id) index_data[index_connection_name] = list(index_connection) index_data_updated = nomagic._pack(index_data) conn.execute( "UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name) entity = nomagic._get_entity_by_id(entity_id) entity_connection = set(entity.get(entity_connection_name, [])) if index_name in entity_connection: entity_connection.remove(index_name) entity[entity_connection_name] = list(entity_connection) nomagic._update_entity_by_id(entity_id, entity)
def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Facebook auth failed") self.user = user #print user #login or create account with fb current_user = conn.get("SELECT * FROM users WHERE facebook_id = %s and type = 'f'",\ user["id"]) if current_user is None: self.user["id"] = conn.execute("INSERT INTO users (type, facebook_id, name) VALUES ('f', %s, %s)",\ user["id"], user["name"]) self.user["city"] = "" else: self.user["id"] = current_user["id"] self.user["city"] = current_user["city"] #get fb email self.facebook_request(path="/me", callback=self._on_userinfo, access_token=user["access_token"])
def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Facebook auth failed") self.user = user #print user #login or create account with fb current_user = conn.get("SELECT * FROM users WHERE facebook_id = %s and type = 'f'",\ user["id"]) if current_user is None: self.user["id"] = conn.execute("INSERT INTO users (type, facebook_id, name) VALUES ('f', %s, %s)",\ user["id"], user["name"]) self.user["city"] = "" else: self.user["id"] = current_user["id"] self.user["city"] = current_user["city"] #get fb email self.facebook_request( path="/me", callback=self._on_userinfo, access_token=user["access_token"])
#! /usr/local/bin/python from setting import conn from random import random train = range(1, 100001) test = [] while len(test) < 20000: test.append(train.pop(int(random() * len(train)))) conn.execute('DELETE FROM core_train') conn.execute('DELETE FROM core_test') conn.execute('INSERT INTO core_test SELECT * FROM core_rating WHERE id IN %s' % str(tuple(test))) conn.execute('INSERT INTO core_train SELECT * FROM core_rating WHERE id IN %s' % str(tuple(train))) conn.commit()
def get(self): mac = "00:00:00:00:00:00" ipv4 = "10.0.0.1" name = "WifiPi Router" conn.execute("INSERT INTO device_log (ipv4, mac, name) VALUES (%s, %s, %s)", ipv4, mac, name) devices[ipv4] = name
def post(self): mac = self.get_argument("mac") ipv4 = self.get_argument("ipv4") name = self.get_argument("name", "") conn.execute("INSERT INTO device_log (ipv4, mac, name) VALUES (%s, %s, %s)", ipv4, mac, name) devices[ipv4] = name