def item_unfavor(item_id): item = Models.Item.objects(item_id=item_id).first_or_404() if current_user.is_authenticated(): current_user.unmark_favored(item) else: guest = Models.GuestRecord.by_key(get_session_key()) guest.unmark_favored(item) return jsonify(message='OK')
def favor_items(): if current_user.is_authenticated(): user = current_user._get_current_object() else: # guest user user = Models.GuestRecord.by_key(get_session_key()) items = Models.Item.objects(item_id__in=user.favor_items) return jsonify(message='OK', items=[Json.item_json_in_list(i) for i in items])
def item_detail(item_id): item = Models.Item.objects(item_id=item_id).first_or_404() item_dict = Json.item_json(item) if current_user.is_authenticated(): user = current_user._get_current_object() else: user = Models.GuestRecord.by_key(get_session_key()) item_dict.update({'is_favored': item_id in user.favor_items}) log_item_visit(user_id=user.id, item_id=item_id) return jsonify(message='OK', item=item_dict)
def post_detail(post_id): post = Models.Post.objects(post_id=post_id).first_or_404() post_dict = Json.post_json(post) comments = Models.PostComment.objects(post=post) comments_json = [c.to_json() for c in comments] likes = Models.PostLike.objects(post=post) likes_json = [l.to_json() for l in likes] post_dict.update({'comments': comments_json}) post_dict.update({'likes': likes_json}) if current_user.is_authenticated: user = current_user._get_current_object() else: user = Models.GuestRecord.by_key(get_session_key()) log_post_visit(user_id=user.id, post_id=post_id) return jsonify(message='OK', post=post_dict)
def combine_favor_items(sender, user): guest_record = Models.GuestRecord.by_key(key=get_session_key()) current_user.favor_items = list( set(current_user.favor_items + guest_record.favor_items)) current_user.num_favors = len(current_user.favor_items) current_user.save()