Add photo_md5 for GroupPhotoInfo

Brightcells 8 年之前
父节点
当前提交
d824fbd441
共有 6 个文件被更改,包括 77 次插入43 次删除
  1. 27 22
      group/lensman_views.py
  2. 19 0
      group/migrations/0031_groupphotoinfo_photo_md5.py
  3. 2 0
      group/models.py
  4. 20 16
      group/views.py
  5. 1 1
      requirements.txt
  6. 8 4
      utils/storage_utils.py

+ 27 - 22
group/lensman_views.py

@@ -5,6 +5,7 @@ from __future__ import division
5 5
 from curtail_uuid import CurtailUUID
6 6
 from django.contrib.auth.hashers import check_password
7 7
 from django.db import transaction
8
+from filemd5 import calculate_md5
8 9
 from isoweek import Week
9 10
 from logit import logit
10 11
 from paginator import pagination
@@ -184,34 +185,38 @@ def lensman_photo_upload_api(request):
184 185
         photo.save()
185 186
 
186 187
         # 写 GroupPhotoInfo 表
187
-        group_photo = GroupPhotoInfo.objects.create(
188
+        group_photo, created = GroupPhotoInfo.objects.get_or_create(
188 189
             group_id=group_id,
189 190
             user_id=user_id,
190
-            nickname=user.final_nickname,
191
-            avatar=user.avatar,
192
-            photo_path=photo_info.photo_path,
193
-            photo_w=photo_info.photo_w,
194
-            photo_h=photo_info.photo_h,
195
-            photo_thumbnail_path=photo_info.photo_thumbnail_path,
196
-            photo_thumbnail_w=photo_info.photo_thumbnail_w,
197
-            photo_thumbnail_h=photo_info.photo_thumbnail_h,
198
-            photo_thumbnail2_path=photo_info.photo_thumbnail2_path,
199
-            photo_thumbnail2_w=photo_info.photo_thumbnail2_w,
200
-            photo_thumbnail2_h=photo_info.photo_thumbnail2_h,
201
-            photo_from=GroupPhotoInfo.SESSION_GROUP,
202
-            session_id=photo.session_id,
203
-            lensman_id=photo.lensman_id,
204
-            lensman_photo_id=photo.photo_id,
191
+            photo_md5=photo_info.photo_md5,
192
+            defaults={
193
+                'nickname': user.final_nickname,
194
+                'avatar': user.avatar,
195
+                'photo_path': photo_info.photo_path,
196
+                'photo_w': photo_info.photo_w,
197
+                'photo_h': photo_info.photo_h,
198
+                'photo_thumbnail_path': photo_info.photo_thumbnail_path,
199
+                'photo_thumbnail_w': photo_info.photo_thumbnail_w,
200
+                'photo_thumbnail_h': photo_info.photo_thumbnail_h,
201
+                'photo_thumbnail2_path': photo_info.photo_thumbnail2_path,
202
+                'photo_thumbnail2_w': photo_info.photo_thumbnail2_w,
203
+                'photo_thumbnail2_h': photo_info.photo_thumbnail2_h,
204
+                'photo_from': GroupPhotoInfo.SESSION_GROUP,
205
+                'session_id': photo.session_id,
206
+                'lensman_id': photo.lensman_id,
207
+                'lensman_photo_id': photo.photo_id,
208
+            }
205 209
         )
206 210
 
207
-        # 设置群组最后一张照片PK
208
-        r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk)
211
+        if created:
212
+            # 设置群组最后一张照片PK
213
+            r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk)
209 214
 
210
-        # 更新今日上传照片数量
211
-        r.incr(TODAY_UPLOAD_PHOTO_AMOUNT % (user_id, tc.local_string(format='%Y%m%d')))
215
+            # 更新今日上传照片数量
216
+            r.incr(TODAY_UPLOAD_PHOTO_AMOUNT % (user_id, tc.local_string(format='%Y%m%d')))
212 217
 
213
-        # Redis 群组数据缓存
214
-        set_group_info_by_id(group_id)
218
+            # Redis 群组数据缓存
219
+            set_group_info_by_id(group_id)
215 220
 
216 221
     return response(200, 'Lensman Upload Photo Success', u'摄影师照片上传成功', {
217 222
         'group_id': group_id,

+ 19 - 0
group/migrations/0031_groupphotoinfo_photo_md5.py

@@ -0,0 +1,19 @@
1
+# -*- coding: utf-8 -*-
2
+from __future__ import unicode_literals
3
+
4
+from django.db import models, migrations
5
+
6
+
7
+class Migration(migrations.Migration):
8
+
9
+    dependencies = [
10
+        ('group', '0030_groupinfo_gather_location'),
11
+    ]
12
+
13
+    operations = [
14
+        migrations.AddField(
15
+            model_name='groupphotoinfo',
16
+            name='photo_md5',
17
+            field=models.CharField(max_length=255, blank=True, help_text='\u7167\u7247 MD5', null=True, verbose_name='photo_md5', db_index=True),
18
+        ),
19
+    ]

+ 2 - 0
group/models.py

@@ -213,6 +213,8 @@ class GroupPhotoInfo(CreateUpdateMixin):
213 213
     nickname = models.CharField(_(u'nickname'), max_length=255, blank=True, null=True, help_text=u'用户群组昵称')
214 214
     avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'用户头像')
215 215
 
216
+    photo_md5 = models.CharField(_(u'photo_md5'), max_length=255, blank=True, null=True, help_text=u'照片 MD5', db_index=True)
217
+
216 218
     photo_path = models.CharField(_(u'photo_path'), max_length=255, blank=True, null=True, help_text=u'照片存放路径')
217 219
     photo_w = models.IntegerField(_(u'photo_w'), default=0, help_text=u'照片宽度')
218 220
     photo_h = models.IntegerField(_(u'photo_h'), default=0, help_text=u'照片高度')

+ 20 - 16
group/views.py

@@ -285,27 +285,31 @@ def flyimg_upload_api(request):
285 285
         photo_info = file_save(photo, prefix='fly', ext='jpeg', thumbnail=True)
286 286
 
287 287
         # 群组照片记录创建
288
-        group_photo = GroupPhotoInfo.objects.create(
288
+        group_photo, created = GroupPhotoInfo.objects.get_or_create(
289 289
             group_id=group_id,
290 290
             user_id=user_id,
291
-            nickname=nickname or user.final_nickname,
292
-            avatar=user.avatar,
293
-            photo_path=photo_info.photo_path,
294
-            photo_w=photo_info.photo_w,
295
-            photo_h=photo_info.photo_h,
296
-            photo_thumbnail_path=photo_info.photo_thumbnail_path,
297
-            photo_thumbnail_w=photo_info.photo_thumbnail_w,
298
-            photo_thumbnail_h=photo_info.photo_thumbnail_h,
299
-            photo_thumbnail2_path=photo_info.photo_thumbnail2_path,
300
-            photo_thumbnail2_w=photo_info.photo_thumbnail2_w,
301
-            photo_thumbnail2_h=photo_info.photo_thumbnail2_h,
291
+            photo_md5=photo_info.photo_md5,
292
+            defaults={
293
+                'nickname': nickname or user.final_nickname,
294
+                'avatar': user.avatar,
295
+                'photo_path': photo_info.photo_path,
296
+                'photo_w': photo_info.photo_w,
297
+                'photo_h': photo_info.photo_h,
298
+                'photo_thumbnail_path': photo_info.photo_thumbnail_path,
299
+                'photo_thumbnail_w': photo_info.photo_thumbnail_w,
300
+                'photo_thumbnail_h': photo_info.photo_thumbnail_h,
301
+                'photo_thumbnail2_path': photo_info.photo_thumbnail2_path,
302
+                'photo_thumbnail2_w': photo_info.photo_thumbnail2_w,
303
+                'photo_thumbnail2_h': photo_info.photo_thumbnail2_h,
304
+            }
302 305
         )
303 306
 
304
-        # 设置群组最后一张照片PK
305
-        r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk)
307
+        if created:
308
+            # 设置群组最后一张照片PK
309
+            r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk)
306 310
 
307
-        # Redis 群组数据缓存
308
-        set_group_info_by_id(group_id)
311
+            # Redis 群组数据缓存
312
+            set_group_info_by_id(group_id)
309 313
 
310 314
     curinfo = get_current_photos(group_id, user_id, group_user.current_id, request=request)
311 315
 

+ 1 - 1
requirements.txt

@@ -9,7 +9,7 @@ TimeConvert==1.3.12
9 9
 cryptography==1.5.2
10 10
 django-curtail-uuid==1.0.0
11 11
 django-detect==1.0.5
12
-django-file-md5==1.0.0
12
+django-file-md5==1.0.1
13 13
 django-ip==1.0.0
14 14
 django-json-response==1.1.4
15 15
 django-logit==1.0.6

+ 8 - 4
utils/storage_utils.py

@@ -29,12 +29,14 @@ def file_save(file_=None, file_path=None, prefix='img', ext='jpeg', watermark=Fa
29 29
     # Ext
30 30
     ext = os.path.splitext(file_.name)[-1] or ext
31 31
 
32
+    # Photo MD5
33
+    photo_md5 = calculate_md5(file_)
34
+
32 35
     # Photo UUID Get or Create
33
-    photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=calculate_md5(file_))
36
+    photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=photo_md5)
34 37
 
35 38
     # 照片路径
36
-    photo_path = photo.photo_path
37
-    if not photo_path:
39
+    if not photo.photo_path:
38 40
         photo_path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext)
39 41
         if default_storage.exists(photo_path):
40 42
             default_storage.delete(photo_path)
@@ -88,7 +90,9 @@ def file_save(file_=None, file_path=None, prefix='img', ext='jpeg', watermark=Fa
88 90
     return DotDict({
89 91
         'ext': ext,
90 92
 
91
-        'photo_path': photo_path,
93
+        'photo_md5': photo_md5,
94
+
95
+        'photo_path': photo.photo_path,
92 96
         'photo_w': photo.photo_w,
93 97
         'photo_h': photo.photo_h,
94 98