- Increase max_length from 100 to 255 for first_name and last_name encrypted fields - Increase phone field max_length from 20 to 255 to accommodate encryption overhead - Add 'id' field to AppointmentRequest admin list_display for easier reference - Remove redundant docstring from _convert_to_datetime method The increased field lengths ensure adequate storage for encrypted data, which typically requires more space than plaintext values.
87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
from django import forms
|
|
from django.contrib import admin
|
|
from .models import AdminWeeklyAvailability, AppointmentRequest
|
|
|
|
class AdminWeeklyAvailabilityForm(forms.ModelForm):
|
|
class Meta:
|
|
model = AdminWeeklyAvailability
|
|
fields = '__all__'
|
|
widgets = {
|
|
'availability_schedule': forms.HiddenInput()
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
for day_num, day_name in AdminWeeklyAvailability.DAYS_OF_WEEK:
|
|
field_name = f'day_{day_num}'
|
|
self.fields[field_name] = forms.MultipleChoiceField(
|
|
choices=AdminWeeklyAvailability.TIME_SLOT_CHOICES,
|
|
required=False,
|
|
label=day_name,
|
|
widget=forms.CheckboxSelectMultiple
|
|
)
|
|
|
|
if self.instance.availability_schedule:
|
|
self.fields[field_name].initial = self.instance.availability_schedule.get(str(day_num), [])
|
|
|
|
def save(self, commit=True):
|
|
instance = super().save(commit=False)
|
|
|
|
availability_schedule = {}
|
|
for day_num, day_name in AdminWeeklyAvailability.DAYS_OF_WEEK:
|
|
field_name = f'day_{day_num}'
|
|
time_slots = self.cleaned_data.get(field_name, [])
|
|
if time_slots:
|
|
availability_schedule[str(day_num)] = time_slots
|
|
|
|
instance.availability_schedule = availability_schedule
|
|
|
|
if commit:
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
@admin.register(AdminWeeklyAvailability)
|
|
class AdminWeeklyAvailabilityAdmin(admin.ModelAdmin):
|
|
form = AdminWeeklyAvailabilityForm
|
|
list_display = ['__str__', 'created_at', 'updated_at']
|
|
|
|
def has_add_permission(self, request):
|
|
if self.model.objects.count() >= 1:
|
|
return False
|
|
return super().has_add_permission(request)
|
|
|
|
@admin.register(AppointmentRequest)
|
|
class AppointmentRequestAdmin(admin.ModelAdmin):
|
|
list_display = ['id','full_name', 'email', 'status', 'formatted_created_at', 'formatted_scheduled_datetime']
|
|
list_filter = ['status', 'created_at', 'scheduled_datetime']
|
|
search_fields = ['first_name', 'last_name', 'email']
|
|
readonly_fields = ['id', 'created_at', 'updated_at', 'formatted_created_at', 'formatted_scheduled_datetime']
|
|
|
|
fieldsets = (
|
|
('Personal Information', {
|
|
'fields': ('first_name', 'last_name', 'email', 'phone', 'reason')
|
|
}),
|
|
('Appointment Preferences', {
|
|
'fields': ('preferred_dates', 'preferred_time_slots')
|
|
}),
|
|
('Scheduling', {
|
|
'fields': ('status', 'scheduled_datetime', 'scheduled_duration', 'rejection_reason')
|
|
}),
|
|
('Video Meeting', {
|
|
'fields': ('jitsi_meet_url', 'jitsi_room_id')
|
|
}),
|
|
('Metadata', {
|
|
'fields': ('id', 'created_at', 'updated_at', 'formatted_created_at', 'formatted_scheduled_datetime'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
|
|
def formatted_created_at(self, obj):
|
|
return obj.formatted_created_at
|
|
formatted_created_at.short_description = 'Created At'
|
|
|
|
def formatted_scheduled_datetime(self, obj):
|
|
return obj.formatted_scheduled_datetime
|
|
formatted_scheduled_datetime.short_description = 'Scheduled Date Time' |