diff options
Diffstat (limited to 'workshop_app')
-rw-r--r-- | workshop_app/forms.py | 13 | ||||
-rw-r--r-- | workshop_app/models.py | 2 | ||||
-rw-r--r-- | workshop_app/templates/workshop_app/edit_workshop_type.html | 102 | ||||
-rw-r--r-- | workshop_app/urls.py | 2 | ||||
-rw-r--r-- | workshop_app/views.py | 36 |
5 files changed, 150 insertions, 5 deletions
diff --git a/workshop_app/forms.py b/workshop_app/forms.py index fcb34c4..4bef28c 100644 --- a/workshop_app/forms.py +++ b/workshop_app/forms.py @@ -1,9 +1,11 @@ from string import punctuation, digits from django import forms +from django.forms import inlineformset_factory from django.utils import timezone -from .models import (Profile, Workshop, ProfileComments, department_choices, title, source, states, WorkshopType) +from .models import (Profile, Workshop, ProfileComments, department_choices, title, source, states, WorkshopType, + AttachmentFile) try: from string import letters @@ -202,3 +204,12 @@ class WorkshopTypeForm(forms.ModelForm): class Meta: model = WorkshopType exclude = [] + + +class AttachmentFileForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super(AttachmentFileForm, self).__init__(*args, **kwargs) + + class Meta: + model = AttachmentFile + exclude = ['workshop_type'] diff --git a/workshop_app/models.py b/workshop_app/models.py index b35d9a7..b43600f 100644 --- a/workshop_app/models.py +++ b/workshop_app/models.py @@ -145,7 +145,7 @@ class WorkshopType(models.Model): class AttachmentFile(models.Model): - attachments = models.FileField(upload_to=attachments, blank=True, + attachments = models.FileField(upload_to=attachments, blank=False, help_text='Please upload workshop documents one by one, \ ie.workshop schedule, instructions etc. \ Please Note: Name of Schedule file should be similar to \ diff --git a/workshop_app/templates/workshop_app/edit_workshop_type.html b/workshop_app/templates/workshop_app/edit_workshop_type.html new file mode 100644 index 0000000..9c883d7 --- /dev/null +++ b/workshop_app/templates/workshop_app/edit_workshop_type.html @@ -0,0 +1,102 @@ +{% extends 'workshop_app/base.html' %} + +{% block title %} + Add new Workshop type +{% endblock %} + +{% block extra-dependencies %} +{% endblock %} + +{% block extra-custom-scripts %} + + <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.formset/1.2.2/jquery.formset.js"></script> +{% endblock %} + +{% block content %} + <div class="container"> + <h2 class="text-center m-2">View / Edit Workshop Type</h2> + <br> + <form method="post" enctype="multipart/form-data"> + <div class="row my-3"> + <div class="col-3 font-weight-bold">Workshop Name :</div> + <div class="col text-left"> + <div class="row"> + <div class="col">{{ form.name }}</div> + </div> + <div class="row m-1 text-danger"> + <div class="col">{{ form.name.errors }}</div> + </div> + </div> + + </div> + <div class="row my-3"> + <div class="col-3 font-weight-bold">Duration :</div> + <div class="col text-left"> + <div class="row"> + <div class="col">{{ form.duration }}</div> + </div> + <div class="row m-1 text-muted"> + <div class="col">{{ form.duration.help_text }}</div> + </div> + <div class="row m-1 text-danger"> + <div class="col">{{ form.duration.errors }}</div> + </div> + </div> + </div> + <div class="row my-3"> + <div class="col-3 font-weight-bold">Description :</div> + <div class="col text-left"> + <div class="row"> + <div class="col">{{ form.description }}</div> + </div> + <div class="row m-1 text-danger"> + <div class="col">{{ form.description.errors }}</div> + </div> + </div> + </div> + <div class="row my-3"> + <div class="col-3 font-weight-bold">Terms and Conditions :</div> + <div class="col text-left"> + <div class="row"> + <div class="col">{{ form.terms_and_conditions }}</div> + </div> + <div class="row m-1 text-danger"> + <div class="col">{{ form.terms_and_conditions.errors }}</div> + </div> + </div> + </div> + + <h2 class="text-center m-2">Attachments</h2> + {{ form_file.management_form }} + {{ form_file.non_form_errors }} + {% for form in form_file %} + <div class="inline {{ form_file.prefix }} my-4"> + {% for field in form %} + <div class="row"> + <div class="col-6">{{ field }}</div> + </div> + {% endfor %} + </div> + <hr> + {% endfor %} + <div class="row my-3"> + <div class="col-12"> + <button type="submit" class="btn btn-md btn-primary float-right m-3">Save</button> + </div> + </div> + </form> + </div> + <br><br><br><br> + <script type="text/javascript"> + $(function () { + $(".inline.{{ form_file.prefix }}").formset({ + prefix: "{{ form_file.prefix }}", // The form prefix for your django formset + addCssClass: "btn btn-md btn-primary bordered inline-form-add", // CSS class applied to the add link + deleteCssClass: "hidden", // CSS class applied to the delete link + addText: 'Add another attachment', // Text for the add link + deleteText: '', + formCssClass: 'inline-form' // CSS class applied to each form in a formset + }) + }); + </script> +{% endblock %}
\ No newline at end of file diff --git a/workshop_app/urls.py b/workshop_app/urls.py index 76a0be6..37b56d5 100644 --- a/workshop_app/urls.py +++ b/workshop_app/urls.py @@ -31,7 +31,7 @@ urlpatterns = [ url(r'^edit_profile/$', views.edit_profile), url(r'^my_workshops/$', views.my_workshops), url(r'^propose_workshop/$', views.propose_workshop), - url(r'^workshop_types/$', views.workshop_type_list), + url(r'^workshop_types/$', views.workshop_type_list, name='workshop_type_list'), url(r'^workshop_type_details/([1-9][0-9]*)$', views.workshop_type_details, name='workshop_type_details'), url(r'^add_workshop_type$', views.add_workshop_type, name='add_workshop_type'), url(r'^view_profile/([1-9][0-9]*)$', views.view_comment_profile), diff --git a/workshop_app/views.py b/workshop_app/views.py index d71f066..0f68cfb 100644 --- a/workshop_app/views.py +++ b/workshop_app/views.py @@ -1,3 +1,4 @@ +from django.forms import inlineformset_factory, model_to_dict from django.urls import reverse try: @@ -19,7 +20,7 @@ from .forms import ( from .models import ( Profile, User, Workshop, - WorkshopType + WorkshopType, AttachmentFile ) from .send_mails import send_email @@ -340,13 +341,44 @@ def propose_workshop(request): ) +@login_required def workshop_type_details(request, workshop_type_id): """Gives the types of workshop details """ user = request.user if user.is_superuser: return redirect("/admin") - workshop_type = WorkshopType.objects.get(id=workshop_type_id) + workshop_type = WorkshopType.objects.filter(id=workshop_type_id) + if workshop_type.exists(): + workshop_type = workshop_type.first() + else: + return redirect(reverse('workshop_type_list')) + + qs = AttachmentFile.objects.filter(workshop_type=workshop_type) + AttachmentFileFormSet = inlineformset_factory(WorkshopType, AttachmentFile, fields=['attachments'], + can_delete=False, extra=(qs.count() + 1)) + + if is_instructor(user): + if request.method == 'POST': + form = WorkshopTypeForm(request.POST, instance=workshop_type) + form_file = AttachmentFileFormSet(request.POST, request.FILES, instance=form.instance) + if form.is_valid(): + form_data = form.save() + for file in form_file: + if file.is_valid() and file.clean() and file.clean()['attachments']: + if file.cleaned_data['id']: + file.cleaned_data['id'].delete() + file.save() + return redirect(reverse('workshop_type_details', args=[form_data.id])) + else: + print(request.POST) + print(form_file.errors) + else: + form = WorkshopTypeForm(instance=workshop_type) + form_file = AttachmentFileFormSet() + for subform, data in zip(form_file, qs): + subform.initial = model_to_dict(data) + return render(request, 'workshop_app/edit_workshop_type.html', {'form': form, 'form_file': form_file}) return render( request, 'workshop_app/workshop_type_details.html', {'workshop_type': workshop_type} |