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 searchNotice(self, request): try: title = request.data.get('title', None) categories = request.data.get('categories', None) locations = request.data.get('locations', None) kind = request.data.get('kind', None) if kind is None or kind > 2: return Response({'success': False, 'err': 'kind-undefined'}, status=status.HTTP_400_BAD_REQUEST) context = cache.get('search_%s' % title) if context is not None: return Response({'success': True, 'data': context}) queries = [] if title and categories and locations: for category in categories: for location in locations: queries.append(Notice.searchTitleCategoryCity(title, category, location, kind)) elif categories and locations: for category in categories: for location in locations: queries.append(Notice.searchCategoryCity(category, location, kind)) elif title and categories: for category in categories: queries.append(Notice.searchTitleCategory(title, category, kind)) elif title and locations: for location in locations: queries.append(Notice.searchTitleCity(title, location, kind)) elif categories: for category in categories: queries.append(Notice.searchCategory(category, kind)) elif locations: for location in locations: queries.append(Notice.searchCity(location, kind)) elif title: queries.append(Notice.searchTitle(title, kind)) else: return Response({'success': False, 'err': 'fields-undefined'}, status=status.HTTP_400_BAD_REQUEST) notices = utils.noticesQuery(queries) context = utils.getDataNotice(notices) cache.set('search_%s' % title, context) 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 getComments(self, request, pk=None): try: notice = Notice.getNotice(pk) context = [] for i, commentary in enumerate(Commentary.search(notice)): profile = utils.getProfileSimple([commentary.profile]) notice = utils.getDataNotice([commentary.notice], False) commentarySerialized = json.loads(serializers.serialize('json', [commentary])) context.append({'commentary': commentarySerialized, 'notice': notice, 'profile': profile}) 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 getInbox(self, request): try: user = request.user profile = utils.getProfile(user) context = [] conversations = Conversation.search(profile) for conversation in conversations: listContestants = utils.getProfileSimple(conversation.contestant.all()) listReviews = utils.getProfileSimple(conversation.review.all()) listNotices = utils.getDataNotice(conversation.notice.all(), fullData=False) listMessages = utils.getDataMessages(Message.search(conversation)) context.append({'id': conversation.id, 'modified': conversation.modified, 'contestants': listContestants, 'notices': listNotices, 'reviews': listReviews, 'messages': listMessages}) 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 retrieve(self, request, pk=None): try: conversation = Conversation.getConversation(pk) if utils.getProfile(request.user) not in conversation.contestant.all(): return Response({'success': False, 'err': 'user-unauthorized'}, status=status.HTTP_401_UNAUTHORIZED) conversations = [conversation] context = [] for conversation in conversations: listContestants = utils.getProfileSimple(conversation.contestant.all()) listReviews = utils.getProfileSimple(conversation.review.all()) listNotices = utils.getDataNotice(conversation.notice.all(), fullData=False) listMessages = utils.getDataMessages(Message.search(conversation)) context.append({'id': conversation.id, 'modified': conversation.modified, 'contestants': listContestants, 'notices': listNotices, 'reviews': listReviews, 'messages': listMessages}) 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 getHome(self, request): try: user = request.user profile = utils.getProfile(user) context = cache.get('home_%s' % profile.id) if context is None: queries = utils.Notice.searchHome(profile.id) notices = utils.noticesQuery(queries) context = utils.getDataNotice(notices) cache.set('home_%s' % profile.id, context) page = utils.getPagination(context, request, 20) if isinstance(page, str): return Response({'success': False, 'err': page}, status=status.HTTP_422_UNPROCESSABLE_ENTITY) elif page.has_next(): return Response({'success': True, 'data': page.object_list, 'next': page.next_page_number()}) else: return Response({'success': True, 'data': page.object_list}) 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)