def solicitar(self, request): """ --- method_path: /clientes/solicitar/ method_action: POST desc: Solicitar alteração de senha. input: - name: email desc: Email do usuário. type: str required: True location: form """ to_email, *_ = get_fields(request.data, ['email']) user = get_object_or_404(User, email=to_email) mail_subject = 'Solicitação para alteração de senha.' message = render_to_string('website/pass_reset.html', { 'user': user, 'domain': settings.FRONT_END_HOST, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) email = EmailMessage( mail_subject, message, to=[to_email] ) email.send() return Response({'message': 'A solicitação será enviada para o seu email.'})
def down(self, request, pk): """ --- method_path: /groups/{id}/down/ method_action: PATCH desc: Diminuir a ordem do conteúdo input: - name: content desc: Id do conteúdo type: integer required: True location: form """ group = self.get_object() content_pk, *_ = get_fields(request.data, ['content']) content = get_object_or_404(Content, pk=content_pk) if content.order < group.group_contents.count(): group.group_contents.filter(order=content.order + 1).update(order=content.order) content.order += 1 content.save() Log.objects.create(user=request.user, action='update', content_object=content) serializer = self.get_serializer(group) return Response(serializer.data)
def add(self, request, pk): """ --- method_path: /groups/{id}/add/ method_action: POST desc: Adicionar conteúdo no grupo. input: - name: media desc: Id da mídia. type: integer required: True location: form - name: duration desc: Duração da exibição type: str required: True location: form """ group = self.get_object() media_pk, duration = get_fields(request.data, ['media', 'duration']) media = get_object_or_404(Media, pk=media_pk) content = Content.objects.create( media=media, duration=dateparse.parse_duration(duration), group=group, order=group.group_contents.count() + 1) Log.objects.create(user=request.user, action='create', content_object=content) serializer = ContentSerializer(content) return Response(serializer.data)
def add(self, request, pk): """ --- method_path: /tvs/{id}/add/ method_action: POST desc: Adicionar grupo na tv. input: - name: group desc: Id do grupo. type: integer required: True location: form """ tv = self.get_object() group_pk, *_ = get_fields(request.data, ['group']) group = get_object_or_404(Group, pk=group_pk) tv.group = group tv.save() serializer = self.get_serializer(tv) Log.objects.create(user=request.user, action='update', content_object=tv) return Response(serializer.data)
def reset(self, request): uidb64, token, password = get_fields( request.data, ['uid', 'token', 'password']) try: uid = force_text(urlsafe_base64_decode(uidb64)) user = get_object_or_404(User, pk=uid) except(TypeError, ValueError, OverflowError): user = None if user is not None and account_activation_token.check_token(user, token): user.set_password(password) user.save() return Response({'message': 'Senha alterada com sucesso'}) return Response({'message': 'Token ou uid inválido'}, status=status.HTTP_400_BAD_REQUEST)
def reset(self, request): """ --- method_path: /accounts/reset/ method_action: POST desc: Alterar senha. input: - name: uid desc: Uid do usuário. type: str required: True location: form - name: token desc: token do usuário. type: str required: True location: form - name: password desc: Nova senha do usuário. type: str required: True location: form """ uidb64, token, password = get_fields(request.data, ['uid', 'token', 'password']) try: uid = force_text(urlsafe_base64_decode(uidb64)) user = get_object_or_404(User, pk=uid) except (TypeError, ValueError, OverflowError): user = None if user is not None and account_activation_token.check_token( user, token): user.set_password(password) user.save() return Response({'message': 'Senha alterada com sucesso'}) return Response({'message': 'Token ou uid inválido'}, status=status.HTTP_400_BAD_REQUEST)
def compra(self, request, pk): if request.user.is_authenticated: messages = [] error = [] cliente = Cliente.objects.get(user=request.user) created = False endereco_pk, *_ = get_fields(request.data, ['endereco']) endereco = cliente.enderecos.get(pk=endereco_pk) if cliente.carrinho.itens_carrinho.count(): venda = cliente.carrinho.to_venda() venda.endereco_entrega = endereco venda.save() cliente.carrinho.itens_carrinho.all().delete() cliente.carrinho.atualizar_valor() serializer = VendaSerializer(venda) data = serializer.data data['messages'] = messages data['error'] = error return Response(data) else: data = {'detail': 'O carrinho está vazio'} return Response(data, status=status.HTTP_400_BAD_REQUEST) else: raise NotAuthenticated