From: Jacob Hilker Date: Tue, 11 Feb 2014 04:24:29 +0000 (-0500) Subject: Add ability to edit tags on notes X-Git-Tag: release-20150131~148^2~15^2~1 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=df4f2a9f29b898e9036ccde7001c196e1690f979;p=oweals%2Fkarmaworld.git Add ability to edit tags on notes --- diff --git a/karmaworld/apps/document_upload/models.py b/karmaworld/apps/document_upload/models.py index 8d26b24..ccf8e81 100644 --- a/karmaworld/apps/document_upload/models.py +++ b/karmaworld/apps/document_upload/models.py @@ -67,6 +67,8 @@ class RawDocument(Document): def save(self, user=None, *args, **kwargs): super(RawDocument, self).save(*args, **kwargs) + + def process_document(self, user=None): if not self.is_processed: tasks.process_raw_document.delay(self, user) diff --git a/karmaworld/apps/document_upload/views.py b/karmaworld/apps/document_upload/views.py index b0195ef..a8c85b2 100644 --- a/karmaworld/apps/document_upload/views.py +++ b/karmaworld/apps/document_upload/views.py @@ -13,6 +13,7 @@ def save_fp_upload(request): """ ajax endpoint for saving a FilePicker uploaded file form """ r_d_f = RawDocumentForm(request.POST) + if r_d_f.is_valid(): raw_document = r_d_f.save(commit=False) @@ -33,6 +34,9 @@ def save_fp_upload(request): raw_document.save() # save the tags to the database, too. don't forget those guys. r_d_f.save_m2m() + # Proccess document after the tags are saved so that it isn't converted + # to a note before the tags are attached to the document + raw_document.process_document() return HttpResponse({'success'}) else: diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index e970655..f6385c3 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -5,6 +5,8 @@ import json import traceback import logging + +from django.core import serializers from django.core.exceptions import ObjectDoesNotExist from karmaworld.apps.courses.models import Course from karmaworld.apps.notes.search import SearchIndex @@ -279,4 +281,22 @@ def downloaded_note(request, pk): """Record that somebody has flagged a note.""" return ajax_base(Note, request, pk, process_downloaded_note) +def edit_note_tags(request, pk): + """ + Saves the posted string of tags + """ + if request.method == "POST" and request.is_ajax(): + note = Note.objects.get(pk=pk) + + # note.tags.set(*json.loads(request.body)) + note.tags.set(request.body) + + note_json = serializers.serialize('json', [note,]) + resp = json.loads(note_json)[0] + resp['fields']['tags'] = list(note.tags.names()) + + return HttpResponse(json.dumps(resp), mimetype="application/json") + else: + return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'Invalid request'}), + mimetype="application/json") diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index af2638b..f4a329d 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -40,7 +40,7 @@ class UserProfile(models.Model): return sum - def can_edit_courses(self): + def can_edit_items(self): return (self.get_points() >= 20) NO_BADGE = 0 diff --git a/karmaworld/assets/css/global.css b/karmaworld/assets/css/global.css index 6386b43..6426f43 100644 --- a/karmaworld/assets/css/global.css +++ b/karmaworld/assets/css/global.css @@ -614,10 +614,11 @@ a.activity_target:hover #add-course-btn, #existing-course-btn, #save-btn, +#save_note_tags, #edit-save-btn { border: none; - background-color: white; + background-color: transparent; color: #f05a28; cursor: pointer; font: 30px/1.2em "MuseoSlab-300", serif; diff --git a/karmaworld/assets/css/note_course_pages.css b/karmaworld/assets/css/note_course_pages.css index bf17df4..c23bedd 100644 --- a/karmaworld/assets/css/note_course_pages.css +++ b/karmaworld/assets/css/note_course_pages.css @@ -12,7 +12,7 @@ margin-right: 3px; } -#note_status, #note_pedigree +#note_status, #note_pedigree, #note_tags { text-align: center; padding-top: 10px; @@ -112,3 +112,24 @@ padding-bottom: 20px; margin-bottom: 20px; } + +.tag-span:after +{ + content: ', '; +} + +.tag-span:last-child:after +{ + content: ''; +} + +#note_tags_form +{ + text-align: center; +} + +#note_tags_input +{ + margin-left: 25%; + width: 50%; +} diff --git a/karmaworld/assets/js/note-detail.js b/karmaworld/assets/js/note-detail.js index 28ece64..d81dc79 100644 --- a/karmaworld/assets/js/note-detail.js +++ b/karmaworld/assets/js/note-detail.js @@ -6,8 +6,8 @@ function autoResize(id){ var newwidth; if(document.getElementById){ - newheight = document.getElementById(id).contentWindow.document .body.scrollHeight; - newwidth = document.getElementById(id).contentWindow.document .body.scrollWidth; + newheight = document.getElementById(id).contentWindow.document.body.scrollHeight; + newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth; } document.getElementById(id).height = (newheight + 10) + "px"; @@ -130,25 +130,45 @@ $(function() { }; }); - $.ajax(note_contents_url, - { - type: 'GET', - xhrFields: { - onprogress: function (progress) { - var percentage = Math.floor((progress.loaded / progress.total) * 100); - writeNoteFrame("

" + percentage + "%

"); - } - }, - success: function(data, textStatus, jqXHR) { - writeNoteFrame(data); - autoResize('noteframe'); - if (pdfControls == true) { - setupPdfViewer(); - } - }, - error: function(data, textStatus, jqXHR) { - writeNoteFrame("

Sorry, your note could not be retrieved.

"); + $.ajax(note_contents_url, { + type: 'GET', + xhrFields: { + onprogress: function (progress) { + var percentage = Math.floor((progress.loaded / progress.total) * 100); + writeNoteFrame("

" + percentage + "%

"); + } + }, + success: function(data, textStatus, jqXHR) { + writeNoteFrame(data); + autoResize('noteframe'); + if (pdfControls == true) { + setupPdfViewer(); + } + }, + error: function(data, textStatus, jqXHR) { + writeNoteFrame("

Sorry, your note could not be retrieved.

"); + } + }); + + $('#edit_note_tags').click(function(event) { + $('#note_tags_form').slideToggle(); + }); + + $('#save_note_tags').click(function(event) { + $.ajax({ + url: edit_note_tags_url, + dataType: 'json', + data: $('#note_tags_input').val(), + // data: JSON.stringify(['test','tags']),//$('#edit-course-form').children().serialize(), + type: 'POST', + success: function(data) { + $('#note_tags_form').slideUp(); + $('.tags').empty(); + $.each(data.fields.tags, function(index, tag) { + $('.tags').append($('', { class: 'tag-span', text: tag })); + }); } }); + }); }); diff --git a/karmaworld/templates/courses/course_detail.html b/karmaworld/templates/courses/course_detail.html index 1514190..d3d6bd8 100644 --- a/karmaworld/templates/courses/course_detail.html +++ b/karmaworld/templates/courses/course_detail.html @@ -68,7 +68,7 @@
{% if user.is_authenticated %} - {% if user.get_profile.can_edit_courses %} + {% if user.get_profile.can_edit_items %} edit_flag {% endif %} @@ -101,7 +101,7 @@
- {% if user.get_profile.can_edit_courses %} + {% if user.get_profile.can_edit_items %}
{% csrf_token %} diff --git a/karmaworld/templates/notes/note_detail.html b/karmaworld/templates/notes/note_detail.html index f2ea776..4d20454 100644 --- a/karmaworld/templates/notes/note_detail.html +++ b/karmaworld/templates/notes/note_detail.html @@ -13,6 +13,7 @@