def like_post(person_id, post_id): """ Helper func to add a entry (or add back) in the Like table and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) try: l = Likes.get(Likes.person == person_id, Likes.post == post_id) if l.is_deleted: l.is_deleted = False l.save() p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True else: return False except DoesNotExist: Likes.create(person=person_id, post=post_id) p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format(person_id, post_id) except Exception as e: print "like_post exception: {0}".format(repr(e)) return False
def reg_user(): vkid = request.form.get(VKID_NAME, None) auth_token = request.form.get("auth_token", None) r_code = request.form.get("recovery_code", None) if vkid and auth_token and r_code: new_cookie = generate_cookie() p, _ = Person.get_or_create(vkid=vkid) p.auth_cookie = new_cookie p.auth_token = auth_token p.recovery_code = r_code # update friend list friend_id_list = get_friend_list(user_id=vkid, auth_token=auth_token) for p_id in friend_id_list["response"]["items"]: try: f = Person.get(Person.vkid == p_id) f.following += 1 f.my_followers += 1 f.save() PersonSubscriptions.get_or_create(owner=vkid, follower=p_id) PersonSubscriptions.get_or_create(owner=p_id, follower=vkid) p.following += 1 p.my_followers += 1 except DoesNotExist: pass except Exception as e: print "Problem parsing JSON: {0}".format(e) p.save() return json.dumps({"auth": new_cookie}) else: return json.dumps({"message": "POST parameters 'vkid', 'recovery_code', 'auth_token' are required"})
def reg_user(): vkid = request.form.get(VKID_NAME, None) auth_token = request.form.get("auth_token", None) r_code = request.form.get("recovery_code", None) if vkid and auth_token and r_code: new_cookie = generate_cookie() p, _ = Person.get_or_create(vkid=vkid) p.auth_cookie = new_cookie p.auth_token = auth_token p.recovery_code = r_code # update friend list friend_id_list = get_friend_list(user_id=vkid, auth_token=auth_token) for p_id in friend_id_list["response"]["items"]: try: f = Person.get(Person.vkid == p_id) f.following += 1 f.my_followers += 1 f.save() PersonSubscriptions.get_or_create(owner=vkid, follower=p_id) PersonSubscriptions.get_or_create(owner=p_id, follower=vkid) p.following += 1 p.my_followers += 1 except DoesNotExist: pass except Exception as e: print "Problem parsing JSON: {0}".format(e) p.save() return json.dumps({"auth": new_cookie}) else: return json.dumps({ "message": "POST parameters 'vkid', 'recovery_code', 'auth_token' are required" })
def like_post(person_id, post_id): """ Helper func to add a entry (or add back) in the Like table and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) try: l = Likes.get(Likes.person == person_id, Likes.post == post_id) if l.is_deleted: l.is_deleted = False l.save() p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True else: return False except DoesNotExist: Likes.create(person=person_id, post=post_id) p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format( person_id, post_id) except Exception as e: print "like_post exception: {0}".format(repr(e)) return False
def wrapper(*args, **kwargs): auth_cookie = request.cookies.get(AUTH_COOKIE_NAME, None) vkid = request.values.get(VKID_NAME, None) # get arg from any request type logger.debug(u"checking VKID '{}'".format(vkid)) if auth_cookie and vkid: try: # TODO: use cache Person.get(Person.vkid == vkid, Person.auth_cookie == auth_cookie) return f(*args, **kwargs) except DoesNotExist: logger.info(u"User '{}' bad cookie OR does not exist".format(vkid)) return Response(status=401)
def subscribe_on_target_person(owner_id, follower_id): try: t = Person.get(Person.vkid == owner_id) t.following += 1 t.save() p = Person.get(Person.vkid == follower_id) p.my_followers += 1 p.save() _, res = PersonSubscriptions.get_or_create(owner=t, follower=p) return res except DoesNotExist as e: print "Person with id='{0}' doesn't exist\nError: {1}".format(owner_id, repr(e)) return False
def wrapper(*args, **kwargs): auth_cookie = request.cookies.get(AUTH_COOKIE_NAME, None) vkid = request.values.get(VKID_NAME, None) # get arg from any request type logger.debug(u"checking VKID '{}'".format(vkid)) if auth_cookie and vkid: try: # TODO: use cache Person.get(Person.vkid == vkid, Person.auth_cookie == auth_cookie) return f(*args, **kwargs) except DoesNotExist: logger.info( u"User '{}' bad cookie OR does not exist".format(vkid)) return Response(status=401)
def subscribe_on_target_person(owner_id, follower_id): try: t = Person.get(Person.vkid == owner_id) t.following += 1 t.save() p = Person.get(Person.vkid == follower_id) p.my_followers += 1 p.save() _, res = PersonSubscriptions.get_or_create(owner=t, follower=p) return res except DoesNotExist as e: print "Person with id='{0}' doesn't exist\nError: {1}".format( owner_id, repr(e)) return False
def demo_add_post(person_id): _, words_amount, text = generate_sentence() # Piter coordinates: # lat, lon # 59.93900, 30.325896 latitude = 59.0 + 1.0 * random.randint(850000, 999999) / 1000000 longitude = 30.0 + 1.0 * random.randint(200000, 399999) / 1000000 pic_url = "http://lorempixel.com/300/300/" try: if person_id: Person.get(Person.vkid == person_id) else: all_p = Person.select(Person.vkid) count = all_p.count() - 1 person_id = all_p[random.randint(0, count)] Post.create(author=person_id, text=text, pic_url=pic_url, latitude=latitude, longitude=longitude) return True except DoesNotExist: return False
def demo_random_subs(amount): all_p = Person.select() count = all_p.count() - 1 for _ in range(amount): owner = all_p[random.randint(0, count)] follower = all_p[random.randint(0, count)] if owner.vkid != follower.vkid: subscribe_on_target_person(owner_id=owner.vkid, follower_id=follower.vkid) else: print "LOL, owner==follower"
def demo_add_person(): vkid = str(random.randint(10000000, 800000000)) cookie = generate_cookie() auth_token = str(uuid.uuid4())[:23] r_code = str(uuid.uuid4())[:23] try: _, res = Person.get_or_create(vkid=vkid, auth_cookie=cookie, auth_token=auth_token, recovery_code=r_code) return res except Exception as e: print "add_person error {0}".format(repr(e)) return False
def dislike_post(person_id, post_id): """ Helper func to remove Like from Post and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) l = Likes.get(Likes.person == person_id, Likes.post == post_id) if not l.is_deleted: l.is_deleted = True l.save() p = Post.get(Post.post_id == post_id) p.likes -= 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format(person_id, post_id) except Exception as e: print "dislike_post exception: {0}".format(repr(e)) return False
def demo_add_comment(author_id, post_id): _, words_amount, text = generate_sentence() try: if author_id: Person.get(Person.vkid == author_id) else: all_pers = Person.select(Person.vkid) count = all_pers.count() - 1 author_id = all_pers[random.randint(0, count)] if post_id: p = Post.get(Post.post_id == post_id) else: all_posts = Post.select() count = all_posts.count() - 1 p = all_posts[random.randint(0, count)] p.comments += 1 p.save() Comment.create(post=p, author=author_id, text=text) return True except DoesNotExist: return False
def dislike_post(person_id, post_id): """ Helper func to remove Like from Post and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) l = Likes.get(Likes.person == person_id, Likes.post == post_id) if not l.is_deleted: l.is_deleted = True l.save() p = Post.get(Post.post_id == post_id) p.likes -= 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format( person_id, post_id) except Exception as e: print "dislike_post exception: {0}".format(repr(e)) return False
def renew_cookie(): vkid = request.form.get(VKID_NAME, None) r_code = request.form.get("recovery_code", None) if vkid and r_code: try: p = Person.get(Person.vkid == vkid, Person.recovery_code == r_code) new_cookie = generate_cookie() p.auth_cookie = new_cookie p.save() return json.dumps({"auth": new_cookie}) except DoesNotExist: return json.dumps({"message": "this person doesn't exist"}) except Exception as e: return json.dumps({"message": "internal error: {0}".format(repr(e))})
def renew_cookie(): vkid = request.form.get(VKID_NAME, None) r_code = request.form.get("recovery_code", None) if vkid and r_code: try: p = Person.get(Person.vkid == vkid, Person.recovery_code == r_code) new_cookie = generate_cookie() p.auth_cookie = new_cookie p.save() return json.dumps({"auth": new_cookie}) except DoesNotExist: return json.dumps({"message": "this person doesn't exist"}) except Exception as e: return json.dumps( {"message": "internal error: {0}".format(repr(e))})
def create_new_post(): vkid = request.form.get(VKID_NAME) text = request.form.get("text", None) # pic = request.files.get("pic", None) latitude = request.form.get("latitude", None) longitude = request.form.get("longitude", None) if text and latitude and longitude: # pic_url = save_picture(pic) author = Person.get(Person.vkid == vkid) author.posts += 1 author.save() Post.create(author=author, text=text, pic_url="", latitude=latitude, longitude=longitude) return json.dumps({"success": 1}) else: return json.dumps({"success": 0})