Skip to content

Commit

Permalink
Fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zrthxn committed May 19, 2021
1 parent 549bd3f commit 81a3120
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 61 deletions.
94 changes: 61 additions & 33 deletions subscribers/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class Meta:
]
widgets = {
"email": forms.TextInput(attrs={"placeholder": "[email protected]"}),
"phone_number": forms.TextInput(attrs={"placeholder": "10-digit number (Optional)"}),
"phone_number": forms.TextInput(
attrs={"placeholder": "10-digit number (Optional)"}
),
"pincode": forms.TextInput(attrs={"placeholder": "110006"}),
"district_id": forms.Select,
}
Expand All @@ -38,55 +40,81 @@ def clean(self):
error = {}

if not cleaned_data.get("email") and not cleaned_data.get("phone_number"):
error.update({
"email": "Please enter either email or phone number.",
"phone_number": "Please enter either email or phone number.",
})
error.update(
{
"email": "Please enter either email or phone number.",
"phone_number": "Please enter either email or phone number.",
}
)

if cleaned_data.get("search_type") == SEARCH_TYPE_CHOICES[0][0] and not cleaned_data.get("pincode"):
error.update({
"pincode": "Please enter your pincode",
})
if cleaned_data.get("search_type") == SEARCH_TYPE_CHOICES[0][
0
] and not cleaned_data.get("pincode"):
error.update(
{
"pincode": "Please enter your pincode",
}
)

if cleaned_data.get("search_type") == SEARCH_TYPE_CHOICES[1][0]:
if not cleaned_data.get("state"):
error.update({
"state": "Please select your State",
})
error.update(
{
"state": "Please select your State",
}
)
if not cleaned_data.get("district_id"):
error.update({
"district_id": "Please select your district",
})
error.update(
{
"district_id": "Please select your district",
}
)

if cleaned_data.get("email") or cleaned_data.get("phone_number"):
email = cleaned_data.get("email")
phone_number = cleaned_data.get("phone_number")

if cleaned_data.get("pincode"):
if email:
if Subscriber.objects.filter(email=email, pincode=cleaned_data.get("pincode")).exists():
error.update({
"pincode": "You have already subscribed at this pincode.",
})
if Subscriber.objects.filter(
email=email, pincode=cleaned_data.get("pincode")
).exists():
error.update(
{
"pincode": "You have already subscribed at this pincode.",
}
)
elif phone_number:
if Subscriber.objects.filter(phone_number=phone_number, pincode=cleaned_data.get("pincode")).exists():
error.update({
"pincode": "You have already subscribed at this pincode.",
})
if Subscriber.objects.filter(
phone_number=phone_number, pincode=cleaned_data.get("pincode")
).exists():
error.update(
{
"pincode": "You have already subscribed at this pincode.",
}
)

if cleaned_data.get("district_id"):
if email:
if Subscriber.objects.filter(email=email, district_id=cleaned_data.get("district_id")).exists():
error.update({
"district_id": "You have already subscribed at this district.",
})
if Subscriber.objects.filter(
email=email, district_id=cleaned_data.get("district_id")
).exists():
error.update(
{
"district_id": "You have already subscribed at this district.",
}
)
elif phone_number:
if Subscriber.objects.filter(phone_number=phone_number, district_id=cleaned_data.get("district_id")).exists():
error.update({
"pincode": "You have already subscribed at this district.",
})


if Subscriber.objects.filter(
phone_number=phone_number,
district_id=cleaned_data.get("district_id"),
).exists():
error.update(
{
"district_id": "You have already subscribed at this district.",
}
)

if cleaned_data.get("pincode") and cleaned_data.get("district_id"):
cleaned_data.pop("district_id")

Expand Down
17 changes: 12 additions & 5 deletions subscribers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ class Subscriber(models.Model):
validators=[MinValueValidator(100000), MaxValueValidator(999999)],
blank=True,
null=True,
help_text = "Don't know your pincode? Use 'By District'"
help_text="Don't know your pincode? Use 'By District'",
)
district_id = models.IntegerField(
"district",
blank=True,
null=True,
help_text="Districts are based on voting districts",
)
district_id = models.IntegerField("district", blank=True, null=True, help_text = "Districts are based on voting districts")
age_limit = models.IntegerField(
"age limit",
help_text = "Select your age category",
help_text="Select your age category",
choices=AGE_CATEGORY,
default=AGE_CATEGORY[0][0],
null=False,
Expand All @@ -55,8 +60,10 @@ class Subscriber(models.Model):

def __str__(self):
s = []
if self.email: s.append(str(self.email))
if self.phone_number: s.append(str(self.phone_number))
if self.email:
s.append(str(self.email))
if self.phone_number:
s.append(str(self.phone_number))

return " ".join(s)

Expand Down
5 changes: 4 additions & 1 deletion subscribers/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def setUp(self):
phone_number="9999999999", district_id=1
)
self.subscriber3 = Subscriber.objects.create(
email="[email protected]", phone_number="9999999999", pincode=222222, district_id=2
email="[email protected]",
phone_number="9999999999",
pincode=222222,
district_id=2,
)

self.test_centers = [
Expand Down

This file was deleted.

0 comments on commit 81a3120

Please sign in to comment.