Commits

Luke Plant committed 1e20cc5

Customised Sermon interface so that when adding you just get an upload field

Other details are filled out automatically

  • Participants
  • Parent commits 6fc853d

Comments (0)

Files changed (2)

File sermons/admin.py

+from functools import partial
+
 from django.contrib import admin
+from django.forms.models import modelform_factory
 
 from sermons.models import Speaker, Topic, Series, Sermon
 
     date_hierarchy = 'date_delivered'
     list_per_page = 200
 
+    def get_form(self, request, obj=None, **kwargs):
+        if obj is None:
+            # For add view, we have a simplified form that just has an upload field.
+            return modelform_factory(self.model,
+                                     fields=["sermon"],
+                                     formfield_callback=partial(self.formfield_for_dbfield, request=request),)
+        else:
+            return super(SermonAdmin, self).get_form(request, obj=obj, **kwargs)
 
+    def save_model(self, request, obj, form, change):
+        if obj.pk is None:
+            from sermons.tags import set_attrs_from_filename
+            set_attrs_from_filename(obj)
+        obj.save()
 
 admin.site.register(Speaker, SpeakerAdmin)
 admin.site.register(Topic, TopicAdmin)

File sermons/templates/admin/sermons/sermon/change_form.html

+{% extends "admin/change_form.html" %}
+{% block after_related_objects %}
+{{ block.super }}
+<script type="text/javascript">
+(function($) {
+    $(document).ready(function() {
+       // Only want 'save and continue editing' if adding new.
+       // Also allow 'save' if editing existing.
+       // Editing can be detected by presence of #id_published
+       $('input[name="_addanother"]').hide();
+       if ($('#id_published').length == 0) {
+           $('input[name="_save"]').hide();
+       }
+
+    });
+})(django.jQuery);
+</script>
+{% endblock %}