from django import forms from manage_events.models import EventMaster, Event, EventCategory, Venue from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ class EventCategoryForm(forms.ModelForm): # image = forms.ImageField() class Meta: model = EventCategory fields = ["title", "image", "description", "video_url"] widgets = { "image": forms.FileInput(attrs={"class": "form-control-file"}), } class EventForm(forms.ModelForm): class Meta: model = Event fields = [ "title", # "event_master", "description", "image", "status", "start_date", "end_date", "from_time", "to_time", "category", "venue", "venue_capacity", # "video_url", "entry_type", "entry_fee", "key_guest", "age_group", "tags", "draft", "active", "deleted", ] widgets = { "title": forms.TextInput(attrs={"class": "form-control"}), "description": forms.Textarea(attrs={"class": "form-control", "rows": 4}), "status": forms.Select(attrs={"class": "form-control"}), "start_date": forms.DateInput( attrs={"class": "form-control", "type": "date"} ), "end_date": forms.DateInput( attrs={"class": "form-control", "type": "date"} ), "from_time": forms.TimeInput( attrs={"class": "form-control", "type": "time"} ), "to_time": forms.TimeInput(attrs={"class": "form-control", "type": "time"}), "venue_capacity": forms.NumberInput(attrs={"class": "form-control"}), "video_url": forms.URLInput(attrs={"class": "form-control"}), "entry_type": forms.Select(attrs={"class": "form-control"}), "entry_fee": forms.NumberInput(attrs={"class": "form-control"}), "key_guest": forms.Textarea(attrs={"class": "form-control", "rows": 3}), "age_group": forms.TextInput(attrs={"class": "form-control"}), "draft": forms.CheckboxInput(attrs={"class": "form-check-input"}), # For the 'image' field, you might not need to specify a widget since the default is appropriate. # However, if you want to add specific classes or attributes, you can do it like this: "image": forms.FileInput(attrs={"class": "form-control-file"}), # For ForeignKey fields like 'EventMaster' and 'venue', Django uses a select widget by default. # You can customize it further if needed: # "event_master": forms.Select(attrs={"class": "form-control"}), "venue": forms.Select(attrs={"class": "form-control"}), "category": forms.Select(attrs={"class": "form-control"}), } def clean(self): cleaned_data = super().clean() # Get the start and end dates from cleaned_data start_date = cleaned_data.get("start_date") end_date = cleaned_data.get("end_date") # Validation 1: end_date should not be less than start_date if end_date and start_date and end_date < start_date: self.add_error("end_date", _("End date cannot be before the start date.")) # Get the from and to times from cleaned_data from_time = cleaned_data.get("from_time") to_time = cleaned_data.get("to_time") # Validation 2: to_time should not be less than or equal to from_time if to_time and from_time and to_time <= from_time: self.add_error("to_time", _("End time must be after the start time.")) return cleaned_data class EventMasterForm(forms.ModelForm): class Meta: model = EventMaster fields = "__all__" # Includes all fields from the model class VenueForm(forms.ModelForm): class Meta: model = Venue fields = [ "title", "description", "address", "image", "url", "latitude", "longitude", ] widgets = { "title": forms.TextInput(attrs={"class": "form-control"}), "description": forms.Textarea(attrs={"class": "form-control", "rows": 4}), "address": forms.Textarea(attrs={"class": "form-control", "rows": 3}), "image": forms.FileInput(attrs={"class": "form-control"}), "url": forms.URLInput(attrs={"class": "form-control"}), "latitude": forms.NumberInput(attrs={"class": "form-control"}), "longitude": forms.NumberInput(attrs={"class": "form-control"}), }