diff --git a/ipv6work/settings.py b/ipv6work/settings.py index c8260df..34196ff 100644 --- a/ipv6work/settings.py +++ b/ipv6work/settings.py @@ -39,6 +39,18 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', ] +# Libraries and other apps +INSTALLED_APPS += [ + 'crispy_forms', + + # Used for autocomplete and dynamic creation of tags. + # Ff this widget is required for admin, place these + # before 'django.contrib.admin' app + 'dal', + 'dal_select2', +] + +# Our apps INSTALLED_APPS += [ 'jobs', ] @@ -122,3 +134,6 @@ USE_TZ = True # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' + + +CRISPY_TEMPLATE_PACK = 'bootstrap4' diff --git a/jobs/autocomplete.py b/jobs/autocomplete.py new file mode 100644 index 0000000..f8e709e --- /dev/null +++ b/jobs/autocomplete.py @@ -0,0 +1,27 @@ +from dal import autocomplete + +from .models import Tag + + +class AuthenticatedUserCreateMixin(): + ''' By default, autocomplete-light only allows creation of + choices if the user has add permission on the model. However, + we need to allow any authenticated user to create tags + ''' + def has_add_permission(self, request): + print("i am called", request.user) + return request.user.is_authenticated + + +class TagAutocomplete( + AuthenticatedUserCreateMixin, autocomplete.Select2QuerySetView): + def get_queryset(self): + if not self.request.user.is_authenticated: + return Tag.objects.none() + + qs = Tag.objects.all() + + if self.q: + qs = qs.filter(name__icontains=self.q) + + return qs \ No newline at end of file diff --git a/jobs/forms.py b/jobs/forms.py new file mode 100644 index 0000000..eaff056 --- /dev/null +++ b/jobs/forms.py @@ -0,0 +1,21 @@ +from django import forms +from crispy_forms.helper import FormHelper +from crispy_forms.layout import Submit +from dal.autocomplete import ModelSelect2Multiple + +from .models import Job + + +class JobForm(forms.ModelForm): + class Meta: + model = Job + fields = ('title', 'description', 'tags') + widgets = { + 'tags': ModelSelect2Multiple(url='jobs:tag-autocomplete') + } + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_method = 'post' + self.helper.add_input(Submit('submit', 'Post Job')) diff --git a/jobs/models.py b/jobs/models.py index 160c275..c56bbe8 100644 --- a/jobs/models.py +++ b/jobs/models.py @@ -9,6 +9,9 @@ User = get_user_model() class Tag(models.Model): name = models.CharField(max_length=255, unique=True, db_index=True) + class Meta: + ordering = ['name'] + def __str__(self): return self.name diff --git a/jobs/templates/base.html b/jobs/templates/base.html index 780fd50..5eafc96 100644 --- a/jobs/templates/base.html +++ b/jobs/templates/base.html @@ -18,7 +18,7 @@ Tags Jobs - Login + Post a job