This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
791 additions
and
751 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
from django.contrib import admin | ||
from .models import Branch, Day | ||
from .models import Branch, OpeningHour | ||
|
||
|
||
# Register your models here. | ||
admin.site.register(Day) | ||
class OpeningHourInline(admin.TabularInline): | ||
model = OpeningHour | ||
extra = 1 | ||
|
||
|
||
class BranchAdmin(admin.ModelAdmin): | ||
list_display = ('branch_name', 'branch_address', | ||
'branch_contact', 'opening_time', 'closing_time') | ||
inlines = [OpeningHourInline] | ||
|
||
|
||
admin.site.register(Branch, BranchAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
from django import forms | ||
from .models import Day, Branch | ||
from .models import Branch, OpeningHour | ||
from django.forms.models import modelformset_factory | ||
|
||
|
||
class AddBranchForm(forms.ModelForm): | ||
class BranchForm(forms.ModelForm): | ||
class Meta: | ||
model = Branch | ||
fields = ('branch_name', 'branch_address', 'branch_contact', | ||
'branch_image', 'day', 'opening_time', 'closing_time') | ||
fields = ['branch_name', 'branch_address', | ||
'branch_contact', 'branch_image'] | ||
widgets = { | ||
'branch_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter branch name'}), | ||
'branch_address': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter branch address'}), | ||
'branch_contact': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter branch contact'}), | ||
'branch_image': forms.FileInput(attrs={'class': 'form-control'}), | ||
'opening_time': forms.TimeInput(attrs={'type': 'time'}), | ||
'closing_time': forms.TimeInput(attrs={'type': 'time'}), | ||
'day': forms.CheckboxSelectMultiple(), | ||
'branch_image': forms.ClearableFileInput(attrs={'class': 'form-control-file'}), | ||
} | ||
|
||
|
||
class OpeningHourForm(forms.ModelForm): | ||
class Meta: | ||
model = OpeningHour | ||
exclude = ['id'] | ||
fields = ['day', 'open_time', 'close_time'] | ||
widgets = { | ||
'day': forms.Select(attrs={'class': 'form-select'}), | ||
'open_time': forms.TimeInput(attrs={'class': 'form-control', 'type': 'time'}), | ||
'close_time': forms.TimeInput(attrs={'class': 'form-control', 'type': 'time'}), | ||
} | ||
|
||
|
||
OpeningHourFormSet = modelformset_factory( | ||
OpeningHour, | ||
fields=('day', 'open_time', 'close_time'), | ||
extra=1, # Number of extra empty forms to display | ||
can_delete=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,61 @@ | ||
import os | ||
from django.db import models | ||
|
||
|
||
# Create your models here. | ||
class Day(models.Model): | ||
DAY_CHOICES = [ | ||
('Monday', 'Monday'), | ||
('Tuesday', 'Tuesday'), | ||
('Wednesday', 'Wednesday'), | ||
('Thursday', 'Thursday'), | ||
('Friday', 'Friday'), | ||
('Saturday', 'Saturday'), | ||
('Sunday', 'Sunday'), | ||
] | ||
day_of_week = models.CharField( | ||
max_length=9, choices=DAY_CHOICES, unique=True) | ||
|
||
def __str__(self): | ||
return self.day_of_week | ||
|
||
|
||
class Branch(models.Model): | ||
branch_name = models.CharField(max_length=150) | ||
branch_address = models.CharField(max_length=200) | ||
branch_contact = models.CharField(max_length=150) | ||
branch_image = models.ImageField( | ||
upload_to='branches', null=True, blank=True) | ||
opening_time = models.TimeField(null=True, blank=True) | ||
closing_time = models.TimeField(null=True, blank=True) | ||
day = models.ManyToManyField( | ||
Day, related_name='branches', blank=True) | ||
|
||
def is_closed(self): | ||
return self.opening_time is None and self.closing_time is None | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
modified_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return self.branch_name | ||
|
||
def delete(self, *args, **kwargs): | ||
# If there's an associated image, delete it | ||
if self.branch_image and os.path.isfile(self.branch_image.path): | ||
os.remove(self.branch_image.path) | ||
super().delete(*args, **kwargs) | ||
|
||
def save(self, *args, **kwargs): | ||
# If updating an instance, delete the old image | ||
if self.pk: | ||
try: | ||
old_instance = Branch.objects.get(pk=self.pk) | ||
if old_instance.branch_image and old_instance.branch_image != self.branch_image: | ||
if os.path.isfile(old_instance.branch_image.path): | ||
os.remove(old_instance.branch_image.path) | ||
except Branch.DoesNotExist: | ||
pass | ||
super().save(*args, **kwargs) | ||
|
||
|
||
class OpeningHour(models.Model): | ||
DAYS_OF_WEEK = [ | ||
('MO', 'Monday'), | ||
('TU', 'Tuesday'), | ||
('WE', 'Wednesday'), | ||
('TH', 'Thursday'), | ||
('FR', 'Friday'), | ||
('SA', 'Saturday'), | ||
('SU', 'Sunday'), | ||
('CL', 'Closed'), | ||
] | ||
|
||
branch = models.ForeignKey( | ||
Branch, related_name='opening_hours', on_delete=models.CASCADE) | ||
day = models.CharField(max_length=2, choices=DAYS_OF_WEEK) | ||
open_time = models.TimeField(default='00:00') | ||
close_time = models.TimeField(default='23:59') | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
modified_at = models.DateTimeField(auto_now=True) | ||
|
||
class Meta: | ||
# Ensure no duplicate days for the same branch | ||
unique_together = ('branch', 'day') | ||
|
||
def __str__(self): | ||
return f"{self.branch.branch_name} - {dict(self.DAYS_OF_WEEK).get(self.day)}: {self.open_time} to {self.close_time}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.content-title { | ||
text-align: center; | ||
color: #cda45e; | ||
padding: 20px;; | ||
} |
Oops, something went wrong.