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 }}
{% 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 %}Tagline: {{ profile.tagline }}
- + {% 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 %}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() {Your Tagline
{{ profile_form.tagline.errors }} {{ profile_form.tagline }}
{% trans "Preview your comment" %}
{{ comment|linebreaksbr }}
-
+ + + + + {{user.username }} + + ( [today's date] )
+ {{ comment|linebreaksbr }}
+