Skip to content

Commit

Permalink
fix: email validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa committed Apr 6, 2023
1 parent d3ce855 commit ef71c3a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Meta:
class VoteSerializer(serializers.Serializer):
choice_id = serializers.IntegerField()
email = serializers.EmailField()
poll_id = serializers.IntegerField()

def validate_choice_id(self, value):
try:
Expand All @@ -35,8 +36,9 @@ def validate_choice_id(self, value):

def validate_email(self, value):
try:
poll_id = self.initial_data['poll_id']
Vote.objects.get(
email=value, choice__poll=self.context['view'].kwargs['pk'])
email=value, choice__poll=poll_id)
except Vote.DoesNotExist:
pass
else:
Expand Down
1 change: 0 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# polls/urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import PollViewSet, VoteViewSet
Expand Down
8 changes: 6 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from .models import Poll, Choice, Vote
from .serializers import PollSerializer, VoteSerializer
from django.core.mail import send_mail
from django.utils import timezone
import environ


env = environ.Env()
environ.Env.read_env()


class PollViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -39,7 +43,7 @@ def create(self, request):
send_mail(
'Your One-Time Password for Voting',
f'Your OTP is {vote.otp}. It will expire in 10 minutes.',
'[email protected]',
env('EMAIL_ADDRESS'),
[email],
fail_silently=False,
)
Expand Down
1 change: 1 addition & 0 deletions polls/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_USER = env('EMAIL_ADDRESS')
EMAIL_HOST_PASSWORD = env('EMAIL_PASSWORD')

0 comments on commit ef71c3a

Please sign in to comment.