Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update functions with doc strings #49

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ class Event(models.Model):
)

def clean_start_date(self):
"""
Validate the start date if it is in the past

Raises:
ValidationError: If the start date is in the past

Returns:
datetime: The valid start date
"""
date_now = datetime.now().date() # Convert to date

if self.start_date.date() < date_now: # Convert to date for comparison
Expand All @@ -85,6 +94,15 @@ def clean_start_date(self):
return self.start_date

def clean_end_date(self):
"""
Validate the end date if it is in the past

Raises:
ValidationError: If the end date is in the past

Returns:
datetime: The valid end date
"""
date_now = datetime.now().date() # Convert to date

if self.end_date.date() < date_now: # Convert to date for comparison
Expand All @@ -93,6 +111,15 @@ def clean_end_date(self):
return self.end_date

def clean_registration_deadline(self):
"""
Validate the end date if it is in the past

Raises:
ValidationError: If the end date is in the past

Returns:
datetime: The valid end date
"""
date_now = datetime.now().date() # Convert to date

if (
Expand All @@ -103,27 +130,44 @@ def clean_registration_deadline(self):
return self.registration_deadline

def clean(self):
"""
Validate if the values are righly related to each other according
to business later.

Raises:
ValidationError:
- If start_date is greater than end date
- If Registration deadline is greater than start_date

Returns:
None


"""
super().clean() # Ensure parent class's clean method is called
date_now = datetime.now()

# Check if start_date is greater than end_date
if self.start_date > self.end_date:
raise ValidationError("Start date must be less than end date.")

if self.registration_deadline:

if self.registration_deadline.date() < date_now.date():
raise ValidationError(
"Registration deadline cannot be in past"
)

# Check if registration_deadline is greater than start_date
if self.registration_deadline.date() > self.start_date:
raise ValidationError(
"Registration deadline must be less than start date."
)

def save(self, *args, **kwargs):
"""
We are overriding events save model
so that we can create slug for the model once
created.

The slug is not changeable,
if slug is changed, URL will be changed as well
so for that reason, the slug will not be changed
"""
if not self.slug:
self.slug = slugify(self.title) + "-" + str(uuid4())[0:8]
super().save(*args, **kwargs)
Expand Down
11 changes: 11 additions & 0 deletions event/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class EventSerializer(serializers.ModelSerializer):
images = serializers.SerializerMethodField(read_only=True)

def get_images(self, obj):
"""
Gets the published images of the event

Args:
obj(Event): The event object
Returns:
list{dict]: A list of dictonaries, each containing
- 'caption' (str): The caption of the image.
- 'images' (str): The URL of image.
- 'created_at' (datetime): The timestamp of image creation
"""
images = []
for img in obj.event_images.filter(published=True).order_by(
"position"
Expand Down
18 changes: 18 additions & 0 deletions newsletter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ class NewsletterCreateView(CreateAPIView):

# View for unsubscribing using the unique unsubscribe token
class UnsubscribeView(UpdateAPIView):

queryset = NewsletterSubscriber.objects.all()
serializer_class = NewsletterSubscriberSerializer
lookup_field = "unsubscribe_token"

def update(self, request, *args, **kwargs):
"""
Deactivate the newsletter subscription for the subscriber.

This method retrieves the subscriber using the
`unsubscribe_token`, marks the subscriber
as inactive, and saves the
updated state in the database.

Args:
request (Request): The HTTP request object containing
data from the client.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.

Returns:
Response: A JSON response with a success message.
"""
subscriber = self.get_object()
subscriber.is_active = False
subscriber.save()
Expand Down
Loading