Issues with keyword editing
authorCharles Connell <charles@connells.org>
Tue, 13 May 2014 18:09:11 +0000 (14:09 -0400)
committerCharles Connell <charles@connells.org>
Tue, 13 May 2014 18:09:11 +0000 (14:09 -0400)
karmaworld/apps/notes/views.py
karmaworld/apps/quizzes/forms.py

index effd4a1689468e6ca3fb2e16fd7560412e32644c..04203e16f22a888bac4762e3087d97f8b3471738 100644 (file)
@@ -206,21 +206,25 @@ class NoteKeywordsView(FormView, SingleObjectMixin):
             word = form['keyword'].data
             definition = form['definition'].data
             id = form['id'].data
-            if not word and not definition:
+            # If the user has deleted an existing keyword
+            if not word and not definition and id:
                 try:
                     keyword_object = Keyword.objects.get(id=id)
                     keyword_object.delete()
                 except (ValueError, ObjectDoesNotExist):
                     pass
-            try:
-                keyword_object = Keyword.objects.get(id=id)
-            except (ValueError, ObjectDoesNotExist):
-                keyword_object = Keyword()
-
-            keyword_object.note = self.get_object()
-            keyword_object.word = word
-            keyword_object.definition = definition
-            keyword_object.save()
+
+            # otherwise get or create a keyword
+            elif word or definition:
+                try:
+                    keyword_object = Keyword.objects.get(id=id)
+                except (ValueError, ObjectDoesNotExist):
+                    keyword_object = Keyword()
+
+                keyword_object.note = self.get_object()
+                keyword_object.word = word
+                keyword_object.definition = definition
+                keyword_object.save()
 
 
 class NoteQuizView(TemplateView):
index 47de948b7787b5c1b8eae4464dd14e2757a5a484..96cf740d8220a993b4670d994dca165b55578583 100644 (file)
@@ -6,7 +6,7 @@ from django.forms import TextInput, Textarea, HiddenInput, Form, CharField, Inte
 
 class KeywordForm(Form):
     keyword = CharField(widget=TextInput(attrs={'placeholder': 'Keyword', 'class': 'keyword'}),
-                        required=True)
+                        required=False)
     definition = CharField(widget=Textarea(attrs={'placeholder': 'Definition', 'class': 'definition'}),
                            required=False)
     id = IntegerField(widget=HiddenInput(attrs={'class': 'object-id'}), required=False)