/
views.py
315 lines (239 loc) · 9.96 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# -*- coding: utf-8 -*-
import arrow
from django.conf import settings
from django.http import JsonResponse
from django.shortcuts import render_to_response
from django.db import connection
from django.db import transaction
from django.db.models import Q
from django.db import DataError
from django.views.generic import View
from apps.config.models import Config
from apps.image.models import ForegroundCategory, ImageBackground, ImageForeground, ImagePairForCollect, ImagePairForDownload
from apps.image.forms import ImagePairForm
from project.exceptions import ProjectException
from project.errorcode import ErrorCode
def get_foregound_category(request):
# 获取前景分类
categories = ForegroundCategory.objects.all().order_by('order_id')
def _make_data(c):
return {
'ID': c.id,
'name_zh': c.name,
'name_en': c.name_en,
'icon': c.image_url,
}
data = [_make_data(c) for c in categories]
data = {
'ret': 0,
'data': data
}
return JsonResponse(data)
class ImageGetterView(object):
BUCKET_SIZE = 45
def __init__(self, request):
self.request = request
self.bucket = int(request.GET.get('bucket', 0))
self.category = int(request.GET.get('category', 0))
client_version = request.META.get('Client-Version', '')
if not client_version:
self.is_current_version = True
else:
self.is_current_version = Config.get_value('version') == client_version
if self.is_current_version:
self.hide_text = ''
else:
self.hide_text = "hide = 'FALSE' and"
if self.category == 1:
# all
self.category = 0
self.phone = request.session['phone']
if request.path.endswith('/hot/'):
self.order_by = 'score desc'
else:
self.order_by = 'upload_at desc'
if self.bucket < 0:
raise ProjectException(ErrorCode.REQUEST_ERROR)
self.now = arrow.utcnow().format('YYYY-MM-DD HH:mm:ssZ')
def build_sql_for_foreground(self):
if self.category:
sql = "select id, images->>%s as image_key from {0} where {1} show_at <= %s and (images->>%s) is not null and array[%s] <@ categories order by {2} offset %s limit %s".format(ImageForeground._meta.db_table, self.hide_text, self.order_by)
params = (self.phone, self.now, self.phone, self.category, self.bucket*self.BUCKET_SIZE, self.BUCKET_SIZE)
else:
sql = "select id, images->>%s as image_key from {0} where {1} show_at <= %s and (images->>%s) is not null order by {2} offset %s limit %s".format(ImageForeground._meta.db_table, self.hide_text, self.order_by)
params = (self.phone, self.now, self.phone, self.bucket*self.BUCKET_SIZE, self.BUCKET_SIZE)
return sql, params
def build_sql_for_background(self):
sql = "select id, images->>%s as image_key from {0} where {1} show_at <= %s and (images->>%s) is not null order by {2} offset %s limit %s".format(ImageBackground._meta.db_table, self.hide_text, self.order_by)
params = (self.phone, self.now, self.phone, self.bucket*self.BUCKET_SIZE, self.BUCKET_SIZE)
return sql, params
def get(self):
if self.request.path.startswith('/foreground/'):
sql, params = self.build_sql_for_foreground()
else:
sql, params = self.build_sql_for_background()
with connection.cursor() as c:
c.execute(sql, params)
result = c.fetchall()
if len(result) == self.BUCKET_SIZE:
next_bucket = self.bucket + 1
else:
next_bucket = None
def _make_image(record):
_id = str(record[0])
_url = settings.QINIU_DOMAIN + record[1]
return {
'ID': _id,
'url': _url,
}
images = [_make_image(record) for record in result]
data = {
'ret': 0,
'data': {
'next_bucket_id': next_bucket,
'images': images
}
}
return JsonResponse(data)
@classmethod
def as_view(cls):
def wrapper(request):
try:
self = cls(request)
except:
raise ProjectException(ErrorCode.REQUEST_ERROR)
return self.get()
return wrapper
class ImagePairView(View):
def get(self, request):
return render_to_response(
"test.html",
{
'action': request.path,
'form': ImagePairForm().as_p()
}
)
def post(self, request):
form = ImagePairForm(request.POST)
if not form.is_valid():
raise ProjectException(ErrorCode.REQUEST_ERROR)
form_data = form.cleaned_data
background_id = form_data['background']
foreground_id = form_data['foreground']
phone = request.session['phone']
try:
image_fore = ImageForeground.objects.get(id=foreground_id)
except ImageForeground.DoesNotExist:
raise ProjectException(ErrorCode.FOREGROUND_NOT_EXIST)
except DataError:
raise ProjectException(ErrorCode.REQUEST_ERROR)
try:
image_back = ImageBackground.objects.get(id=background_id)
except ImageBackground.DoesNotExist:
raise ProjectException(ErrorCode.BACKGROUND_NOT_EXIST)
except DataError:
raise ProjectException(ErrorCode.REQUEST_ERROR)
try:
foreground_key = image_fore.images[phone]
background_key = image_back.images[phone]
except KeyError:
print "==== ERROR ===="
print "can't find the key of this phone: {0}".format(phone)
print "foreground_id: {0}, background_id: {1}".format(foreground_id, background_id)
raise ProjectException(ErrorCode.REQUEST_ERROR)
if request.path == '/collect/':
self.post_collect(request, background_id, foreground_id, foreground_key, background_key)
elif request.path == '/uncollect/':
self.post_uncollect(request, background_id, foreground_id)
else:
self.post_download(request, background_id, foreground_id, foreground_key, background_key)
return JsonResponse({'ret': 0})
def post_collect(self, request, background_id, foreground_id, foreground_key, background_key):
# 收藏
udid = request.session['udid']
condition = Q(phone_udid=udid) & Q(background_id=background_id) & Q(foreground_id=foreground_id)
if ImagePairForCollect.objects.filter(condition).exists():
return
ImagePairForCollect.objects.create(
phone_udid=udid,
foreground_id=foreground_id,
background_id=background_id,
foreground_key=foreground_key,
background_key=background_key,
)
self.incr_background_score(background_id)
self.incr_foreground_score(foreground_id)
def post_uncollect(self, request, background_id, foreground_id):
# 取消收藏
udid = request.session['udid']
condition = Q(phone_udid=udid) & Q(background_id=background_id) & Q(foreground_id=foreground_id)
pair = ImagePairForCollect.objects.filter(condition)
if not pair.exists():
raise ProjectException(ErrorCode.COLLECTION_PAIR_NOT_EXIST)
pair.delete()
self.incr_background_score(background_id, value=-1)
self.incr_foreground_score(foreground_id, value=-1)
def post_download(self, request, background_id, foreground_id, foreground_key, background_key):
# 下载
udid = request.session['udid']
condition = Q(phone_udid=udid) & Q(background_id=background_id) & Q(foreground_id=foreground_id)
if ImagePairForDownload.objects.filter(condition).exists():
return
ImagePairForDownload.objects.create(
phone_udid=udid,
foreground_id=foreground_id,
background_id=background_id,
foreground_key=foreground_key,
background_key=background_key,
)
self.incr_background_score(background_id)
self.incr_foreground_score(foreground_id)
def incr_background_score(self, background_id, value=1):
with transaction.atomic():
image = ImageBackground.objects.select_for_update().get(id=background_id)
image.score += value
if image.score < 0:
image.score = 0
image.save()
def incr_foreground_score(self, foreground_id, value=1):
with transaction.atomic():
image = ImageForeground.objects.select_for_update().get(id=foreground_id)
image.score += value
if image.score < 0:
image.score = 0
image.save()
def show_collected_images(request):
# 获取收藏的前景/背景组合
# BUCKET_SIZE = 45
# try:
# bucket = int(request.GET.get('bucket', 0))
# except:
# raise ProjectException(ErrorCode.REQUEST_ERROR)
udid = request.session['udid']
# paris = ImagePairForCollect.objects.filter(phone_udid=udid).order_by('-action_date')[bucket*BUCKET_SIZE: (bucket+1)*BUCKET_SIZE]
paris = ImagePairForCollect.objects.filter(phone_udid=udid).order_by('-action_date')
# if paris.count < BUCKET_SIZE:
# next_bucket = None
# else:
# next_bucket = bucket + 1
next_bucket = None
def _make_item(p):
return {
'background': {
'ID': p.background_id,
'url': settings.QINIU_DOMAIN + p.background_key,
},
'foreground': {
'ID': p.foreground_id,
'url': settings.QINIU_DOMAIN + p.foreground_key,
}
}
items = [_make_item(p) for p in paris]
data = {
'ret': 0,
'data': {
'next_bucket_id': next_bucket,
'items': items
}
}
return JsonResponse(data)