From: Charles Connell Date: Sun, 2 Feb 2014 18:52:31 +0000 (-0500) Subject: Save flag and thank events on users now, not sessions X-Git-Tag: release-20150131~182 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=bf2d0b9f1c80736f92bf2d064e5688dcdbe53404;p=oweals%2Fkarmaworld.git Save flag and thank events on users now, not sessions --- diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index d485ea3..191793d 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -24,6 +24,7 @@ from karmaworld.apps.notes.forms import FileUploadForm from karmaworld.utils import ajax_increment, format_session_increment_field FLAG_FIELD = 'flags' +USER_PROFILE_FLAGS_FIELD = 'flagged_courses' class CourseListView(ListView, ModelFormMixin, ProcessFormView): @@ -80,8 +81,12 @@ class CourseDetailView(DetailView): # For the Filepicker Partial template kwargs['file_upload_form'] = FileUploadForm() - if self.request.session.get(format_session_increment_field(Course, self.object.id, FLAG_FIELD), False): - kwargs['already_flagged'] = True + if self.request.user.is_authenticated(): + try: + self.request.user.get_profile().flagged_courses.get(pk=self.object.pk) + kwargs['already_flagged'] = True + except ObjectDoesNotExist: + pass return kwargs @@ -199,5 +204,5 @@ def process_course_flag_events(request_user, course): def flag_course(request, pk): """Record that somebody has flagged a note.""" - return ajax_increment(Course, request, pk, FLAG_FIELD, process_course_flag_events) + return ajax_increment(Course, request, pk, FLAG_FIELD, USER_PROFILE_FLAGS_FIELD, process_course_flag_events) diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index ad9b1e6..8736288 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -32,18 +32,23 @@ PDF_MIMETYPES = ( 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ) + def is_pdf(self): if self.object.file_type == 'pdf': return True return False + def is_ppt(self): if self.object.file_type == 'ppt': return True return False THANKS_FIELD = 'thanks' +USER_PROFILE_THANKS_FIELD = 'thanked_notes' FLAG_FIELD = 'flags' +USER_PROFILE_FLAGS_FIELD = 'flagged_notes' + class NoteDetailView(DetailView): """ Class-based view for the note html page """ @@ -63,11 +68,18 @@ class NoteDetailView(DetailView): if self.object.mimetype in PDF_MIMETYPES: kwargs['pdf_controls'] = True - if self.request.session.get(format_session_increment_field(Note, self.object.id, THANKS_FIELD), False): - kwargs['already_thanked'] = True + if self.request.user.is_authenticated(): + try: + self.request.user.get_profile().thanked_notes.get(pk=self.object.pk) + kwargs['already_thanked'] = True + except ObjectDoesNotExist: + pass - if self.request.session.get(format_session_increment_field(Note, self.object.id, FLAG_FIELD), False): - kwargs['already_flagged'] = True + try: + self.request.user.get_profile().flagged_notes.get(pk=self.object.pk) + kwargs['already_flagged'] = True + except ObjectDoesNotExist: + pass return super(NoteDetailView, self).get_context_data(**kwargs) @@ -237,7 +249,7 @@ def process_note_thank_events(request_user, note): def thank_note(request, pk): """Record that somebody has thanked a note.""" - return ajax_increment(Note, request, pk, THANKS_FIELD, process_note_thank_events) + return ajax_increment(Note, request, pk, THANKS_FIELD, USER_PROFILE_THANKS_FIELD, process_note_thank_events) def process_note_flag_events(request_user, note): @@ -252,7 +264,7 @@ def process_note_flag_events(request_user, note): def flag_note(request, pk): """Record that somebody has flagged a note.""" - return ajax_increment(Note, request, pk, FLAG_FIELD, process_note_flag_events) + return ajax_increment(Note, request, pk, FLAG_FIELD, USER_PROFILE_FLAGS_FIELD, process_note_flag_events) def process_downloaded_note(request_user, note): diff --git a/karmaworld/apps/users/migrations/0006_auto.py b/karmaworld/apps/users/migrations/0006_auto.py new file mode 100644 index 0000000..1a43a17 --- /dev/null +++ b/karmaworld/apps/users/migrations/0006_auto.py @@ -0,0 +1,208 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding M2M table for field thanked_notes on 'UserProfile' + m2m_table_name = db.shorten_name(u'users_userprofile_thanked_notes') + db.create_table(m2m_table_name, ( + ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), + ('userprofile', models.ForeignKey(orm[u'users.userprofile'], null=False)), + ('note', models.ForeignKey(orm[u'notes.note'], null=False)) + )) + db.create_unique(m2m_table_name, ['userprofile_id', 'note_id']) + + # Adding M2M table for field flagged_notes on 'UserProfile' + m2m_table_name = db.shorten_name(u'users_userprofile_flagged_notes') + db.create_table(m2m_table_name, ( + ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), + ('userprofile', models.ForeignKey(orm[u'users.userprofile'], null=False)), + ('note', models.ForeignKey(orm[u'notes.note'], null=False)) + )) + db.create_unique(m2m_table_name, ['userprofile_id', 'note_id']) + + # Adding M2M table for field flagged_courses on 'UserProfile' + m2m_table_name = db.shorten_name(u'users_userprofile_flagged_courses') + db.create_table(m2m_table_name, ( + ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), + ('userprofile', models.ForeignKey(orm[u'users.userprofile'], null=False)), + ('course', models.ForeignKey(orm[u'courses.course'], null=False)) + )) + db.create_unique(m2m_table_name, ['userprofile_id', 'course_id']) + + + def backwards(self, orm): + # Removing M2M table for field thanked_notes on 'UserProfile' + db.delete_table(db.shorten_name(u'users_userprofile_thanked_notes')) + + # Removing M2M table for field flagged_notes on 'UserProfile' + db.delete_table(db.shorten_name(u'users_userprofile_flagged_notes')) + + # Removing M2M table for field flagged_courses on 'UserProfile' + db.delete_table(db.shorten_name(u'users_userprofile_flagged_courses')) + + + models = { + u'auth.group': { + 'Meta': {'object_name': 'Group'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + u'auth.permission': { + 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + u'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + u'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'courses.course': { + 'Meta': {'ordering': "['-file_count', 'school', 'name']", 'unique_together': "(('name', 'department'),)", 'object_name': 'Course'}, + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'department': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['courses.Department']", 'null': 'True', 'blank': 'True'}), + 'desc': ('django.db.models.fields.TextField', [], {'max_length': '511', 'null': 'True', 'blank': 'True'}), + 'file_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'flags': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'instructor_email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'null': 'True', 'blank': 'True'}), + 'instructor_name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'school': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['courses.School']", 'null': 'True', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '150', 'null': 'True'}), + 'updated_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.utcnow'}), + 'url': ('django.db.models.fields.URLField', [], {'max_length': '511', 'null': 'True', 'blank': 'True'}) + }, + u'courses.department': { + 'Meta': {'unique_together': "(('name', 'school'),)", 'object_name': 'Department'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'school': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['courses.School']"}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '150', 'null': 'True'}), + 'url': ('django.db.models.fields.URLField', [], {'max_length': '511', 'null': 'True', 'blank': 'True'}) + }, + u'courses.school': { + 'Meta': {'ordering': "['-file_count', '-priority', 'name']", 'object_name': 'School'}, + 'alias': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'facebook_id': ('django.db.models.fields.BigIntegerField', [], {'null': 'True', 'blank': 'True'}), + 'file_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'hashtag': ('django.db.models.fields.CharField', [], {'max_length': '16', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'location': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'priority': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '150', 'null': 'True'}), + 'url': ('django.db.models.fields.URLField', [], {'max_length': '511', 'blank': 'True'}), + 'usde_id': ('django.db.models.fields.BigIntegerField', [], {'unique': 'True', 'null': 'True', 'blank': 'True'}) + }, + u'licenses.license': { + 'Meta': {'object_name': 'License'}, + 'html': ('django.db.models.fields.TextField', [], {}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}) + }, + u'notes.note': { + 'Meta': {'ordering': "['-uploaded_at']", 'unique_together': "(('fp_file', 'upstream_link'),)", 'object_name': 'Note'}, + 'course': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['courses.Course']"}), + 'file_type': ('django.db.models.fields.CharField', [], {'default': "'???'", 'max_length': '15', 'null': 'True', 'blank': 'True'}), + 'flags': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'fp_file': ('django_filepicker.models.FPFileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), + 'gdrive_url': ('django.db.models.fields.URLField', [], {'max_length': '1024', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'html': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39', 'null': 'True', 'blank': 'True'}), + 'is_hidden': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'license': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['licenses.License']", 'null': 'True', 'blank': 'True'}), + 'mimetype': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'pdf_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '255'}), + 'static_html': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'text': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'thanks': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}), + 'tweeted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'uploaded_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.utcnow', 'null': 'True'}), + 'upstream_link': ('django.db.models.fields.URLField', [], {'max_length': '1024', 'unique': 'True', 'null': 'True', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']", 'null': 'True', 'on_delete': 'models.SET_NULL', 'blank': 'True'}), + 'year': ('django.db.models.fields.IntegerField', [], {'default': '2014', 'null': 'True', 'blank': 'True'}) + }, + u'taggit.tag': { + 'Meta': {'ordering': "['namespace', 'name']", 'object_name': 'Tag'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}), + 'namespace': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '100'}) + }, + u'taggit.taggeditem': { + 'Meta': {'object_name': 'TaggedItem'}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "u'taggit_taggeditem_tagged_items'", 'to': u"orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'object_id': ('django.db.models.fields.IntegerField', [], {'db_index': 'True'}), + 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "u'taggit_taggeditem_items'", 'to': u"orm['taggit.Tag']"}) + }, + u'users.coursekarmaevent': { + 'Meta': {'unique_together': "(('points', 'user', 'timestamp'),)", 'object_name': 'CourseKarmaEvent'}, + 'course': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['courses.Course']"}), + 'event_type': ('django.db.models.fields.CharField', [], {'max_length': '15'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'points': ('django.db.models.fields.IntegerField', [], {}), + 'timestamp': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.utcnow'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}) + }, + u'users.generickarmaevent': { + 'Meta': {'unique_together': "(('points', 'user', 'timestamp'),)", 'object_name': 'GenericKarmaEvent'}, + 'event_type': ('django.db.models.fields.CharField', [], {'default': "'none'", 'max_length': '15'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'message': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'points': ('django.db.models.fields.IntegerField', [], {}), + 'timestamp': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.utcnow'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}) + }, + u'users.notekarmaevent': { + 'Meta': {'unique_together': "(('points', 'user', 'timestamp'),)", 'object_name': 'NoteKarmaEvent'}, + 'event_type': ('django.db.models.fields.CharField', [], {'max_length': '15'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'note': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['notes.Note']"}), + 'points': ('django.db.models.fields.IntegerField', [], {}), + 'timestamp': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.utcnow'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}) + }, + u'users.userprofile': { + 'Meta': {'object_name': 'UserProfile'}, + 'flagged_courses': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'users_flagged'", 'symmetrical': 'False', 'to': u"orm['courses.Course']"}), + 'flagged_notes': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'users_flagged'", 'symmetrical': 'False', 'to': u"orm['notes.Note']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'school': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['courses.School']", 'null': 'True', 'blank': 'True'}), + 'thanked_notes': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'users_thanked'", 'symmetrical': 'False', 'to': u"orm['notes.Note']"}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['auth.User']", 'unique': 'True'}) + } + } + + complete_apps = ['users'] \ No newline at end of file diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index 15af634..4899330 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -22,9 +22,11 @@ class UserProfileManager(models.Manager): class UserProfile(models.Model): - user = models.OneToOneField(User) - - school = models.ForeignKey(School, blank=True, null=True) + user = models.OneToOneField(User) + thanked_notes = models.ManyToManyField('notes.Note', related_name='users_thanked') + flagged_notes = models.ManyToManyField('notes.Note', related_name='users_flagged') + flagged_courses = models.ManyToManyField('courses.Course', related_name='users_flagged') + school = models.ForeignKey(School, blank=True, null=True) def natural_key(self): return (self.user,) diff --git a/karmaworld/assets/js/course-detail.js b/karmaworld/assets/js/course-detail.js index c0149a5..d4ea78b 100644 --- a/karmaworld/assets/js/course-detail.js +++ b/karmaworld/assets/js/course-detail.js @@ -17,6 +17,7 @@ $(function() { dataType: "json", type: 'POST' }); + } }); }); diff --git a/karmaworld/assets/js/note-detail.js b/karmaworld/assets/js/note-detail.js index 150ef2d..28ece64 100644 --- a/karmaworld/assets/js/note-detail.js +++ b/karmaworld/assets/js/note-detail.js @@ -104,7 +104,7 @@ $(function() { $('#flag-button-disabled').show(); $(this).unbind('click'); - // tell server that somebody thanked + // tell server that somebody flagged // this note $.ajax({ url: note_flag_url, diff --git a/karmaworld/utils/ajax_increment.py b/karmaworld/utils/ajax_increment.py index 2e6e466..8484480 100644 --- a/karmaworld/utils/ajax_increment.py +++ b/karmaworld/utils/ajax_increment.py @@ -25,7 +25,7 @@ def ajax_base(cls, request, pk, event_processor): return HttpResponse(status=204) -def ajax_increment(cls, request, pk, field, event_processor=None): +def ajax_increment(cls, request, pk, field, user_profile_field=None, event_processor=None): def ajax_increment_work(request_user, obj): count = getattr(obj, field) setattr(obj, field, count+1) @@ -36,7 +36,9 @@ def ajax_increment(cls, request, pk, field, event_processor=None): # Record that user has performed this, to prevent # them from doing it again - request.session[format_session_increment_field(cls, pk, field)] = True + if user_profile_field: + getattr(request_user.get_profile(), user_profile_field).add(obj) + obj.save() return ajax_base(cls, request, pk, ajax_increment_work)