save tags for new resource
authorJan Szejko <janek37@gmail.com>
Thu, 13 Apr 2017 14:55:19 +0000 (16:55 +0200)
committerJan Szejko <janek37@gmail.com>
Thu, 13 Apr 2017 14:55:19 +0000 (16:55 +0200)
apps/catalogue/forms.py
apps/catalogue/views.py

index e286a1d..dc811ab 100644 (file)
@@ -26,12 +26,8 @@ class DocumentCreateForm(forms.Form):
     """
     owner_organization = forms.CharField(required=False)
     title = forms.CharField()
-    language = forms.CharField()
     publisher = forms.CharField(required=False)
     description = forms.CharField(required=False)
-    rights = forms.CharField(required=False)
-    audience = forms.CharField()
-    
     cover = forms.FileField(required=False)
 
     def clean_cover(self):
@@ -58,10 +54,11 @@ class TagForm(forms.Form):
         if self.instance:
             self.field().initial = self.initial()
 
-    def save(self):
-        assert self.instance, 'No instance provided'
-        self.instance.tags.remove(*self.instance.tags.filter(category=self.category))
-        self.instance.tags.add(self.cleaned_tags())
+    def save(self, instance=None):
+        instance = instance or self.instance
+        assert instance, 'No instance provided'
+        instance.tags.remove(*instance.tags.filter(category=self.category))
+        instance.tags.add(*self.cleaned_tags())
 
     def field(self):
         raise NotImplementedError
@@ -72,6 +69,11 @@ class TagForm(forms.Form):
     def cleaned_tags(self):
         raise NotImplementedError
 
+    def metadata_rows(self):
+        return '\n'.join(
+            '<dc:%(name)s>%(value)s</dc:%(name)s>' % {'name': tag.category.dc_tag, 'value': tag.dc_value}
+            for tag in self.cleaned_tags())
+
 
 class TagSelect(forms.Select):
     def render_option(self, selected_choices, option_value, option_label):
index 4c1a21c..86dc74d 100644 (file)
@@ -94,6 +94,9 @@ def create_missing(request):
 
             doc = Document.objects.create(**kwargs)
 
+            for tag_form in tag_forms:
+                tag_form.save(instance=doc)
+
             cover = request.FILES.get('cover')
             if cover:
                 uppath = 'uploads/%d/' % doc.pk
@@ -116,9 +119,7 @@ def create_missing(request):
                 <metadata>
                     <dc:publisher>''' + form.cleaned_data['publisher'] + '''</dc:publisher>
                     <dc:description>''' + form.cleaned_data['description'] + '''</dc:description>
-                    <dc:language>''' + form.cleaned_data['language'] + '''</dc:language>
-                    <dc:rights>''' + form.cleaned_data['rights'] + '''</dc:rights>
-                    <dc:audience>''' + form.cleaned_data['audience'] + '''</dc:audience>
+                    ''' + '\n'.join(tag_form.metadata_rows() for tag_form in tag_forms) + '''
                     <dc:relation.coverImage.url>''' + cover_url + '''</dc:relation.coverImage.url>
                 </metadata>
                 <header>''' + title + '''</header>