def friend_serialize(friend): data_serialize = [] if type(friend) == QuerySet: for i in friend: data = {'friend': serialize_user_basic(i.friend)} data_serialize.append(data) else: data = {'friend': serialize_user_basic(friend.friend)} data_serialize.append(data) return data_serialize
def get(self, request): if check_authentication(request): user_request = get_user(request) try: total = int(request.GET.get('total')) except: response_error() target = request.GET.get('target') user_receive_id = request.GET.getlist('listId[]') #now it is just 1 item in user_receive_id user_receive_id = user_receive_id[0] try: user_receive = User.objects.get(user_id=user_receive_id) except User.DoesNotExist: response_error() if total == 2 and target == 'user': m = MessageFrame.objects.filter(message_type='user').filter( users=user_request).filter(users=user_receive) if len(m) == 1 and len(m[0].users.all()) == 2: data = { 'frame_id': m[0].id, 'status_code': 'ok', 'users': [ serialize_user_basic(user_request), serialize_user_basic(user_receive) ] } elif len(m) == 0: m = MessageFrame.objects.create(message_type='user') m.users.add(user_request) m.users.add(user_receive) m.save() data = { 'frame_id': m.id, 'status_code': 'ok', 'users': [ serialize_user_basic(user_request), serialize_user_basic(user_receive) ] } else: data = {'status_code': 'error'} else: data = {'status_code': 'error'} response = HttpResponse('while(1);' + json.dumps(data), content_type='text/javascript') set_response_header(response) return response
def status_vote_serialize(status): sv = status.status_statusvote_status.all() data_se = [] for i in sv: if i.been_vote == True: data_se.append({'user': serialize_user_basic(i.user)}) return data_se
def header_search(user=None): data = {} data['users'] = [] if user: for i in user: data_se = serialize_user_basic(i) data['users'].append(data_se) return data
def status_reaction_user_vote_serialize(statusreaction): sv = statusreaction.status_statusreactionvote_statusreaction.filter( been_vote=True) data = [] for i in sv: data_se = {'user': serialize_user_basic(i.user)} data.append(data_se) return data
def status_B_serialize(noti): status_b_notification = noti.noti_statusbnotification_noti reset_B(status_b_notification) return { 'status': status_b_notification.status.id, 'user': serialize_user_basic(status_b_notification.user), 'statuscomment': status_b_notification.statuscomment.id, 'comment': status_b_notification.statuscomment.comment }
def serialize_friendtag(post): data = [] try: ft = post.post_postfriendtag_post f = ft.friend.all() for i in f: data.append(serialize_user_basic(i)) return data except ObjectDoesNotExist: return data
def plustag_vote_serialize(plustag): pv = plustag.plustag_plustagvote_plustag.filter(been_vote=True) data = [] for i in pv: data.append({ 'user_vote': serialize_user_basic(i.user_vote), 'been_vote': i.been_vote, 'time': i.time.isoformat() }) return data
def plustag_serialize(plustag): return { 'id': plustag.pk, 'user_receiver_plus': serialize_user_basic(plustag.user_receive_plus) if plustag.user_receive_plus else None, 'user_send_plus': serialize_user_basic(plustag.user_send_plus) if plustag.user_send_plus else None, 'status': plustag.status.id if plustag.status else None, 'plustag_name': plustag.plustag_name, 'votes': plustag.votes, 'create_time': plustag.create_time.isoformat(), 'uservotes': plustag_vote_serialize(plustag) }
def status_comment_serialize(status, number_query=3, page=0): data = [] sc = status.status_statuscomment_status.all().order_by( '-time_create')[page:page + number_query] for i in sc: obj = { 'user': serialize_user_basic(i.user), 'id': i.pk, 'comment': i.comment, 'time': i.time_create.isoformat() } data.append(obj) return data
def status_serialize(status, user=None): data_serialize = [] if type(status) == QuerySet: for i in status: data_in = { 'id': i.id, 'type': i.verb, 'votes': i.vote, 'user': serialize_user_basic(i.user), 'post': serialize_post(i.post), 'time': i.time_create.isoformat(), 'totalComments': len(i.status_statuscomment_status.all()), 'comments': status_comment_serialize(i, 3), 'plustags': status_plustag_serialize(i), 'reactions': status_reaction_serialize(i, user), 'df_r': i.use_default_reaction, 'ct_r': i.use_custom_reaction } data_serialize.append(data_in) else: data_in = { 'id': status.id, 'type': status.verb, 'votes': status.vote, 'user': serialize_user_basic(status.user), 'post': serialize_post(status.post), 'time': status.time_create.isoformat(), 'totalComments': len(status.status_statuscomment_status.all()), 'comments': status_comment_serialize(status, 3), 'plustags': status_plustag_serialize(status), 'reactions': status_reaction_serialize(status, user), 'df_r': status.use_default_reaction, 'ct_r': status.use_custom_reaction } data_serialize.append(data_in) return data_serialize
def friend_serialize(friend): data_serialize = [] if type(friend) == QuerySet: for i in friend: try: time = i.friend.onlinetime.time except ObjectDoesNotExist: time = i.friend.last_login data = { 'friend': serialize_user_basic(i.friend), 'time': time.isoformat() } data_serialize.append(data) else: try: time = i.friend.onlinetime.time except ObjectDoesNotExist: time = i.friend.last_login data = { 'friend': serialize_user_basic(friend.friend), 'time': time.isoformat() } data_serialize.append(data) return data_serialize
def get(self, request): if check_authentication(request, True): user = get_user(request) data = {'status_code': 'ok', 'user': serialize_user_basic(user)} response = HttpResponse( 'while(1);' + json.dumps(data), content_type='application/x-javascript; charset=utf-8') set_response_header(response) return response data = {'status_code': 'error'} response = HttpResponse( 'while(1);' + json.dumps(data), content_type='application/x-javascript; charset=utf-8') set_response_header(response) return response
def serialize_post(post): images = [] if post.has_link_image: image_objects = post.image.all() for i in image_objects: images.append(serialize_image(i)) return { 'user': serialize_user_basic(post.user), 'text': post.text, 'saleItem': post.sale_item, 'time': post.time_create.isoformat(), 'has_link_image': post.has_link_image, 'old_image': post.old_image.url if post.old_image else '', 'images': images, 'friends': serialize_friendtag(post) }
def get(self, request, *a, **kw): isSelf = False isStranger = False isFriend = False user = None if check_authentication(request): user = get_user(request) friends = Friend.objects.filter(user=user) profile_url = request.GET.get('profileUrl') try: user_visited = User.objects.get( account__profilename__exact=profile_url) except User.DoesNotExist: response = JsonResponse({'status_code': 'error'}) set_response_header(response) return response if user: if user == user_visited: isSelf = True elif user_visited.id in friends.values_list('friend_id', flat=True): isFriend = True else: isStranger = True data = { 'user': serialize_user_basic(user_visited), 'relationShip': { 'isSelf': isSelf, 'isFriend': isFriend, 'isStranger': isStranger }, 'status_code': 'ok' } response = JsonResponse(data) set_response_header(response) return response
def get(self, request): # tab id of each tab. If this is empty, it means that tab has not created. This is required tab_id = request.GET.get('tab_id') # tab name of each tab. This is optional because searching not based on name but based on id. When tab_id is empty, this must be not empty. Such as 'family' because it has created on client but not on the server so it has name but not has id yet tab_name = request.GET.get('tab_name') # type of GET request: 'fetch_user' is fetching all User in this tab and 'filter_user' is filtering User when user type name in input action_type = request.GET.get('action_type') # input_name is text user type in input to filter user has not been in this tab input_name = request.GET.get('input_name') # friend Ids is the ID has been typed in input field. Those will be filtered out for the results. friendIds = request.GET.getlist('friend_ids') # check authentication and get user if check_authentication(request): user = get_user(request) if action_type == 'fetch_user': if tab_id: try: TabFollower = FollowTimeline.objects.get( follow_id=tab_id, user=user) users = TabFollower.members.all() data_se = [] for i in users: data_se.append(serialize_user_basic(i)) data = {'status_code': 'ok', 'data': data_se} except FollowTimeline.DoesNotExist: data = {'status_code': 'error'} elif tab_name: try: TabFollower = FollowTimeline.objects.get(name=tab_name, user=user) users = TabFollower.members.all() data_se = [] for i in users: data_se.append(serialize_user_basic(i)) data = {'status_code': 'ok', 'data': data_se} except FollowTimeline.DoesNotExist: data = {'status_code': 'error'} else: data = {'status_code': 'ok'} response = JsonResponse(data) set_response_header(response) return response elif action_type == 'filter_user': if tab_id: try: TabFollower = FollowTimeline.objects.get( follow_id=tab_id, user=user) users_id_in_tab_followers = TabFollower.members.all( ).values_list('id', flat=True) friends = Friend.objects.filter( friend__fullname__icontains=input_name, user=user).exclude( friend__user_id__in=users_id_in_tab_followers ).exclude(friend__user_id__in=friendIds) data_se = [] for i in friends: data_se.append(serialize_user_basic(i.friend)) data = {'status_code': 'ok', 'data': data_se} except FollowTimeline.DoesNotExist: data = {'status_code': 'error'} elif tab_name: try: TabFollower = FollowTimeline.objects.get(name=tab_name, user=user) users_id_in_tab_followers = TabFollower.members.all( ).values_list('id', flat=True) friends = Friend.objects.filter( friend__fullname__icontains=input_name, user=user).exclude( friend__user_id__in=users_id_in_tab_followers ).exclude(friend__user_id__in=friendIds) data_se = [] for i in friends: data_se.append(serialize_user_basic(i.friend)) data = {'status_code': 'ok', 'data': data_se} except FollowTimeline.DoesNotExist: data = {'status_code': 'error'} else: data = {'status_code': 'ok'} response = JsonResponse(data) set_response_header(response) return response else: data = {'status_code': 'error'} response = JsonResponse(data) set_response_header(response) return response
def status_A_serialize(noti): status_a_notification = noti.noti_statusanotification_noti return { 'status': status_a_notification.status.id, 'user': serialize_user_basic(status_a_notification.user) }
def friend_info(user, number_friends = 10): friend = user.friend_friend_user.all()[0:number_friends] data = [] for i in friend: data.append(serialize_user_basic(i.friend)) return data
def post(self, request): if check_authentication(request): user = get_user(request) try: reactionPk = int(request.POST.get('reaction')) reaction_type = request.POST.get('type') except ValueError: return JsonResponse({'status_type': 'ok'}) status_reaction = StatusReaction.objects.get( pk=reactionPk, reactionType=reaction_type) status = status_reaction.status try: #vote = StatusVote.objects.create(user = user, status = status) #status.vote += 1 #status.save() #vote.been_vote = True #vote.save() #if status.user and user != status.user: # noti = Notification.objects.create(user = status.user, noti_type = 'status-a') # StatusANotification.objects.create(noti = noti, status = status, who_vote = user) reaction_vote = StatusReactionVote.objects.create( statusreaction=status_reaction, user=user) # specific reaction of status status_reaction.vote += 1 status_reaction.save() reaction_vote.been_vote = True reaction_vote.save() # the whole votes for all reactions status.vote += 1 status.save() if status.user and user != status.user: noti = Notification.objects.create(user=status.user, noti_type='status-a') StatusANotification.objects.create(noti=noti, status=status, user=user) except IntegrityError: #vote = StatusVote.objects.get(user = user, status = status) #if vote.been_vote == True: # status.vote -= 1 # status.save() # vote.been_vote = False # vote.save() #else: # status.vote += 1 # status.save() # vote.been_vote = True # vote.save() reaction_vote = StatusReactionVote.objects.get( statusreaction=status_reaction, user=user) if reaction_vote.been_vote == True: reaction_vote.been_vote = False reaction_vote.save() status_reaction.vote -= 1 status_reaction.save() status.vote -= 1 status.save() else: reaction_vote.been_vote = True reaction_vote.save() status_reaction.vote += 1 status_reaction.save() status.vote += 1 status.save() heart = 0 if status.user: try: friend = status.user x = Friend.objects.get(user=user, friend=friend) try: t = FriendRelationship.objects.get(user=user, friend=x) except FriendRelationship.DoesNotExist: t = FriendRelationship.objects.create(user=user, friend=x) finally: try: a = FriendHeart.objects.get(user=user, friend=friend, friendrelationship=t, source='status', reason='like', status=status) except FriendHeart.DoesNotExist: a = FriendHeart.objects.create( user=user, friend=friend, friendrelationship=t, source='status', reason='like', status=status) finally: if reaction_vote.been_vote == True: a.heart = 1 t.friend_heart += a.heart else: t.friend_heart -= a.heart a.heart = 0 a.save() t.save() zz = FriendHeart.objects.filter( user=user, friend=friend, friendrelationship=t, status=status) for i in zz: heart += i.heart except Friend.DoesNotExist: pass response = JsonResponse({ 'been_vote': reaction_vote.been_vote, 'user': { 'user': serialize_user_basic(user) }, 'status_type': 'ok' }) set_response_header(response) return response