From 2457f5e3e1d36374dd5485f8a9a3fb8aeedd422e Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Tue, 23 Apr 2013 14:18:45 +0100 Subject: [PATCH] Added processuser management command --- core/utils/url.py | 2 +- dss/settings.py | 11 +++++----- spa/api/v1/UserResource.py | 2 +- .../{processaudio.py => processmix.py} | 3 ++- spa/management/commands/processuser.py | 21 +++++++++++++++++++ spa/middleware/sqlprinter.py | 5 ++++- spa/models/Genre.py | 2 +- spa/models/Mix.py | 3 ++- spa/models/UserProfile.py | 18 ++++++++-------- spa/views.py | 6 ++---- static/js/app/views/user.js | 5 ++--- 11 files changed, 51 insertions(+), 27 deletions(-) rename spa/management/commands/{processaudio.py => processmix.py} (89%) create mode 100644 spa/management/commands/processuser.py diff --git a/core/utils/url.py b/core/utils/url.py index de87667..55da954 100644 --- a/core/utils/url.py +++ b/core/utils/url.py @@ -49,7 +49,7 @@ def unique_slugify(instance, value, slug_field_name='slug', queryset=None, slug_ slug = '%s%s' % (slug, end) next += 1 - setattr(instance, slug_field.attname, slug) + return slug def _slug_strip(value, separator='-'): diff --git a/dss/settings.py b/dss/settings.py index c2beedf..2c4a923 100644 --- a/dss/settings.py +++ b/dss/settings.py @@ -133,8 +133,8 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'spa.middleware.uploadify.SWFUploadMiddleware', - #'spa.middleware.sqlprinter.SqlPrintingMiddleware', - #'debug_toolbar.middleware.DebugToolbarMiddleware', + 'spa.middleware.sqlprinter.SqlPrintingMiddleware', + 'debug_toolbar.middleware.DebugToolbarMiddleware', ) WSGI_APPLICATION = 'dss.wsgi.application' @@ -166,6 +166,7 @@ INSTALLED_APPS = ( 'allauth.socialaccount.providers.facebook', 'allauth.socialaccount.providers.twitter', 'allauth.socialaccount.providers.google', + 'debug_toolbar' #'backbone_tastypie', ) @@ -226,9 +227,9 @@ PIPELINE_CSS = { }, }, } -INTERNAL_IPS = ('127.0.0.1', '86.44.166.21') +INTERNAL_IPS = ('127.0.0.1', '86.44.166.21', '192.168.1.111') + GOOGLE_ANALYTICS_CODE = localsettings.GOOGLE_ANALYTICS_CODE -#TASTYPIE_DATETIME_FORMATTING = 'iso-8601' TASTYPIE_DATETIME_FORMATTING = 'rfc-2822' SENDFILE_BACKEND = localsettings.SENDFILE_BACKEND @@ -256,4 +257,4 @@ import mimetypes mimetypes.add_type("text/xml", ".plist", False) -#HTML_MINIFY = not localsettings.DEBUG +HTML_MINIFY = not localsettings.DEBUG diff --git a/spa/api/v1/UserResource.py b/spa/api/v1/UserResource.py index 6e7c9d7..a2096ce 100644 --- a/spa/api/v1/UserResource.py +++ b/spa/api/v1/UserResource.py @@ -33,7 +33,7 @@ class UserResource(BackboneCompatibleResource): def dehydrate(self, bundle): #set the "me" user only properties - if bundle.obj.id == bundle.request.user.id: + if bundle.obj.user.id == bundle.request.user.id: bundle.data['email'] = bundle.obj.email if bundle.obj.activity_sharing is not None: bundle.data['activity_share_likes'] = \ diff --git a/spa/management/commands/processaudio.py b/spa/management/commands/processmix.py similarity index 89% rename from spa/management/commands/processaudio.py rename to spa/management/commands/processmix.py index e3ed368..81be1ca 100644 --- a/spa/management/commands/processaudio.py +++ b/spa/management/commands/processmix.py @@ -2,6 +2,7 @@ from django.core.management.base import NoArgsCommand, CommandError from django.template.defaultfilters import slugify from core.utils.audio import Mp3FileNotFoundException from core.utils.audio.mp3 import mp3_length +from core.utils.url import unique_slugify from spa.models import Mix @@ -20,7 +21,7 @@ class Command(NoArgsCommand): mix.duration = length if mix.slug == 'Invalid': print "Slugifying mix: %s" % mix.title - mix.slug = slugify(mix.title) + mix.slug = unique_slugify(mix, mix.title) print "\tNew title: %s" % mix.slug mix.save() except Mp3FileNotFoundException, me: diff --git a/spa/management/commands/processuser.py b/spa/management/commands/processuser.py new file mode 100644 index 0000000..1ae74c7 --- /dev/null +++ b/spa/management/commands/processuser.py @@ -0,0 +1,21 @@ +from django.contrib.auth.models import User +from django.core.exceptions import ObjectDoesNotExist +from django.core.management.base import NoArgsCommand, CommandError +from spa.models import UserProfile + + +class Command(NoArgsCommand): + help = "Updates audio files with their durations" + + def handle(self, *args, **options): + try: + candidates = User.objects.all() + for user in candidates: + try: + profile = user.get_profile() + except ObjectDoesNotExist, ce: + print "Creating profile for %s" % user.get_username() + UserProfile.objects.create(user=user) + user.save() + except Exception, ex: + raise CommandError(ex.message) \ No newline at end of file diff --git a/spa/middleware/sqlprinter.py b/spa/middleware/sqlprinter.py index 6286412..d2d5d80 100644 --- a/spa/middleware/sqlprinter.py +++ b/spa/middleware/sqlprinter.py @@ -36,6 +36,9 @@ class SqlPrintingMiddleware(object): """ def process_response(self, request, response): + if not settings.DEBUG: + return + indentation = 2 if len(connection.queries) > 0 and settings.DEBUG: width = terminal_width() @@ -43,7 +46,7 @@ class SqlPrintingMiddleware(object): for query in connection.queries: nice_sql = query['sql'].replace('"', '').replace(',', ', ') sql = "\033[1;31m[%s]\033[0m %s" % (query['time'], nice_sql) - total_time = total_time + float(query['time']) + total_time += float(query['time']) while len(sql) > width - indentation: print "%s%s" % (" " * indentation, sql[:width - indentation]) sql = sql[width - indentation:] diff --git a/spa/models/Genre.py b/spa/models/Genre.py index 476abe6..f4030c1 100644 --- a/spa/models/Genre.py +++ b/spa/models/Genre.py @@ -11,7 +11,7 @@ class Genre(_BaseModel): def save(self, force_insert=False, force_update=False, using=None): if not self.slug: - unique_slugify(self, self.description, slug_separator='_') + self.description = unique_slugify(self, self.description, slug_separator='_') super(Genre, self).save(force_insert, force_update, using) diff --git a/spa/models/Mix.py b/spa/models/Mix.py index 9070372..ea8b214 100644 --- a/spa/models/Mix.py +++ b/spa/models/Mix.py @@ -10,6 +10,7 @@ from django.db import models from django.db.models import Count from core.utils import url +from core.utils.url import unique_slugify from spa.models.Genre import Genre from spa.models.MixPlay import MixPlay from spa.models.MixDownload import MixDownload @@ -55,7 +56,7 @@ class Mix(_BaseModel): def save(self, force_insert=False, force_update=False, using=None): if not self.id: - self.slug = slugify(self.title) + self.slug = unique_slugify(self, self.title) #TODO #turn away now - horrid hack to strip media root url diff --git a/spa/models/UserProfile.py b/spa/models/UserProfile.py index e58ef53..ca5da88 100644 --- a/spa/models/UserProfile.py +++ b/spa/models/UserProfile.py @@ -1,7 +1,6 @@ import urlparse from django.contrib.auth.models import User -from django.contrib.sites.models import Site from django.core.exceptions import SuspiciousOperation from django.db import models from django.db.models.signals import post_save @@ -57,14 +56,6 @@ class UserProfile(_BaseModel): return super(UserProfile, self).save(force_insert, force_update, using) - """ - def create_user_profile(sender, instance, created, **kwargs): - if created: - UserProfile.objects.create(user=instance) - - post_save.connect(create_user_profile, sender=User) - """ - def get_username(self): return self.user.username @@ -149,3 +140,12 @@ class UserProfile(_BaseModel): @classmethod def get_default_avatar_image(cls): return urlparse.urljoin(settings.STATIC_URL, "img/default-avatar-32.png") + + +def create_user_profile(sender, instance, created, **kwargs): + if created: + UserProfile.objects.create(user=instance) + + +post_save.connect(create_user_profile, sender=User) + diff --git a/spa/views.py b/spa/views.py index f9dbede..6c3dde7 100644 --- a/spa/views.py +++ b/spa/views.py @@ -24,10 +24,8 @@ def app(request): def default(request): - if 'HTTP_USER_AGENT' in request.META: - if request.META['HTTP_USER_AGENT'].startswith('facebookexternalhit'): - logger.debug("Redirecting facebook hit") - return social_redirect(request) + if 'HTTP_USER_AGENT' in request.META and request.META['HTTP_USER_AGENT'].startswith('facebookexternalhit'): + return social_redirect(request) backbone_url = "http://%s/#%s" % (request.get_host(), rreplace(lreplace(request.path, '/', ''), '/', '')) return redirect(backbone_url) diff --git a/static/js/app/views/user.js b/static/js/app/views/user.js index 6c86bb4..60cab8a 100644 --- a/static/js/app/views/user.js +++ b/static/js/app/views/user.js @@ -51,11 +51,10 @@ window.UserView = DSSEditableView.extend({ this._saveChanges({ success:function () { com.podnoms.utils.showAlert("Success", "Successfully updated yourself"); - Backbone.history.navigate('/me', {trigger:true}); + Backbone.history.navigate('/', {trigger:true}); }, error:function () { - com.podnoms.utils.showAlert("Success", "Successfully updated yourself"); - alert("Error"); + com.podnoms.utils.showError("Error", "There was an error updating your info. Please try again later."); } }); return false;