diff --git a/core/auth.py b/core/auth.py new file mode 100644 index 00000000..e35df54c --- /dev/null +++ b/core/auth.py @@ -0,0 +1,54 @@ +import logging +from social_auth.backends.pipeline.social import social_auth_user, load_extra_data +from social_auth.models import UserSocialAuth +from regluit.core.models import TWITTER, FACEBOOK + +logger = logging.getLogger(__name__) + + +def selectively_associate(backend, uid, user=None, *args, **kwargs): + """Not using Facebook or Twitter to authenticate a user. + """ + social_user = UserSocialAuth.get_social_auth(backend.name, uid) + if backend.name in ('twitter', 'facebook'): + # not for authentication + return {'social_user': social_user} + return social_auth_user(backend, uid, user=None, *args, **kwargs) + +def facebook_extra_values( user, extra_data): + try: + facebook_id = extra_data.get('id') + user.profile.facebook_id = facebook_id + if user.profile.avatar_source is None: + user.profile.avatar_source = FACEBOOK + user.profile.save() + return True + except Exception,e: + logger.error(e) + return False + +def twitter_extra_values( user, extra_data): + try: + twitter_id = extra_data.get('screen_name') + profile_image_url = extra_data.get('profile_image_url_https') + user.profile.twitter_id = twitter_id + if user.profile.avatar_source is None or user.profile.avatar_source is TWITTER: + user.profile.pic_url = profile_image_url + if user.profile.avatar_source is None: + user.profile.avatar_source = TWITTER + user.profile.save() + return True + except Exception,e: + logger.error(e) + return False + +def deliver_extra_data(backend, details, response, uid, user, social_user=None, + *args, **kwargs): + pipeline_data = load_extra_data(backend, details, response, uid, user, social_user=None, + *args, **kwargs) + + if backend.name is 'twitter': + twitter_extra_values( user, social_user.extra_data) + if backend.name is 'facebook': + facebook_extra_values( user, social_user.extra_data) + diff --git a/core/migrations/0042_auto__add_field_userprofile_avatar_source.py b/core/migrations/0042_auto__add_field_userprofile_avatar_source.py new file mode 100644 index 00000000..051b7697 --- /dev/null +++ b/core/migrations/0042_auto__add_field_userprofile_avatar_source.py @@ -0,0 +1,235 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding field 'UserProfile.avatar_source' + db.add_column('core_userprofile', 'avatar_source', + self.gf('django.db.models.fields.PositiveSmallIntegerField')(null=True), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'UserProfile.avatar_source' + db.delete_column('core_userprofile', 'avatar_source') + + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + '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': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + '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': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + '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': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + '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'}), + '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'}) + }, + 'core.author': { + 'Meta': {'object_name': 'Author'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'editions': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'authors'", 'symmetrical': 'False', 'to': "orm['core.Edition']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '500'}) + }, + 'core.badge': { + 'Meta': {'object_name': 'Badge'}, + 'description': ('django.db.models.fields.TextField', [], {'default': "''", 'null': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '72', 'blank': 'True'}) + }, + 'core.campaign': { + 'Meta': {'object_name': 'Campaign'}, + 'activated': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'amazon_receiver': ('django.db.models.fields.CharField', [], {'max_length': '100', 'blank': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'deadline': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True'}), + 'description': ('ckeditor.fields.RichTextField', [], {'null': 'True'}), + 'details': ('ckeditor.fields.RichTextField', [], {'null': 'True', 'blank': 'True'}), + 'edition': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'campaigns'", 'null': 'True', 'to': "orm['core.Edition']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'left': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '14', 'decimal_places': '2'}), + 'license': ('django.db.models.fields.CharField', [], {'default': "'CC BY-NC-ND'", 'max_length': '255'}), + 'managers': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'campaigns'", 'symmetrical': 'False', 'to': "orm['auth.User']"}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '500', 'null': 'True'}), + 'paypal_receiver': ('django.db.models.fields.CharField', [], {'max_length': '100', 'blank': 'True'}), + 'status': ('django.db.models.fields.CharField', [], {'default': "'INITIALIZED'", 'max_length': '15', 'null': 'True'}), + 'target': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '14', 'decimal_places': '2'}), + 'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'campaigns'", 'to': "orm['core.Work']"}) + }, + 'core.campaignaction': { + 'Meta': {'object_name': 'CampaignAction'}, + 'campaign': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'actions'", 'to': "orm['core.Campaign']"}), + 'comment': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'timestamp': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': '15'}) + }, + 'core.celerytask': { + 'Meta': {'object_name': 'CeleryTask'}, + 'active': ('django.db.models.fields.NullBooleanField', [], {'default': 'True', 'null': 'True', 'blank': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2013, 3, 14, 0, 0)', 'auto_now_add': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.CharField', [], {'max_length': '2048', 'null': 'True'}), + 'function_args': ('django.db.models.fields.IntegerField', [], {'null': 'True'}), + 'function_name': ('django.db.models.fields.CharField', [], {'max_length': '1024'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'task_id': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'tasks'", 'null': 'True', 'to': "orm['auth.User']"}) + }, + 'core.claim': { + 'Meta': {'object_name': 'Claim'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'rights_holder': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'claim'", 'to': "orm['core.RightsHolder']"}), + 'status': ('django.db.models.fields.CharField', [], {'default': "'pending'", 'max_length': '7'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'claim'", 'to': "orm['auth.User']"}), + 'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'claim'", 'to': "orm['core.Work']"}) + }, + 'core.ebook': { + 'Meta': {'object_name': 'Ebook'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'edition': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ebooks'", 'to': "orm['core.Edition']"}), + 'format': ('django.db.models.fields.CharField', [], {'max_length': '25'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'provider': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'rights': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'db_index': 'True'}), + 'url': ('django.db.models.fields.URLField', [], {'max_length': '1024'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True'}) + }, + 'core.edition': { + 'Meta': {'object_name': 'Edition'}, + 'cover_image': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'public_domain': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}), + 'publication_date': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}), + 'publisher': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '1000'}), + 'unglued': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'editions'", 'null': 'True', 'to': "orm['core.Work']"}) + }, + 'core.identifier': { + 'Meta': {'unique_together': "(('type', 'value'),)", 'object_name': 'Identifier'}, + 'edition': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'identifiers'", 'null': 'True', 'to': "orm['core.Edition']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': '4'}), + 'value': ('django.db.models.fields.CharField', [], {'max_length': '31'}), + 'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'identifiers'", 'to': "orm['core.Work']"}) + }, + 'core.key': { + 'Meta': {'object_name': 'Key'}, + 'encrypted_value': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}) + }, + 'core.premium': { + 'Meta': {'object_name': 'Premium'}, + 'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '0'}), + 'campaign': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'premiums'", 'null': 'True', 'to': "orm['core.Campaign']"}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'null': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'limit': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': '2'}) + }, + 'core.rightsholder': { + 'Meta': {'object_name': 'RightsHolder'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'email': ('django.db.models.fields.CharField', [], {'max_length': '100', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'owner': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'rights_holder'", 'to': "orm['auth.User']"}), + 'rights_holder_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + 'core.subject': { + 'Meta': {'ordering': "['name']", 'object_name': 'Subject'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '200'}), + 'works': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'subjects'", 'symmetrical': 'False', 'to': "orm['core.Work']"}) + }, + 'core.userprofile': { + 'Meta': {'object_name': 'UserProfile'}, + 'avatar_source': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}), + 'badges': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'holders'", 'symmetrical': 'False', 'to': "orm['core.Badge']"}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'facebook_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}), + 'goodreads_auth_secret': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'goodreads_auth_token': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'goodreads_user_id': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}), + 'goodreads_user_link': ('django.db.models.fields.CharField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}), + 'goodreads_user_name': ('django.db.models.fields.CharField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}), + 'home_url': ('django.db.models.fields.URLField', [], {'max_length': '200', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'librarything_id': ('django.db.models.fields.CharField', [], {'max_length': '31', 'blank': 'True'}), + 'pic_url': ('django.db.models.fields.URLField', [], {'max_length': '200', 'blank': 'True'}), + 'tagline': ('django.db.models.fields.CharField', [], {'max_length': '140', 'blank': 'True'}), + 'twitter_id': ('django.db.models.fields.CharField', [], {'max_length': '15', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'profile'", 'unique': 'True', 'to': "orm['auth.User']"}) + }, + 'core.waswork': { + 'Meta': {'object_name': 'WasWork'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'moved': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True'}), + 'was': ('django.db.models.fields.IntegerField', [], {'unique': 'True'}), + 'work': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['core.Work']"}) + }, + 'core.wishes': { + 'Meta': {'object_name': 'Wishes', 'db_table': "'core_wishlist_works'"}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'source': ('django.db.models.fields.CharField', [], {'max_length': '15', 'blank': 'True'}), + 'wishlist': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['core.Wishlist']"}), + 'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'wishes'", 'to': "orm['core.Work']"}) + }, + 'core.wishlist': { + 'Meta': {'object_name': 'Wishlist'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'wishlist'", 'unique': 'True', 'to': "orm['auth.User']"}), + 'works': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'wishlists'", 'symmetrical': 'False', 'through': "orm['core.Wishes']", 'to': "orm['core.Work']"}) + }, + 'core.work': { + 'Meta': {'ordering': "['title']", 'object_name': 'Work'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'default': "''", 'null': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'default': "'en'", 'max_length': '2'}), + 'num_wishes': ('django.db.models.fields.IntegerField', [], {'default': '0', 'db_index': 'True'}), + 'openlibrary_lookup': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '1000'}) + } + } + + complete_apps = ['core'] \ No newline at end of file diff --git a/core/models.py b/core/models.py index 7d92f148..dd7d2270 100755 --- a/core/models.py +++ b/core/models.py @@ -1,6 +1,11 @@ import re import random import logging +import urllib +import hashlib + + + from regluit.utils.localdatetime import now, date_today from regluit.utils import crypto from datetime import timedelta @@ -1057,6 +1062,10 @@ def pledger2(): return pledger2.instance pledger2.instance=None +ANONYMOUS_AVATAR = '/static/images/header/avatar.png' +(NO_AVATAR, GRAVATAR, TWITTER, FACEBOOK) = (0, 1, 2, 3) + + class UserProfile(models.Model): created = models.DateTimeField(auto_now_add=True) user = models.OneToOneField(User, related_name='profile') @@ -1067,13 +1076,17 @@ class UserProfile(models.Model): facebook_id = models.PositiveIntegerField(null=True) librarything_id = models.CharField(max_length=31, blank=True) badges = models.ManyToManyField('Badge', related_name='holders') - + goodreads_user_id = models.CharField(max_length=32, null=True, blank=True) goodreads_user_name = models.CharField(max_length=200, null=True, blank=True) goodreads_auth_token = models.TextField(null=True, blank=True) goodreads_auth_secret = models.TextField(null=True, blank=True) goodreads_user_link = models.CharField(max_length=200, null=True, blank=True) + + avatar_source = models.PositiveSmallIntegerField(null = True, default = GRAVATAR, + choices=((NO_AVATAR,'No Avatar, Please'),(GRAVATAR,'Gravatar'),(TWITTER,'Twitter'),(FACEBOOK,'Facebook'))) + def reset_pledge_badge(self): #count user pledges n_pledges = self.pledge_count @@ -1164,6 +1177,27 @@ class UserProfile(models.Model): except Exception, e: logger.error("error unsubscribing from mailchimp list %s" % (e)) return False + + def gravatar(self): + # construct the url + gravatar_url = "https://www.gravatar.com/avatar/" + hashlib.md5(self.user.email.lower()).hexdigest() + "?" + gravatar_url += urllib.urlencode({'d':'wavatar', 's':'50'}) + return gravatar_url + + + @property + def avatar_url(self): + if self.avatar_source is None or self.avatar_source is TWITTER: + if self.pic_url: + return self.pic_url + else: + return ANONYMOUS_AVATAR + elif self.avatar_source == GRAVATAR: + return self.gravatar() + elif self.avatar_source == FACEBOOK and self.facebook_id != None: + return 'https://graph.facebook.com/' + str(self.facebook_id) + '/picture' + else: + return ANONYMOUS_AVATAR @property def social_auths(self): @@ -1173,45 +1207,8 @@ class UserProfile(models.Model): auths[social.provider]=True return auths -#class CampaignSurveyResponse(models.Model): -# # generic -# campaign = models.ForeignKey("Campaign", related_name="surveyresponse", null=False) -# user = models.OneToOneField(User, related_name='surveyresponse') -# transaction = models.ForeignKey("payment.Transaction", null=True) -# # for OLA only -# premium = models.ForeignKey("Premium", null=True) -# anonymous = models.BooleanField(null=False) -# # relevant to all campaigns since these arise from acknowledgement requirements from generic premiums -# name = models.CharField(max_length=140, blank=True) -# url = models.URLField(blank=True) -# tagline = models.CharField(max_length=140, blank=True) -# # do we need to collect address for Rupert or will he do that once he has emails? - # this was causing a circular import problem and we do not seem to be using # anything from regluit.core.signals after this line # from regluit.core import signals from regluit.payment.manager import PaymentManager -from social_auth.signals import pre_update -from social_auth.backends.facebook import FacebookBackend -from social_auth.backends.twitter import TwitterBackend - -def facebook_extra_values(sender, user, response, details, **kwargs): - facebook_id = response.get('id') - user.profile.facebook_id = facebook_id - user.profile.pic_url = 'https://graph.facebook.com/' + facebook_id + '/picture' - user.profile.save() - return True - -def twitter_extra_values(sender, user, response, details, **kwargs): - import requests, urllib - - twitter_id = response.get('screen_name') - profile_image_url = response.get('profile_image_url_https') - user.profile.twitter_id = twitter_id - user.profile.pic_url = profile_image_url - user.profile.save() - return True - -pre_update.connect(facebook_extra_values, sender=FacebookBackend) -pre_update.connect(twitter_extra_values, sender=TwitterBackend) diff --git a/frontend/forms.py b/frontend/forms.py index f8888d6a..52bdc051 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -15,6 +15,7 @@ from selectable.forms import AutoCompleteSelectMultipleWidget,AutoCompleteSelect from selectable.forms import AutoCompleteSelectWidget,AutoCompleteSelectField from regluit.core.models import UserProfile, RightsHolder, Claim, Campaign, Premium, Ebook, Edition, PledgeExtra, Work +from regluit.core.models import TWITTER, FACEBOOK, GRAVATAR from regluit.core.lookups import OwnerLookup, WorkLookup from regluit.utils.localdatetime import now @@ -123,18 +124,41 @@ class RightsHolderForm(forms.ModelForm): except RightsHolder.DoesNotExist: return rights_holder_name raise forms.ValidationError(_("Another rights holder with that name already exists.")) + class ProfileForm(forms.ModelForm): clear_facebook=forms.BooleanField(required=False) clear_twitter=forms.BooleanField(required=False) clear_goodreads=forms.BooleanField(required=False) + class Meta: model = UserProfile - fields = 'tagline', 'librarything_id', 'home_url', 'clear_facebook', 'clear_twitter', 'clear_goodreads' + fields = 'tagline', 'librarything_id', 'home_url', 'clear_facebook', 'clear_twitter', 'clear_goodreads', 'avatar_source' widgets = { 'tagline': forms.Textarea(attrs={'rows': 5, 'onKeyUp': "counter(this, 140)", 'onBlur': "counter(this, 140)"}), } + def __init__(self, *args, **kwargs): + profile = kwargs.get('instance') + super(ProfileForm, self).__init__(*args, **kwargs) + choices = [] + for choice in self.fields['avatar_source'].choices : + if choice[0] == FACEBOOK and not profile.facebook_id: + pass + elif choice[0] == TWITTER and not profile.twitter_id: + pass + else: + choices.append(choice) + self.fields['avatar_source'].choices = choices + + def clean(self): + # check that if a social net is cleared, we're not using it a avatar source + if self.cleaned_data.get("clear_facebook", False) and self.cleaned_data.get("avatar_source", None)==FACEBOOK: + self.cleaned_data["avatar_source"]==GRAVATAR + if self.cleaned_data.get("clear_twitter", False) and self.cleaned_data.get("avatar_source", None)==TWITTER: + self.cleaned_data["avatar_source"]==GRAVATAR + return self.cleaned_data + class UserData(forms.Form): username = forms.RegexField( label=_("New Username"), diff --git a/frontend/templates/base.html b/frontend/templates/base.html index 6b1ce1de..c8100fdc 100644 --- a/frontend/templates/base.html +++ b/frontend/templates/base.html @@ -79,18 +79,14 @@ Hi, {{ user.username }}
  • - {% if user.profile.pic_url %} - Picture of {{ user.username }} - {% else %} - Generic Ungluer Avatar - {% endif %} + Avatar for {{ user.username }} {% if unseen_count %} {{ unseen_count }} {% endif %}
  • diff --git a/frontend/templates/comments.html b/frontend/templates/comments.html index ce293ab6..8bc5d83d 100644 --- a/frontend/templates/comments.html +++ b/frontend/templates/comments.html @@ -58,18 +58,14 @@
    cover image
    - {{ comment.user.username }} on {{ comment.content_object.title }}
    + {{ comment.user.username }} on {{ comment.content_object.title }}
    {{ comment.comment|linebreaksbr }}
    - - {% if supporter.profile.pic_url %} - Picture of {{ comment.user }} - {% else %} - Generic Ungluer Avatar - {% endif %} + + Avatar for {{ comment.user }}
    diff --git a/frontend/templates/comments/list.html b/frontend/templates/comments/list.html index dd574052..7807b39a 100644 --- a/frontend/templates/comments/list.html +++ b/frontend/templates/comments/list.html @@ -3,13 +3,9 @@
    - diff --git a/frontend/templates/comments/preview.html b/frontend/templates/comments/preview.html index 497e9412..1fc8ba8b 100644 --- a/frontend/templates/comments/preview.html +++ b/frontend/templates/comments/preview.html @@ -20,17 +20,16 @@ {% else %}

    {% trans "Preview your comment" %}

    -


    + + + Avatar for {{ user }} + + {{user.username }} + + ( [today's date] )
    + {{ comment|linebreaksbr }}
    +
    +


    {% trans "and" %} {% trans "or make changes" %}:

    diff --git a/frontend/templates/explore.html b/frontend/templates/explore.html index e6fc70e1..bdb0f334 100644 --- a/frontend/templates/explore.html +++ b/frontend/templates/explore.html @@ -46,13 +46,9 @@ Ungluers diff --git a/frontend/templates/home.html b/frontend/templates/home.html index 04dcecd9..f3107825 100755 --- a/frontend/templates/home.html +++ b/frontend/templates/home.html @@ -77,7 +77,7 @@ function put_un_in_cookie2(){ {% else %}
    - You are logged in as {{ request.user.username }}. + You are logged in as {{ request.user.username }}.
    {% endif %} @@ -154,7 +154,7 @@ function put_un_in_cookie2(){ {% else %}
    - You are logged in as {{ request.user.username }}. + You are logged in as {{ request.user.username }}.
    {% endif %} diff --git a/frontend/templates/notification/notice_template.html b/frontend/templates/notification/notice_template.html index 9e551585..4709be50 100644 --- a/frontend/templates/notification/notice_template.html +++ b/frontend/templates/notification/notice_template.html @@ -20,14 +20,10 @@ tweet, FB, email sharing show up. Include a {% url work your.local.work.id as w This is where you put your headline. Brief text, graphics-oriented info like user avatars and campaign status icons. e.g.: - - {% if supporter.profile.pic_url %} - Picture of {{ comment.user }} - {% else %} - Generic Ungluer Avatar - {% endif %} + + Avatar for {{ comment.user }} - {{ comment.user.username }} on {{ comment.content_object.title }}{% endcomment %}{% endblock %} + {{ comment.user.username }} on {{ comment.content_object.title }}{% endcomment %}{% endblock %}
    {% now "M d Y, h:i A"%}

    diff --git a/frontend/templates/notification/wishlist_message/notice.html b/frontend/templates/notification/wishlist_message/notice.html index 40e314d4..2f29a314 100644 --- a/frontend/templates/notification/wishlist_message/notice.html +++ b/frontend/templates/notification/wishlist_message/notice.html @@ -6,11 +6,7 @@ {% endblock %} {% block comments_graphical %} - {% if sender.profile.pic_url %} - Picture of {{ sender }} - {% else %} - Generic Ungluer Avatar - {% endif %} + Avatar for {{ sender }} Message from {{ sender.username }} {% if sender.is_staff %} (Unglue.it staff) {% endif %} on {{ work.title }} {% endblock %} diff --git a/frontend/templates/profiles/profile_detail.html b/frontend/templates/profiles/profile_detail.html index 5572444e..83ad4bc8 100644 --- a/frontend/templates/profiles/profile_detail.html +++ b/frontend/templates/profiles/profile_detail.html @@ -8,6 +8,6 @@

    Tagline: {{ profile.tagline }}

    Edit Again

    -

    {{user}}'s Unglue.it Page

    +

    {{user}}'s Unglue.it Page

    {% endblock content %} diff --git a/frontend/templates/registration/activate.html b/frontend/templates/registration/activate.html index faf5d0c0..9b2c927b 100644 --- a/frontend/templates/registration/activate.html +++ b/frontend/templates/registration/activate.html @@ -9,7 +9,7 @@ Oops – it seems that your activation key is invalid. Please check the url {% endif %} {% else %}
    -You are logged in as {{ request.user.username }}. +You are logged in as {{ request.user.username }}.
    {% endif %} {% endblock %} diff --git a/frontend/templates/registration/activation_complete.html b/frontend/templates/registration/activation_complete.html index dafaabd3..94b90938 100644 --- a/frontend/templates/registration/activation_complete.html +++ b/frontend/templates/registration/activation_complete.html @@ -14,7 +14,7 @@ {% else %}
    -You are already logged in as {{ user.username }}. +You are already logged in as {{ user.username }}.
    {% endif %} {% endblock %} diff --git a/frontend/templates/registration/login.html b/frontend/templates/registration/login.html index 377b2b06..70eb2a2c 100644 --- a/frontend/templates/registration/login.html +++ b/frontend/templates/registration/login.html @@ -27,7 +27,7 @@ Make sure the username box has your username, not your email -- some brow {% else %}
    -You are already logged in as {{ user.username }}. +You are already logged in as {{ user.username }}.
    {% endif %} diff --git a/frontend/templates/registration/registration_complete.html b/frontend/templates/registration/registration_complete.html index e385b9a8..b01c65d6 100644 --- a/frontend/templates/registration/registration_complete.html +++ b/frontend/templates/registration/registration_complete.html @@ -15,7 +15,7 @@ An account activation email has been sent. Please check your email and click on {% endif %} {% else %}
    -You are logged in as {{ request.user.username }}. +You are logged in as {{ request.user.username }}.
    {% endif %} diff --git a/frontend/templates/registration/registration_form.html b/frontend/templates/registration/registration_form.html index 2661276a..aa8d6b44 100644 --- a/frontend/templates/registration/registration_form.html +++ b/frontend/templates/registration/registration_form.html @@ -27,7 +27,7 @@ function put_un_in_cookie(){ {% else %}
    -You are already logged in as {{ user.username }}. +You are already logged in as {{ user.username }}.
    {% endif %} diff --git a/frontend/templates/rights_holders.html b/frontend/templates/rights_holders.html index 063bff6b..a720b993 100644 --- a/frontend/templates/rights_holders.html +++ b/frontend/templates/rights_holders.html @@ -24,7 +24,7 @@
    {{ rights_holder.rights_holder_name }}
    PSA #: {{ rights_holder.id }}
    email: {{ rights_holder.email }} -
    owner: {{ rights_holder.owner }}
    +
    owner: {{ rights_holder.owner }} {% empty %}

    No rights holders have been accepted yet

    {% endfor %} diff --git a/frontend/templates/search.html b/frontend/templates/search.html index 59077459..38224bfc 100644 --- a/frontend/templates/search.html +++ b/frontend/templates/search.html @@ -97,7 +97,7 @@ $j(document).ready(function() {
    {% for work in results %} - {% if not work.campaigns %} + {% if not work.last_campaign %}
    {% with work.googlebooks_id as googlebooks_id %} {% with 'yes' as on_search_page %} diff --git a/frontend/templates/supporter.html b/frontend/templates/supporter.html index c032ded7..ab65d623 100644 --- a/frontend/templates/supporter.html +++ b/frontend/templates/supporter.html @@ -98,11 +98,7 @@ there's no tab for seeing ALL my books, only the filters! huh.
    - {% if supporter.profile.pic_url %} - Picture of {{ supporter }} - {% else %} - Generic Ungluer Avatar - {% endif %} + Avatar for {{ supporter }} {{ supporter.username }} @@ -176,7 +172,11 @@ there's no tab for seeing ALL my books, only the filters! huh. {% csrf_token %}
    -

    Your Tagline

    + {{ profile_form.avatar_source.errors }} + Your profile pic: {{ profile_form.avatar_source }} {% ifequal supporter.profile.avatar_source 1 %}
    Set your Gravatar

    {% endifequal %} +
    +
    +

    Your Tagline

    {{ profile_form.tagline.errors }} {{ profile_form.tagline }}
    140 characters remaining

    @@ -186,7 +186,7 @@ there's no tab for seeing ALL my books, only the filters! huh.
    -

    Links

    +

    Your Links

    {{ profile_form.home_url }}{{ profile_form.home_url.errors }} diff --git a/frontend/templates/work.html b/frontend/templates/work.html index 1c070db9..d8f4afc0 100644 --- a/frontend/templates/work.html +++ b/frontend/templates/work.html @@ -217,13 +217,9 @@ {% for wish in work.wishes.all reversed %} {% with wish.wishlist.user as supporter %}
    - + - {% if supporter.profile.pic_url %} - Picture of {{ supporter }} - {% else %} - Generic Ungluer Avatar - {% endif %} + Avatar for {{ supporter }}
    @@ -245,13 +241,9 @@ {% for wish in work.wishes.all reversed %} {% with wish.wishlist.user as supporter %}
    - diff --git a/frontend/views.py b/frontend/views.py index b901a842..c1272bab 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -1459,8 +1459,12 @@ def supporter(request, supporter_username, template_name): if profile_form.cleaned_data['clear_facebook'] or profile_form.cleaned_data['clear_twitter'] or profile_form.cleaned_data['clear_goodreads'] : if profile_form.cleaned_data['clear_facebook']: profile_obj.facebook_id=0 + if profile_obj.avatar_source == models.FACEBOOK: + profile_obj.avatar_source = models.GRAVATAR if profile_form.cleaned_data['clear_twitter']: profile_obj.twitter_id="" + if profile_obj.avatar_source == models.TWITTER: + profile_obj.avatar_source = models.GRAVATAR if profile_form.cleaned_data['clear_goodreads']: profile_obj.goodreads_user_id = None profile_obj.goodreads_user_name = None diff --git a/settings/common.py b/settings/common.py index 1d1cabf7..046b3386 100644 --- a/settings/common.py +++ b/settings/common.py @@ -211,8 +211,10 @@ AUTHENTICATION_BACKENDS = ( ) SOCIAL_AUTH_ENABLED_BACKENDS = ['google', 'facebook', 'twitter'] -SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True +#SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/' +FACEBOOK_SOCIAL_AUTH_BACKEND_ERROR_URL = '/' +SOCIAL_AUTH_SLUGIFY_USERNAMES = True # following is needed because of length limitations in a unique constrain for MySQL # see https://github.com/omab/django-social-auth/issues/539 SOCIAL_AUTH_UID_LENGTH = 222 @@ -220,7 +222,17 @@ SOCIAL_AUTH_NONCE_SERVER_URL_LENGTH = 200 SOCIAL_AUTH_ASSOCIATION_SERVER_URL_LENGTH = 135 SOCIAL_AUTH_ASSOCIATION_HANDLE_LENGTH = 125 -TWITTER_EXTRA_DATA = [('profile_image_url', 'profile_image_url')] +SOCIAL_AUTH_PIPELINE = ( + 'regluit.core.auth.selectively_associate', + 'social_auth.backends.pipeline.associate.associate_by_email', + 'social_auth.backends.pipeline.user.get_username', + 'social_auth.backends.pipeline.user.create_user', + 'social_auth.backends.pipeline.social.associate_user', + 'regluit.core.auth.deliver_extra_data', + 'social_auth.backends.pipeline.user.update_user_details' +) + +TWITTER_EXTRA_DATA = [('profile_image_url_https', 'profile_image_url_https'),('screen_name','screen_name')] LOGIN_URL = "/accounts/superlogin/" LOGIN_REDIRECT_URL = "/"