def startConversation(self, request): try: user = request.user profileSender = utils.getProfile(user) idNotice = request.data.get('notice', None) notice = None if idNotice is None else Notice.getNotice(idNotice) idUserReceiver = request.data.get('user', None) profileReceiver = utils.getProfile(utils.getUser(idUserReceiver)) if notice is None else notice.profile profiles = [] profiles.append(profileReceiver) profiles.append(profileSender) text = request.data.get('text', None) image = request.data.get('image', None) if profiles[0] == profiles[1]: return Response({'success': False, 'err': 'Message to myself not allowed'}, status=status.HTTP_422_UNPROCESSABLE_ENTITY) if (text or image) and len(profiles) > 0: conversation = Conversation.create(profiles, notice)[0][0] Message.create(text, image, profileSender, conversation) utils.sendEmail(profiles[0].user.email, 'notifications/new_message.html') return Response({'success': True, 'msg': 'conversation-created'}, status=status.HTTP_201_CREATED) else: return Response({'success': False, 'err': 'Incomplete data'}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: if hasattr(e, 'message'): err = e.message else: err = e return Response({'success': False, 'err': str(err)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
def getData(self, request, pk=None): try: user = utils.getUser(pk) context = {} context['user'] = json.loads(serializers.serialize('json', [user], fields=('username', 'first_name', 'last_name', 'email', 'is_active', 'last_login', 'date_joined'))) profile = utils.getProfile(user) context['profile'] = json.loads(serializers.serialize('json', [profile])) context['profile'][0]['fields']['location'] = utils.getDataCities([profile.location]) context['profile'][0]['fields']['avatar'] = S3 + MEDIA_URL + str(profile.avatar) context['duration'] = utils.getDurationUser(user) context['numberFollowers'] = utils.getNumberFollowersUser(user) haveCategories = utils.getHaveCategoriesUser(user) context['haveCategories'] = json.loads(serializers.serialize('json', haveCategories)) for i, category in enumerate(context['haveCategories']): context['haveCategories'][i]['fields']['name'] = str(haveCategories[i]) searchCategories = utils.getSearchCategoriesUser(user) context['searchCategories'] = json.loads(serializers.serialize('json', searchCategories)) for i, category in enumerate(context['searchCategories']): context['searchCategories'][i]['fields']['name'] = str(searchCategories[i]) context['tags'] = json.loads(serializers.serialize('json', utils.getTagsUser(user))) return Response({'success': True, 'data': context}) except Exception as e: if hasattr(e, 'message'): err = e.message else: err = e return Response({'success': False, 'err': str(err)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
def getNotices(self, request, pk=None): try: user = utils.getUser(pk) notices = utils.getNoticesUser(user) context = utils.getDataNotice(notices) return Response({'success': True, 'data': context}) except Exception as e: if hasattr(e, 'message'): err = e.message else: err = e return Response({'success': False, 'err': str(err)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
def checkFollowing(self, request): try: following = utils.getProfile(utils.getUser(request.data.get('user', None))) follower = utils.getProfile(request.user) if following is None: return Response({'success': False, 'err': 'field-undefined'}, status=status.HTTP_400_BAD_REQUEST) return Response({'success': True, 'following': Follow.checkFollowing(follower, following)}) except Exception as e: if hasattr(e, 'message'): err = e.message else: err = e return Response({'success': False, 'err': str(err)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
def unfollow(self, request): try: follower = utils.getProfile(request.user) user = request.data.get('user', None) if user is None: return Response({'success': False, 'err': 'field-undefined'}, status=status.HTTP_400_BAD_REQUEST) following = utils.getProfile(utils.getUser(user)) instance = Follow.getFollowing(follower, following) instance.delete() return Response({'success': True}) except Exception as e: if hasattr(e, 'message'): err = e.message else: err = e return Response({'success': False, 'err': str(err)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
def create(self, request, *args, **kwargs): follower = utils.getProfile(request.user) user = request.data.get('user', None) if user is None: return Response({'success': False, 'err': 'field-undefined'}, status=status.HTTP_400_BAD_REQUEST) following = utils.getProfile(utils.getUser(user)) if Follow.checkFollowing(follower.id, following.id): return Response({'success': False, 'err': 'already following'}, status=status.HTTP_422_UNPROCESSABLE_ENTITY) request.data['following'] = following.id request.data['follower'] = follower.id serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) utils.sendEmail(following.user.email, 'notifications/new_follower.html') return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)