From a2552321a9c615b9ec3d674b50dacc9e6a600626 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Tue, 13 May 2014 14:09:11 -0400 Subject: [PATCH] Issues with keyword editing --- karmaworld/apps/notes/views.py | 24 ++++++++++++++---------- karmaworld/apps/quizzes/forms.py | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index effd4a1..04203e1 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -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): diff --git a/karmaworld/apps/quizzes/forms.py b/karmaworld/apps/quizzes/forms.py index 47de948..96cf740 100644 --- a/karmaworld/apps/quizzes/forms.py +++ b/karmaworld/apps/quizzes/forms.py @@ -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) -- 2.25.1