From 6919546a0f74e32cd4454042e8772d246e1db88c Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sat, 26 May 2012 04:32:48 +0000 Subject: [PATCH] Media main parts complete svn: r19656 --- src/data/templates/view_media_detail.html | 62 ++++++++-- src/data/templates/view_note_detail.html | 18 +-- src/webapp/grampsdb/forms.py | 36 ++++++ src/webapp/grampsdb/models.py | 15 +-- src/webapp/grampsdb/view/media.py | 132 ++++++++-------------- 5 files changed, 155 insertions(+), 108 deletions(-) diff --git a/src/data/templates/view_media_detail.html b/src/data/templates/view_media_detail.html index 6307f761d..8e31bbb2d 100644 --- a/src/data/templates/view_media_detail.html +++ b/src/data/templates/view_media_detail.html @@ -10,20 +10,42 @@ }); +
+ {% include "detail_breadcrumb.html" %} - + +
+
+ + {% if mediaform.errors %} +
+

The following fields have errors. Please correct and try again.

+
{{mediaform.errors}}
+
+ {% endif %} +{% csrf_token %} - + + - + + + + - + + - + + + + + +
Event type: {{event.event_type|escape}}Date:{{mediaform.desc.label}}:{% render mediaform.desc user action %}
Description:{{event.description|escape}}{{mediaform.gramps_id.label}}:{% render mediaform.gramps_id user action %}{{mediaform.private.label}}:{% render mediaform.private user action %}
ID:{{event.gramps_id|escape}}{{mediaform.text.label}}:{% render mediaform.text user action %}
Place:{{event.place.title|escape}}{{mediaform.path.label}}:{% render mediaform.path user action %}
{{mediaform.tags.label}}:{% render mediaform.tags user action %}
@@ -37,12 +59,34 @@
  • Attributes
  • References
  • -
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    +
    +
    -
    Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.
    -
    Nam dui erat, auctor a, dignissim quis, sollicitudin eu, felis. Pellentesque nisi urna, interdum eget, sagittis et, consequat vestibulum, lacus. Mauris porttitor ullamcorper augue.
    +
    +
    +
    +
    - +{% if user.is_superuser %} + {% if action == "edit" %} + {% make_button "Back to Media" "/media/%s" media.handle %} + + + {% else %} + {% ifequal action "add" %} + {% make_button "Cancel" "/media/" %} + + + {% else %} + {% make_button "Back to Media" "/media" %} + {% make_button "Add Media" "/media/add" %} + {% make_button "Edit Media" "/media/%s/edit" media.handle %} + {% make_button "Delete Media" "/media/%s/delete" media.handle %} + {% endifequal %} + {% endif %} +{% else %} +{% endif %} + {% endblock %} diff --git a/src/data/templates/view_note_detail.html b/src/data/templates/view_note_detail.html index fe77f9eac..5e453e0c9 100644 --- a/src/data/templates/view_note_detail.html +++ b/src/data/templates/view_note_detail.html @@ -25,22 +25,22 @@ {% endif %}
    {% csrf_token %} -{{noteform.gramps_id.label}}: - {% render noteform.gramps_id user action %} - - -{{noteform.note_type.label}}: -{% render noteform.note_type user action %} - - {{noteform.text.label}}: -{% render noteform.text user action %} +{% render noteform.text user action %} + + {{noteform.preformatted.label}}: {% render noteform.preformatted user action %} +{{noteform.gramps_id.label}}: + {% render noteform.gramps_id user action %} +{{noteform.note_type.label}}: +{% render noteform.note_type user action %} + + {{noteform.tags.label}}: {% render noteform.tags user action %} diff --git a/src/webapp/grampsdb/forms.py b/src/webapp/grampsdb/forms.py index ce81d9d95..3171a816b 100644 --- a/src/webapp/grampsdb/forms.py +++ b/src/webapp/grampsdb/forms.py @@ -163,3 +163,39 @@ class NoteForm(forms.ModelForm): model = Note exclude = ["handle"] +class MediaForm(forms.ModelForm): + class Meta: + model = Media + exclude = ["handle", "sortval", "month1", "year1", "day1", + "newyear", "calendar", "modifier", "quality"] + + def clean(self): + from webapp.utils import dp + data = super(MediaForm, self).clean() + dobj = dp(data.get('text')) + if not dobj.is_valid(): + msg = u"Invalid date format" + self._errors["date"] = self.error_class([msg]) + del data["text"] + return data + + def save(self, commit=True): + from webapp.utils import dp + from webapp.libdjango import DjangoInterface + dji = DjangoInterface() + model = super(MediaForm, self).save(commit=False) + dobj = dp(self.cleaned_data['text']) + dji.add_date(model, dobj.serialize()) + if commit: + model.save() + return model + + text = forms.CharField(label="Date", + required=False, + widget=TextInput(attrs={'size':'70'})) + desc = forms.CharField(label="Title", + required=False, + widget=TextInput(attrs={'size':'70'})) + path = forms.CharField(label="Path", + required=False, + widget=TextInput(attrs={'size':'70'})) diff --git a/src/webapp/grampsdb/models.py b/src/webapp/grampsdb/models.py index 60657c110..44e725f7d 100644 --- a/src/webapp/grampsdb/models.py +++ b/src/webapp/grampsdb/models.py @@ -413,7 +413,7 @@ class PrimaryObject(models.Model): ## Fields: id = models.AutoField(primary_key=True) handle = models.CharField(max_length=19, unique=True) - gramps_id = models.CharField('gramps id', max_length=25, blank=True) + gramps_id = models.CharField('ID', max_length=25, blank=True) last_saved = models.DateTimeField('last changed', auto_now=True) last_changed = models.DateTimeField('last changed', null=True, blank=True) # user edits @@ -431,7 +431,7 @@ class Person(PrimaryObject): """ The model for the person object """ - gender_type = models.ForeignKey('GenderType') + gender_type = models.ForeignKey('GenderType', verbose_name="Gender") probably_alive = models.BooleanField("Probably alive") families = models.ManyToManyField('Family', blank=True, null=True) parent_families = models.ManyToManyField('Family', @@ -475,7 +475,7 @@ class Family(PrimaryObject): null=True, blank=True) mother = models.ForeignKey('Person', related_name="mother_ref", null=True, blank=True) - family_rel_type = models.ForeignKey('FamilyRelType') + family_rel_type = models.ForeignKey('FamilyRelType', verbose_name="Type") tags = models.ManyToManyField('Tag', blank=True, null=True) def make_tag_list(self): @@ -515,7 +515,7 @@ class Source(PrimaryObject): # .datamap_set class Event(DateObject, PrimaryObject): - event_type = models.ForeignKey('EventType') + event_type = models.ForeignKey('EventType', verbose_name="Type") description = models.CharField('description', max_length=50, blank=True) place = models.ForeignKey('Place', null=True, blank=True) references = generic.GenericRelation('EventRef', related_name="refs", @@ -549,7 +549,7 @@ class Place(PrimaryObject): class Media(DateObject, PrimaryObject): path = models.TextField(blank=True) mime = models.TextField(blank=True, null=True) - desc = models.TextField(blank=True) + desc = models.TextField("Title", blank=True) references = generic.GenericRelation('MediaRef', related_name="refs", content_type_field="object_type", object_id_field="object_id") @@ -559,7 +559,7 @@ class Media(DateObject, PrimaryObject): return tuple() class Note(PrimaryObject): - note_type = models.ForeignKey('NoteType') + note_type = models.ForeignKey('NoteType', verbose_name="Type") text = models.TextField(blank=True) preformatted = models.BooleanField('preformatted') references = generic.GenericRelation('NoteRef', related_name="refs", @@ -595,6 +595,7 @@ class Surname(models.Model): Surname table, which links to name. """ name_origin_type = models.ForeignKey('NameOriginType', + verbose_name="Origin", related_name="name_origin_code", default=2) surname = models.TextField(blank=True) @@ -608,7 +609,7 @@ class Surname(models.Model): return "%s" % self.surname class Name(DateObject, SecondaryObject): - name_type = models.ForeignKey('NameType', + name_type = models.ForeignKey('NameType', verbose_name="Type", related_name="name_code", default=2) preferred = models.BooleanField('Preferred name?') diff --git a/src/webapp/grampsdb/view/media.py b/src/webapp/grampsdb/view/media.py index 86cd21674..3937ebd53 100644 --- a/src/webapp/grampsdb/view/media.py +++ b/src/webapp/grampsdb/view/media.py @@ -22,7 +22,7 @@ """ Views for Person, Name, and Surname """ ## Gramps Modules -from webapp.utils import _, boolean +from webapp.utils import _, boolean, update_last_changed from webapp.grampsdb.models import Media from webapp.grampsdb.forms import * from webapp.libdjango import DjangoInterface @@ -41,88 +41,54 @@ def process_media(request, context, handle, action): # view, edit, save context["tview"] = _("Media") context["tviews"] = _("Media") context["action"] = "view" - context["object"] = Media() view_template = "view_media_detail.html" - return render_to_response(view_template, context) - if request.user.is_authenticated(): - if action in ["edit", "view"]: - pf, nf, sf, person = get_person_forms(handle, empty=False) - elif action == "add": - pf, nf, sf, person = get_person_forms(handle=None, protect=False, empty=True) - elif action == "delete": - pf, nf, sf, person = get_person_forms(handle, protect=False, empty=True) - person.delete() - return redirect("/person/") - elif action in ["save", "create"]: # could be create a new person - # look up old data, if any: - if handle: - person = Person.objects.get(handle=handle) - name = person.name_set.get(preferred=True) - surname = name.surname_set.get(primary=True) - else: # create new item - person = Person(handle=create_id()) - name = Name(person=person, preferred=True) - surname = Surname(name=name, primary=True, order=1) - surname = Surname(name=name, - primary=True, - order=1, - name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) - # combine with user data: - pf = PersonForm(request.POST, instance=person) - pf.model = person - nf = NameFormFromPerson(request.POST, instance=name) - nf.model = name - sf = SurnameForm(request.POST, instance=surname) - # check if valid: - if nf.is_valid() and pf.is_valid() and sf.is_valid(): - # name.preferred and surname.primary get set False in the above is_valid() - person = pf.save() - # Process data: - name.person = person - name = nf.save(commit=False) - # Manually set any data: - name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else "" - name.preferred = True # FIXME: why is this False? - check_preferred(name, person) - name.save() - # Process data: - surname.name = name - surname = sf.save(commit=False) - # Manually set any data: - surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" - surname.primary = True # FIXME: why is this False? - surname.save() - # FIXME: last_saved, last_changed, last_changed_by - dji.rebuild_cache(person) - # FIXME: update probably_alive - return redirect("/person/%s" % person.handle) - else: - # need to edit again - if handle: - action = "edit" - else: - action = "add" - else: # error? - raise Http404(_("Requested %s does not exist.") % "person") - else: # not authenticated - # BEGIN NON-AUTHENTICATED ACCESS - try: - person = Person.objects.get(handle=handle) - except: - raise Http404(_("Requested %s does not exist.") % "person") - if person.private: - raise Http404(_("Requested %s does not exist.") % "person") - pf, nf, sf, person = get_person_forms(handle, protect=True) - # END NON-AUTHENTICATED ACCESS - context["action"] = action - context["view"] = "person" - context["tview"] = _("Person") - context["tviews"] = _("People") - context["personform"] = pf - context["nameform"] = nf - context["surnameform"] = sf - context["person"] = person - context["object"] = person - context["next"] = "/person/%s" % person.handle + if handle == "add": + action = "add" + if request.POST.has_key("action"): + action = request.POST.get("action") + # Handle: edit, view, add, create, save, delete + if action == "add": + media = Media() + mediaform = MediaForm(instance=media) + mediaform.model = media + elif action in ["view", "edit"]: + media = Media.objects.get(handle=handle) + mediaform = MediaForm(instance=media) + mediaform.model = media + elif action == "save": + media = Media.objects.get(handle=handle) + mediaform = MediaForm(request.POST, instance=media) + mediaform.model = media + if mediaform.is_valid(): + update_last_changed(media, request.user.username) + media = mediaform.save() + dji.rebuild_cache(media) + action = "view" + else: + action = "edit" + elif action == "create": + media = Media(handle=create_id()) + mediaform = MediaForm(request.POST, instance=media) + mediaform.model = media + if mediaform.is_valid(): + update_last_changed(media, request.user.username) + media = mediaform.save() + dji.rebuild_cache(media) + action = "view" + else: + action = "add" + elif action == "delete": + media = Media.objects.get(handle=handle) + media.delete() + return redirect("/media/") + else: + raise Exception("Unhandled action: '%s'" % action) + + context["mediaform"] = mediaform + context["object"] = media + context["media"] = media + context["action"] = action + + return render_to_response(view_template, context)