def event_update(request, event_id): event = get_object_or_404(Event, pk=event_id) success = True msg = '' serializer = EventSerializer( event, data=request.data, context={'user': request.user, 'instance': event} ) success = serializer.is_valid() if success: event = serializer.save() msg = 'Evento atualizado com sucesso.' http_status = status.HTTP_202_ACCEPTED else: for key, value in serializer.errors.items(): msg = msg.join(value) http_status = status.HTTP_200_OK return Response({ 'success': success, 'msg': msg }, status=http_status )
def event_add(request): success = True msg = '' # Tenta serializar dados recebidos num evento válido serializer = EventSerializer( data=request.data, context={'user': request.user} ) success = serializer.is_valid() if success: event = serializer.save(creator=request.user.pk) EventSubscription(user=request.user, event=event).save() msg = 'Evento criado com sucesso.' http_status = status.HTTP_201_CREATED else: for key, value in serializer.errors.items(): msg = msg.join(value) http_status = status.HTTP_200_OK return Response({ 'success': success, 'msg': msg }, status=http_status )
def event_get(request, event_id): event = get_object_or_404(Event, pk=event_id) return Response({ 'success': True, 'msg': EventSerializer(event).data }, status=status.HTTP_200_OK )
def event_invite_details(request, id): # Checagem de existência e permissão event = get_object_or_404(Event, id=id) if (event.creator != request.user): return Response({ 'success': False, 'msg': 'Você não tem autorização para convidar outros usuários' ' para este evento.' }, status=status.HTTP_403_FORBIDDEN ) # Busca de informações subscribed = EventSubscription.objects.filter(event=event) \ .exclude(user=request.user) \ .values('user__id', 'user__first_name', 'user__last_name') invited = EventInvitation.objects.filter(event=event) \ .exclude(rejected=True) \ .values('user__id', 'user__first_name', 'user__last_name') rejected = EventInvitation.objects.filter(event=event) \ .exclude(rejected=False) \ .values('user__id', 'user__first_name', 'user__last_name') users = get_user_model().objects \ .exclude(pk=request.user.pk) \ .exclude(is_superuser=True) \ .exclude(id__in=[subs['user__id'] for subs in subscribed]) \ .exclude(id__in=[invt['user__id'] for invt in invited]) \ .exclude(id__in=[rejc['user__id'] for rejc in rejected]) \ user_list = [] for user in users: user_list.append({ 'id': user.id, 'text': user.first_name + ' ' + user.last_name }) success = True msg = {} msg.update({ 'event': EventSerializer(event).data, 'invitations': { 'invited': invited, 'subscribed': subscribed, 'rejected': rejected }, 'users': user_list, }) return Response({ 'success': success, 'msg': msg }, status=status.HTTP_200_OK )
def event_subscriptions_list_year(request, year): success = True msg = '' event_subscriptions = EventSubscription.objects \ .filter(user_id=request.user) \ .filter(event__start_datetime__year=year) \ msg = {} for month in range(1, 13): month_subscriptions = event_subscriptions \ .filter(event__start_datetime__month=month) \ .order_by('event__start_datetime') if month_subscriptions.count() > 0: msg[str(month)] = [] for index, sub in enumerate(month_subscriptions): msg[str(month)].append({ 'event': EventSerializer(sub.event).data, 'creator': ( sub.event.creator.first_name + ' ' + sub.event.creator.last_name if sub.event.creator != request.user else None # 'None' vira 'null' ao parsear em JSON ), 'subscription_id': sub.id }) if sub.event.creator == request.user: subscribed = EventSubscription.objects \ .filter(event=sub.event) \ .exclude(user=request.user) \ .count() invited = EventInvitation.objects \ .filter(event=sub.event) \ .exclude(rejected=True) \ .count() rejected = EventInvitation.objects \ .filter(event=sub.event) \ .exclude(rejected=False) \ .count() msg[str(month)][index].update({ 'invited': invited, 'subscribed': subscribed, 'rejected': rejected, }) return Response({ 'success': success, 'msg': msg }, status=status.HTTP_200_OK )
def event_next_list(request, count): success = True msg = [] next_events = request.user.eventsubscription_set \ .filter(event__start_datetime__gte=now()) \ .order_by('event__start_datetime')[:int(count)] for index, subs in enumerate(next_events): msg.append({ 'event': EventSerializer(subs.event).data, 'creator': ( subs.event.creator.first_name + ' ' + subs.event.creator.last_name if subs.event.creator != request.user else None # 'None' vira 'null' ao parsear em JSON ), }) if subs.event.creator == request.user: subscribed = EventSubscription.objects \ .filter(event=subs.event) \ .exclude(user=request.user) \ .count() invited = EventInvitation.objects \ .filter(event=subs.event) \ .exclude(rejected=True) \ .count() rejected = EventInvitation.objects \ .filter(event=subs.event) \ .exclude(rejected=False) \ .count() msg[index].update({ 'invited': invited, 'subscribed': subscribed, 'rejected': rejected, }) return Response({ 'success': success, 'msg': msg }, status=status.HTTP_200_OK )
def event_invite_list(request): success = True msg = '' event_invitations = EventInvitation.objects \ .filter(user_id=request.user) \ .filter(rejected=False) \ .order_by('event__start_datetime') msg = [] for inv in event_invitations: msg.append({ 'event': EventSerializer(inv.event).data, 'creator': inv.event.creator.first_name + ' ' + inv.event.creator.last_name, 'invite_id': inv.id }) return Response({ 'success': success, 'msg': msg })