Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Fix add branch and restaurant
Browse files Browse the repository at this point in the history
  • Loading branch information
haneeva committed May 22, 2024
1 parent 0f4fdef commit 094153a
Show file tree
Hide file tree
Showing 47 changed files with 791 additions and 751 deletions.
10 changes: 5 additions & 5 deletions branches/admin.py
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)
34 changes: 26 additions & 8 deletions branches/form.py
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
)
39 changes: 21 additions & 18 deletions branches/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 5.0.4 on 2024-05-18 11:10
# Generated by Django 5.0.4 on 2024-05-22 13:18

import django.db.models.deletion
from django.db import migrations, models


Expand All @@ -11,29 +12,31 @@ class Migration(migrations.Migration):
]

operations = [
migrations.CreateModel(
name='Day',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('day_of_week', models.CharField(choices=[('Monday', 'Monday'), ('Tuesday', 'Tuesday'), ('Wednesday', 'Wednesday'), (
'Thursday', 'Thursday'), ('Friday', 'Friday'), ('Saturday', 'Saturday'), ('Sunday', 'Sunday')], max_length=9, unique=True)),
],
),
migrations.CreateModel(
name='Branch',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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(blank=True,
null=True, upload_to='media/restaurants/')),
('opening_time', models.TimeField(blank=True, null=True)),
('closing_time', models.TimeField(blank=True, null=True)),
('day', models.ManyToManyField(
related_name='branches', to='branches.day')),
('branch_image', models.ImageField(blank=True, null=True, upload_to='branches')),
('created_at', models.DateTimeField(auto_now_add=True)),
('modified_at', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='OpeningHour',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('day', models.CharField(choices=[('MO', 'Monday'), ('TU', 'Tuesday'), ('WE', 'Wednesday'), ('TH', 'Thursday'), ('FR', 'Friday'), ('SA', 'Saturday'), ('SU', 'Sunday'), ('CL', 'Closed')], max_length=2)),
('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)),
('branch', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='opening_hours', to='branches.branch')),
],
options={
'unique_together': {('branch', 'day')},
},
),
]
19 changes: 0 additions & 19 deletions branches/migrations/0002_alter_branch_day.py

This file was deleted.

19 changes: 0 additions & 19 deletions branches/migrations/0003_alter_branch_day.py

This file was deleted.

18 changes: 0 additions & 18 deletions branches/migrations/0004_alter_branch_day.py

This file was deleted.

18 changes: 0 additions & 18 deletions branches/migrations/0005_alter_branch_branch_image.py

This file was deleted.

18 changes: 0 additions & 18 deletions branches/migrations/0006_alter_branch_branch_image.py

This file was deleted.

74 changes: 49 additions & 25 deletions branches/models.py
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}"
5 changes: 5 additions & 0 deletions branches/static/css/branches.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.content-title {
text-align: center;
color: #cda45e;
padding: 20px;;
}
Loading

0 comments on commit 094153a

Please sign in to comment.