Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512""" This file contains a collection of Forms, Fields, And Widgets that we use in form_base.html"""
import osfrom collections import Counterfrom uuid import UUID
from django.conf import settingsfrom django.contrib.auth.password_validation import validate_password, ValidationError, \ get_password_validators, password_validators_help_textsfrom django.forms import Form, ModelForm, fields, PasswordInputfrom django.forms.widgets import DateInput, TimeInput, ClearableFileInput, TextInput
from edit import models
def get_size_of_folder(folder_path): """ Given the path of a folder, get its size
@param folder_path: The path to a folder @type folder_path: str @return: The size of the folder in bytes @rtype: int """
total_size = 0 for dir_path, dir_names, filenames in os.walk(folder_path): for f in filenames: fp = os.path.join(dir_path, f) if not os.path.islink(fp): total_size += os.path.getsize(fp) return total_size
MAX_BYTES = 900000000
def check_media_quota(): """ Checks to ensure we won't go over the max amount of bytes we can store
@return: whether we're close to going over the limit @rtype: bool """
return get_size_of_folder(settings.MEDIA_ROOT) <= MAX_BYTES
class TimeSelectorField(TimeInput): """ This class overrides the django TimeInput class and sets the html input type to "time" """
input_type = 'time'
class DateSelectorField(DateInput): """ This class overrides the django DateInput class and sets the html input type to "date" """
input_type = 'date'
class OrderInput(TextInput): """ This input is used to allow the user to re-order the arrangement of objects with drag&drop """
input_type = "hidden" template_name = "custom_widgets/OrderWidget.html" objects = [] display_name = ""
def get_context(self, name, value, attrs): """ Get the context and add our custom values to it
@return: New Context @rtype: dict """
context = super().get_context(name, value, attrs) context['widget']['objects'] = self.objects context['widget']['viewset_display_name'] = self.display_name context['widget']['currentOrder'] = ",".join(self.get_current_order()) return context
def get_current_order(self): """ Get the current order of objects
@return: a list of objects in the current order @rtype: list """
return [str(obj.id) for obj in self.objects]
class Media: """ This class defines additional css and js we want to use in the input """
css = { "all": ("admin/order.css",) } js = ("https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js", "https://cdn.jsdelivr.net/npm/jquery-sortablejs@latest/jquery-sortable.js", "admin/order.js")
class OrderField(fields.CharField): """ This field uses the OrderWidget """
widget = OrderInput()
def set_objects(self, objects): """ This sets the objects value of the widget
@param objects: a list of objects @type objects: list """
self.widget.objects = objects
def set_name(self, name): """ This sets the display name for the widget
@param name: The new name to display @type name: str """
self.widget.display_name = name
class PermInput(TextInput): """ This widget is used to allow the user to edit what other users can access and edit """
input_type = "hidden" template_name = "custom_widgets/PermissionWidget.html"
viewsets = []
def get_context(self, name, value, attrs): """ Get the context and add our custom values to it
@return: New Context @rtype: dict """
context = super().get_context(name, value, attrs) context['widget']['viewsets'] = self.viewsets return context
class Media: """ This class defines additional css and js we want to use in the input """
js = ("admin/permissionLogic.js",)
class PermField(fields.CharField): """ This field uses the permissions widget """
widget = PermInput
def set_viewsets(self, viewsets): """ This sets the viewsets the user can change permissions of
@param viewsets: A list of viewsets that we can change permissions for @type viewsets: list """
self.widget.viewsets = viewsets
class ConfirmWidget(TextInput): """ This is a simple widget that asks the user if they're sure they want to delete an object """
input_type = "hidden" template_name = "custom_widgets/ConfirmTextWidget.html"
def get_context(self, name, value, attrs): """ Get the context and add our custom values to it
@return: New Context @rtype: dict """
context = super().get_context(name, value, attrs) context['widget']['object_name'] = self.object_name return context
class ConfirmField(fields.CharField): """ This field uses the ConfirmWidget """
widget = ConfirmWidget
def set_object_name(self, object_name): """ Sets the objects name to display
@param object_name: The name of an object that's being deleted @type object_name: str """
self.widget.object_name = object_name
class PhotoField(ClearableFileInput): template_name = "custom_widgets/PhotoInput.html"
class LinkForm(ModelForm): """ This form handles editing and adding links in the db """
class Meta: model = models.ExternalLink fields = ['url', 'display_name']
class SocialForm(ModelForm): """ This form handles editing and adding Social Media Pages to the db """
class Meta: model = models.Social exclude = ['id', 'sort_order']
class PhotoForm(ModelForm): """ This form handles editing and adding Gallery Photos to the db """
def __init__(self, *args, **kargs): """ Set the help text on the caption field to explain its purpose """
super().__init__(*args, **kargs) self.fields['caption'].help_text = "This is required for accessibility," \ " please provide a description of the picture"
class Meta: model = models.GalleryPhoto fields = ['picture', 'caption', 'featured'] widgets = { 'picture': PhotoField }
def clean(self): """ This function ensures two things: 1. Only 6 featured photos are active at one time 2. We have enough space to save the picture """
max_featured_photos = 6 cleaned_data = super().clean() featured = cleaned_data.get("featured")
if not check_media_quota(): self.add_error("picture", "There is not enough space to upload this picture," " please delete some older pictures to free up space")
if featured is not None: currently_featured = len(list(models.GalleryPhoto.objects.filter(featured=True))) if featured and currently_featured >= max_featured_photos: msg = f"There are already {max_featured_photos} featured photos" self.add_error("featured", msg)
class OfficerForm(ModelForm): """ This form handles adding and editing Officers to the db """
class Meta: model = models.Officer exclude = ['id', 'sort_order', "width", "height"] widgets = { "picture": PhotoField }
def __init__(self, *args, **kargs): """ Set the biography field's textarea to have a more suitable row and column count """
super().__init__(*args, **kargs) self.fields["biography"].widget.attrs.update(rows="4", cols="25")
class EventForm(ModelForm): """ This form handles editing and adding events to the db """
class Meta: model = models.Event exclude = ['id'] widgets = { 'startTime': TimeSelectorField, 'endTime': TimeSelectorField, 'startDate': DateSelectorField, 'endDate': DateSelectorField }
class Media: """ This class defines extra css and js to include in the form """
js = ("admin/event_form.js",)
def __init__(self, *args, **kargs): """ Sets label and help texts to more suitable values It also updates the description field to have different width/col """
super().__init__(*args, **kargs) self.fields["virtual"].help_text = "Determines Whether We Should Display A Link Or A Location" self.fields["description"].widget.attrs.update(rows="4", cols="25") self.fields['startDate'].label = "Start Date" self.fields['endDate'].label = "End Date" self.fields['startTime'].label = "Start Time" self.fields['endTime'].label = "End Time"
class OrderForm(Form): """ This form is used to change the sort order of objects (like Links or Social Media Pages) """
new_order = OrderField()
def __init__(self, *args, **kargs): """ Sets the help text to explain how to use this form """
super().__init__(*args, **kargs) self.fields["new_order"].help_text = 'Click and drag the handle (<i class="fas fa-grip-lines"></i>)' \ ' on an item to move it.'
def clean(self): """ If the user uses the form, this should never fail, however, if the user uses another way to send the POST data We check to make sure everything is valid for security """
cleaned_data = super().clean() new_order_raw = cleaned_data.get("new_order").split(",") if new_order_raw: try: new_order = [UUID(raw_id) for raw_id in new_order_raw] current_order = [UUID(raw_id) for raw_id in self.fields["new_order"].widget.get_current_order()] if Counter(new_order) != Counter(current_order): self.add_error("new_order", "Error Setting New Order (Counter)") except ValueError: self.add_error("new_order", "Error Setting New Order (ValueError)")
class UserCreateForm(ModelForm): """ This form is used to add a user to the db The main difference between this and the edit, is this one includes the password fields """
new_password = fields.CharField(widget=PasswordInput) confirm_new_password = fields.CharField(widget=PasswordInput) permissions = PermField()
class Meta: model = models.User fields = ["username", "first_name", "last_name", "email"]
def __init__(self, *args, **kargs): """ Show requirements for password, and change labels to more suitable names """
super().__init__(*args, **kargs) validators = list(settings.AUTH_PASSWORD_VALIDATORS) for ignored in settings.IGNORED_VALIDATORS_FOR_NEW_PASSWORD: validators.remove(ignored) new_config = get_password_validators(validators) rule_list = password_validators_help_texts(password_validators=new_config) self.fields['username'].help_text = None self.fields['first_name'].widget.attrs.update(required=True) self.fields['last_name'].widget.attrs.update(required=True) self.fields['email'].widget.attrs.update(required=True) self.fields["new_password"].widget.attrs.update(autocomplete="new-password") self.fields["new_password"].help_text = "<br/>".join(rule_list) self.fields["confirm_new_password"].widget.attrs.update(autocomplete="new-password")
def clean(self): """ This validates the password, and makes sure the confirm password matches """
cleaned_data = super().clean() new_password = cleaned_data.get("new_password", "") confirm_password = cleaned_data.get("confirm_new_password", "") if new_password != confirm_password: self.add_error("confirm_new_password", "Passwords don't match") try: validate_password(new_password, user=None) except ValidationError as ve: [self.add_error("new_password", error) for error in list(ve)]
class UserEditForm(ModelForm): """ This form is used to edit details about a user This form cannot change a user's password, we use another page for that """
permissions = PermField()
class Meta: model = models.User fields = ["username", "email", "first_name", "last_name"]
def __init__(self, *args, **kargs): """ Sets variables to more suitable values, makes some fields required """
super().__init__(*args, **kargs) self.fields['username'].help_text = None self.fields['first_name'].widget.attrs.update(required=True) self.fields['last_name'].widget.attrs.update(required=True) self.fields['email'].widget.attrs.update(required=True)
class SetUserPasswordForm(Form): """ This form is used to set a users password """
new_password = fields.CharField(widget=PasswordInput) confirm_new_password = fields.CharField(widget=PasswordInput) user = None
def __init__(self, *args, **kargs): """ Display the requirements for the new password """
super().__init__(*args, **kargs) self.fields["new_password"].label = "New Password" rule_list = password_validators_help_texts() self.fields["new_password"].help_text = "<br/>".join(rule_list) self.fields["confirm_new_password"].label = "Confirm New Password" self.fields["new_password"].widget.attrs.update(autocomplete="new-password") self.fields["confirm_new_password"].widget.attrs.update(autocomplete="new-password")
def set_user(self, user): """ Set the user whose password we want to change """
self.user = user
def clean(self): """ Validates the password and ensures that the passwords match """
cleaned_data = super().clean() new_password = cleaned_data.get("new_password", "") confirm_password = cleaned_data.get("confirm_new_password", "") if new_password != confirm_password: self.add_error("confirm_new_password", "Passwords don't match") try: validate_password(new_password, user=self.user) except ValidationError as ve: [self.add_error("new_password", error) for error in list(ve)]
class ConfirmDeleteForm(Form): """ This an extremely simple form that confirms a user wants to delete an object """
confirm = ConfirmField()