Skip to content

Commit 10ded9b

Browse files
committed
Shortening working
1 parent ab8a541 commit 10ded9b

File tree

9 files changed

+126
-17
lines changed

9 files changed

+126
-17
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ test:
3535
jstest:
3636
@jasmine-splinter `pwd`/jstests/index.html
3737

38-
38+
update_schema:
39+
@cd myimgat/ && python manage.py schemamigration wall --auto
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# encoding: utf-8
2+
import datetime
3+
from south.db import db
4+
from south.v2 import SchemaMigration
5+
from django.db import models
6+
7+
class Migration(SchemaMigration):
8+
9+
def forwards(self, orm):
10+
11+
# Adding field 'CroppedPhoto.hash'
12+
db.add_column('wall_croppedphoto', 'hash', self.gf('django.db.models.fields.CharField')(max_length=200, null=True, db_index=True), keep_default=False)
13+
14+
15+
def backwards(self, orm):
16+
17+
# Deleting field 'CroppedPhoto.hash'
18+
db.delete_column('wall_croppedphoto', 'hash')
19+
20+
21+
models = {
22+
'auth.group': {
23+
'Meta': {'object_name': 'Group'},
24+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
25+
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
26+
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
27+
},
28+
'auth.permission': {
29+
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
30+
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
31+
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
32+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
33+
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
34+
},
35+
'auth.user': {
36+
'Meta': {'object_name': 'User'},
37+
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
38+
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
39+
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
40+
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
41+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
42+
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
43+
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
44+
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
45+
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
46+
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
47+
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
48+
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
49+
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
50+
},
51+
'contenttypes.contenttype': {
52+
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
53+
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
54+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
55+
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
56+
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
57+
},
58+
'wall.album': {
59+
'Meta': {'object_name': 'Album'},
60+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
61+
'identifier': ('django.db.models.fields.CharField', [], {'max_length': '200', 'db_index': 'True'}),
62+
'title': ('django.db.models.fields.CharField', [], {'max_length': '4000', 'null': 'True', 'blank': 'True'}),
63+
'url': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '300', 'null': 'True', 'blank': 'True'}),
64+
'username': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100', 'db_index': 'True', 'blank': 'True'})
65+
},
66+
'wall.croppedphoto': {
67+
'Meta': {'object_name': 'CroppedPhoto'},
68+
'hash': ('django.db.models.fields.CharField', [], {'max_length': '200', 'null': 'True', 'db_index': 'True'}),
69+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
70+
'original_photo': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'crops'", 'to': "orm['wall.Photo']"}),
71+
'url': ('django.db.models.fields.CharField', [], {'max_length': '500', 'db_index': 'True'}),
72+
'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'cropped_photos'", 'null': 'True', 'to': "orm['auth.User']"})
73+
},
74+
'wall.photo': {
75+
'Meta': {'object_name': 'Photo'},
76+
'album': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'photos'", 'to': "orm['wall.Album']"}),
77+
'height': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
78+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
79+
'thumbnail': ('django.db.models.fields.CharField', [], {'max_length': '500', 'db_index': 'True'}),
80+
'title': ('django.db.models.fields.CharField', [], {'max_length': '4000', 'null': 'True', 'blank': 'True'}),
81+
'url': ('django.db.models.fields.CharField', [], {'max_length': '500', 'db_index': 'True'}),
82+
'width': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'})
83+
},
84+
'wall.provider': {
85+
'Meta': {'object_name': 'Provider'},
86+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
87+
'provider_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
88+
'update_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
89+
'username': ('django.db.models.fields.CharField', [], {'default': "'heynemann'", 'max_length': '100', 'db_index': 'True', 'blank': 'True'})
90+
}
91+
}
92+
93+
complete_apps = ['wall']

myimgat/apps/wall/models.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# -*- coding: utf-8 -*-
33
import logging
44
from datetime import datetime, timedelta
5+
from os.path import splitext
6+
from hashlib import md5
57

68
from django.db import models
79
from django.conf import settings
@@ -139,7 +141,11 @@ class CroppedPhoto(models.Model):
139141
original_photo = models.ForeignKey(Photo, related_name='crops')
140142
user = models.ForeignKey(User, blank=True, null=True, related_name='cropped_photos')
141143
url = models.CharField(max_length=500, db_index=True)
144+
hash = models.CharField(max_length=200, null=True, db_index=True)
145+
146+
def set_hash(self):
147+
self.hash = md5(self.url).hexdigest()
142148

143149
def get_absolute_url(self):
144-
return reverse("cropped_photo_url", kwargs={'object_id': self.id})
150+
return "%s.%s" % (self.hash, splitext(self.url)[-1].lstrip('.'))
145151

myimgat/apps/wall/static/wall/js/crop-popin.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
share: function(e) {
2525
e.preventDefault();
2626
var data = Object.clone(this.shareButton.retrieve('crop-info'));
27+
data["id"] = this.image.id;
2728
data[this.token.get('name')] = this.token.get('value');
28-
this.request.addEvent('success', function() {
29-
console.log('sucesso');
29+
this.request.addEvent('success', function(url) {
30+
console.log('url');
3031
}).post(data);
3132
},
3233

myimgat/apps/wall/static/wall/js/init.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
});
1111
cropPopin.addEvent('onCropActive', function(image){
1212
this.element.getElement('h2').set('text', image.title);
13+
cropPopin.image = image;
1314
var photoContainer = this.element.getElement('.photo');
1415
var photo = new Element('img', {
1516
events: {

myimgat/apps/wall/urls.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@
44
from django.conf.urls.defaults import patterns, url
55
from django.views.generic.list_detail import object_detail
66

7-
from wall.views import index, albums, save_cropped_photo
8-
from wall.models import CroppedPhoto, Photo
7+
from wall.views import index, albums, save_cropped_photo, shortened_url
8+
from wall.models import Photo
99

1010
urlpatterns = patterns('',
1111

1212
url('^$', index),
1313

1414
url('^api/shorten/?$', save_cropped_photo),
1515
url('^api/(?P<username>[\w._-]+).(?P<extension>(json|jsonp))$', albums),
16-
17-
url('^(?P<object_id>\d+)[.](?:jpe?g|gif|png)$', object_detail, {
18-
'queryset': CroppedPhoto.objects.all(),
19-
'template_object_name': 'photo',
20-
}, name="cropped_photo_url"),
16+
url('^(?P<image_hash>.+?)[.](?:jpe?g|gif|png|JPE?G|GIF|PNG)$', shortened_url),
2117

2218
url('^(?P<username>[\w._-]+)$', index),
2319

myimgat/apps/wall/views.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# -*- coding: utf-8 -*-
33

44
from json import dumps
5+
from os.path import join
56

67
from django.conf import settings
78
from django.shortcuts import render
8-
from django.http import Http404, HttpResponse
9+
from django.http import Http404, HttpResponse, HttpResponsePermanentRedirect
910

1011
from providers.base import format_url
1112

@@ -41,6 +42,7 @@ def albums(request, username=None, extension="json"):
4142
}
4243
for photo in PhotoProxy.objects.load(album):
4344
album_data['photos'].append({
45+
'id': photo.id,
4446
'url': photo.url,
4547
'title': photo.title,
4648
'thumbnail': photo.thumbnail,
@@ -59,9 +61,9 @@ def albums(request, username=None, extension="json"):
5961
raise Http404
6062

6163
@load_username
62-
def save_cropped_photo(request):
64+
def save_cropped_photo(request, username=None):
6365
identifier = request.POST['id']
64-
photo = Photo.objects.get(int(identifier))
66+
photo = Photo.objects.get(id=int(identifier))
6567

6668
left = request.POST['left']
6769
top = request.POST['top']
@@ -81,4 +83,14 @@ def save_cropped_photo(request):
8183
url=url
8284
)
8385

84-
return cropped.get_absolute_url()
86+
cropped.set_hash()
87+
cropped.save()
88+
89+
return HttpResponse(request.build_absolute_uri('../' + cropped.get_absolute_url()))
90+
91+
def shortened_url(request, image_hash):
92+
obj = CroppedPhoto.objects.get(hash=image_hash)
93+
if not obj:
94+
raise Http404()
95+
return HttpResponsePermanentRedirect(join(settings.THUMBOR_SERVER.rstrip('/'), obj.url.lstrip('/')))
96+

myimgat/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153

154154
# django-shorturls app settings
155155
SHORTEN_MODELS = {
156-
'P': 'wall.photo',
156+
'P': 'wall.cropped_photo',
157157
}
158158
# end of django-shorturl app settings
159159

myimgat/urls.py

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
urlpatterns = patterns('',
1010
url(r'', include('social_auth.urls')),
11-
url(r's/', include('shorturls.urls')),
1211
url(r'^favicon.ico$', redirect_to, {'url': '%sfavicon.ico' % settings.STATIC_URL}),
1312
url(r'^logout/$', 'django.contrib.auth.views.logout', {"next_page": "/"}, name="logout"),
1413
url(r'^privacy-policy', redirect_to, {'url': '/'}),

0 commit comments

Comments
 (0)