diff --git a/Makefile b/Makefile index cc8c8a1..09a55d1 100644 --- a/Makefile +++ b/Makefile @@ -35,4 +35,5 @@ test: jstest: @jasmine-splinter `pwd`/jstests/index.html - +update_schema: + @cd myimgat/ && python manage.py schemamigration wall --auto diff --git a/myimgat/apps/wall/migrations/0007_auto__add_field_croppedphoto_hash.py b/myimgat/apps/wall/migrations/0007_auto__add_field_croppedphoto_hash.py new file mode 100644 index 0000000..127404b --- /dev/null +++ b/myimgat/apps/wall/migrations/0007_auto__add_field_croppedphoto_hash.py @@ -0,0 +1,93 @@ +# encoding: 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 'CroppedPhoto.hash' + db.add_column('wall_croppedphoto', 'hash', self.gf('django.db.models.fields.CharField')(max_length=200, null=True, db_index=True), keep_default=False) + + + def backwards(self, orm): + + # Deleting field 'CroppedPhoto.hash' + db.delete_column('wall_croppedphoto', 'hash') + + + 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'}) + }, + 'wall.album': { + 'Meta': {'object_name': 'Album'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'identifier': ('django.db.models.fields.CharField', [], {'max_length': '200', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '4000', 'null': 'True', 'blank': 'True'}), + 'url': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '300', 'null': 'True', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100', 'db_index': 'True', 'blank': 'True'}) + }, + 'wall.croppedphoto': { + 'Meta': {'object_name': 'CroppedPhoto'}, + 'hash': ('django.db.models.fields.CharField', [], {'max_length': '200', 'null': 'True', 'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'original_photo': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'crops'", 'to': "orm['wall.Photo']"}), + 'url': ('django.db.models.fields.CharField', [], {'max_length': '500', 'db_index': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'cropped_photos'", 'null': 'True', 'to': "orm['auth.User']"}) + }, + 'wall.photo': { + 'Meta': {'object_name': 'Photo'}, + 'album': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'photos'", 'to': "orm['wall.Album']"}), + 'height': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'thumbnail': ('django.db.models.fields.CharField', [], {'max_length': '500', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '4000', 'null': 'True', 'blank': 'True'}), + 'url': ('django.db.models.fields.CharField', [], {'max_length': '500', 'db_index': 'True'}), + 'width': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}) + }, + 'wall.provider': { + 'Meta': {'object_name': 'Provider'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'provider_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'update_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'default': "'heynemann'", 'max_length': '100', 'db_index': 'True', 'blank': 'True'}) + } + } + + complete_apps = ['wall'] diff --git a/myimgat/apps/wall/models.py b/myimgat/apps/wall/models.py index f4bcbd3..6a91335 100644 --- a/myimgat/apps/wall/models.py +++ b/myimgat/apps/wall/models.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- import logging from datetime import datetime, timedelta +from os.path import splitext +from hashlib import md5 from django.db import models from django.conf import settings @@ -139,7 +141,11 @@ class CroppedPhoto(models.Model): original_photo = models.ForeignKey(Photo, related_name='crops') user = models.ForeignKey(User, blank=True, null=True, related_name='cropped_photos') url = models.CharField(max_length=500, db_index=True) + hash = models.CharField(max_length=200, null=True, db_index=True) + + def set_hash(self): + self.hash = md5(self.url).hexdigest() def get_absolute_url(self): - return reverse("cropped_photo_url", kwargs={'object_id': self.id}) + return "%s.%s" % (self.hash, splitext(self.url)[-1].lstrip('.')) diff --git a/myimgat/apps/wall/static/wall/js/crop-popin.js b/myimgat/apps/wall/static/wall/js/crop-popin.js index 17d0147..3d7df19 100644 --- a/myimgat/apps/wall/static/wall/js/crop-popin.js +++ b/myimgat/apps/wall/static/wall/js/crop-popin.js @@ -24,9 +24,10 @@ share: function(e) { e.preventDefault(); var data = Object.clone(this.shareButton.retrieve('crop-info')); + data["id"] = this.image.id; data[this.token.get('name')] = this.token.get('value'); - this.request.addEvent('success', function() { - console.log('sucesso'); + this.request.addEvent('success', function(url) { + console.log('url'); }).post(data); }, diff --git a/myimgat/apps/wall/static/wall/js/init.js b/myimgat/apps/wall/static/wall/js/init.js index a8092e0..ae50ade 100644 --- a/myimgat/apps/wall/static/wall/js/init.js +++ b/myimgat/apps/wall/static/wall/js/init.js @@ -10,6 +10,7 @@ }); cropPopin.addEvent('onCropActive', function(image){ this.element.getElement('h2').set('text', image.title); + cropPopin.image = image; var photoContainer = this.element.getElement('.photo'); var photo = new Element('img', { events: { diff --git a/myimgat/apps/wall/urls.py b/myimgat/apps/wall/urls.py index 135bd72..ad9a6b0 100644 --- a/myimgat/apps/wall/urls.py +++ b/myimgat/apps/wall/urls.py @@ -4,8 +4,8 @@ from django.conf.urls.defaults import patterns, url from django.views.generic.list_detail import object_detail -from wall.views import index, albums, save_cropped_photo -from wall.models import CroppedPhoto, Photo +from wall.views import index, albums, save_cropped_photo, shortened_url +from wall.models import Photo urlpatterns = patterns('', @@ -13,11 +13,7 @@ url('^api/shorten/?$', save_cropped_photo), url('^api/(?P[\w._-]+).(?P(json|jsonp))$', albums), - - url('^(?P\d+)[.](?:jpe?g|gif|png)$', object_detail, { - 'queryset': CroppedPhoto.objects.all(), - 'template_object_name': 'photo', - }, name="cropped_photo_url"), + url('^(?P.+?)[.](?:jpe?g|gif|png|JPE?G|GIF|PNG)$', shortened_url), url('^(?P[\w._-]+)$', index), diff --git a/myimgat/apps/wall/views.py b/myimgat/apps/wall/views.py index 34f39a1..ab44dde 100644 --- a/myimgat/apps/wall/views.py +++ b/myimgat/apps/wall/views.py @@ -2,10 +2,11 @@ # -*- coding: utf-8 -*- from json import dumps +from os.path import join from django.conf import settings from django.shortcuts import render -from django.http import Http404, HttpResponse +from django.http import Http404, HttpResponse, HttpResponsePermanentRedirect from providers.base import format_url @@ -41,6 +42,7 @@ def albums(request, username=None, extension="json"): } for photo in PhotoProxy.objects.load(album): album_data['photos'].append({ + 'id': photo.id, 'url': photo.url, 'title': photo.title, 'thumbnail': photo.thumbnail, @@ -59,9 +61,9 @@ def albums(request, username=None, extension="json"): raise Http404 @load_username -def save_cropped_photo(request): +def save_cropped_photo(request, username=None): identifier = request.POST['id'] - photo = Photo.objects.get(int(identifier)) + photo = Photo.objects.get(id=int(identifier)) left = request.POST['left'] top = request.POST['top'] @@ -81,4 +83,14 @@ def save_cropped_photo(request): url=url ) - return cropped.get_absolute_url() + cropped.set_hash() + cropped.save() + + return HttpResponse(request.build_absolute_uri('../' + cropped.get_absolute_url())) + +def shortened_url(request, image_hash): + obj = CroppedPhoto.objects.get(hash=image_hash) + if not obj: + raise Http404() + return HttpResponsePermanentRedirect(join(settings.THUMBOR_SERVER.rstrip('/'), obj.url.lstrip('/'))) + diff --git a/myimgat/settings.py b/myimgat/settings.py index 0e8f166..d70b438 100644 --- a/myimgat/settings.py +++ b/myimgat/settings.py @@ -153,7 +153,7 @@ # django-shorturls app settings SHORTEN_MODELS = { - 'P': 'wall.photo', + 'P': 'wall.cropped_photo', } # end of django-shorturl app settings diff --git a/myimgat/urls.py b/myimgat/urls.py index c4d25c1..faee9c9 100644 --- a/myimgat/urls.py +++ b/myimgat/urls.py @@ -8,7 +8,6 @@ urlpatterns = patterns('', url(r'', include('social_auth.urls')), - url(r's/', include('shorturls.urls')), url(r'^favicon.ico$', redirect_to, {'url': '%sfavicon.ico' % settings.STATIC_URL}), url(r'^logout/$', 'django.contrib.auth.views.logout', {"next_page": "/"}, name="logout"), url(r'^privacy-policy', redirect_to, {'url': '/'}),