Skip to content

Commit 3d8d4df

Browse files
author
Hooram Nam
committed
Add list views with caching
1 parent e50df63 commit 3d8d4df

File tree

7 files changed

+211
-38
lines changed

7 files changed

+211
-38
lines changed

api/face_classify.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ def train_faces():
115115
face.person_label_is_inferred = True
116116
face.save()
117117

118-
print(list(zip(face_ids_unknown,pred)))
119-
120118

121119
# for front end cluster visualization
122120
faces = Face.objects.all()
@@ -148,4 +146,4 @@ def train_faces():
148146

149147

150148
if __name__ == "__main__":
151-
res=train_faces()
149+
res=train_faces()

api/serializers.py

+101-13
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ def extract_date(entity):
155155

156156
from itertools import groupby
157157

158+
159+
160+
161+
158162
class AlbumDateSerializer(serializers.ModelSerializer):
159163
photos = PhotoSerializer(many=True, read_only=True)
160164

@@ -166,6 +170,45 @@ class Meta:
166170
"date",
167171
"photos")
168172

173+
class AlbumDateListSerializer(serializers.ModelSerializer):
174+
# photos = PhotoSerializer(many=True, read_only=True)
175+
people = serializers.SerializerMethodField()
176+
cover_photo_url = serializers.SerializerMethodField()
177+
photo_count = serializers.SerializerMethodField()
178+
179+
class Meta:
180+
model = AlbumDate
181+
fields = (
182+
"id",
183+
"people",
184+
"cover_photo_url",
185+
"title",
186+
"photo_count",
187+
"date")
188+
189+
def get_photo_count(self,obj):
190+
return obj.photos.count()
191+
192+
def get_cover_photo_url(self,obj):
193+
first_photo = obj.photos.first()
194+
return first_photo.image.url
195+
196+
def get_people(self,obj):
197+
# ipdb.set_trace()
198+
photos = obj.photos.all()
199+
res = []
200+
for photo in photos:
201+
faces = photo.faces.all()
202+
for face in faces:
203+
serialized_person = PersonSerializer(face.person).data
204+
if serialized_person not in res:
205+
res.append(serialized_person)
206+
return res
207+
208+
209+
210+
211+
169212
class AlbumPersonSerializer(serializers.ModelSerializer):
170213
# faces = FaceSerializer(many=True, read_only=False)
171214
# faces = serializers.StringRelatedField(many=True)
@@ -185,6 +228,7 @@ def get_photos(self,obj):
185228
res.append(PhotoSerializer(face.photo).data)
186229
return res
187230

231+
# todo: remove this unecessary thing
188232
def get_people(self,obj):
189233
faces = obj.faces.all()
190234
res = []
@@ -194,6 +238,45 @@ def get_people(self,obj):
194238
res.append(serialized_person)
195239
return res
196240

241+
242+
class AlbumPersonListSerializer(serializers.ModelSerializer):
243+
# faces = FaceSerializer(many=True, read_only=False)
244+
# faces = serializers.StringRelatedField(many=True)
245+
# people = serializers.SerializerMethodField()
246+
photo_count = serializers.SerializerMethodField()
247+
cover_photo_url = serializers.SerializerMethodField()
248+
class Meta:
249+
model = Person
250+
fields = ('name',
251+
"photo_count",
252+
"cover_photo_url",
253+
# 'people',
254+
'id',)
255+
# 'faces')
256+
257+
def get_photo_count(self,obj):
258+
return obj.faces.count()
259+
260+
def get_cover_photo_url(self,obj):
261+
first_face = obj.faces.first()
262+
if first_face:
263+
return first_face.photo.image.url
264+
else:
265+
return None
266+
267+
def get_face_photo_url(self,obj):
268+
first_face = obj.faces.first()
269+
if first_face:
270+
return first_face.image.url
271+
else:
272+
return None
273+
274+
275+
276+
277+
278+
279+
197280
class AlbumAutoSerializer(serializers.ModelSerializer):
198281
photos = PhotoSerializer(many=True, read_only=True)
199282
people = serializers.SerializerMethodField()
@@ -224,8 +307,9 @@ def get_people(self,obj):
224307

225308

226309
class AlbumAutoListSerializer(serializers.ModelSerializer):
227-
# people = serializers.SerializerMethodField()
310+
people = serializers.SerializerMethodField()
228311
cover_photo_url = serializers.SerializerMethodField()
312+
photo_count = serializers.SerializerMethodField()
229313

230314
class Meta:
231315
model = AlbumAuto
@@ -235,22 +319,26 @@ class Meta:
235319
"timestamp",
236320
"created_on",
237321
"cover_photo_url",
322+
"photo_count",
238323
"gps_lat",
239-
# 'people',
324+
'people',
240325
"gps_lon")
241326

327+
def get_photo_count(self,obj):
328+
return obj.photos.count()
329+
242330
def get_cover_photo_url(self,obj):
243331
first_photo = obj.photos.first()
244332
return first_photo.image.url
245333

246-
# def get_people(self,obj):
247-
# # ipdb.set_trace()
248-
# photos = obj.photos.all()
249-
# res = []
250-
# for photo in photos:
251-
# faces = photo.faces.all()
252-
# for face in faces:
253-
# serialized_person = PersonSerializer(face.person).data
254-
# if serialized_person not in res:
255-
# res.append(serialized_person)
256-
# return res
334+
def get_people(self,obj):
335+
# ipdb.set_trace()
336+
photos = obj.photos.all()
337+
res = []
338+
for photo in photos:
339+
faces = photo.faces.all()
340+
for face in faces:
341+
serialized_person = PersonSerializer(face.person).data
342+
if serialized_person not in res:
343+
res.append(serialized_person)
344+
return res

api/views.py

+63-20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515

1616
from api.serializers import AlbumAutoListSerializer
17+
from api.serializers import AlbumPersonListSerializer
18+
from api.serializers import AlbumDateListSerializer
1719

1820

1921
from api.face_classify import train_faces, cluster_faces
@@ -43,6 +45,9 @@
4345
PaginationKeyBit
4446
)
4547

48+
# CACHE_TTL = 60 * 60 * 24 # 1 day
49+
CACHE_TTL = 60 * 60 * 24 # 1 min
50+
4651
#caching stuff straight out of https://chibisov.github.io/drf-extensions/docs/#caching
4752
class UpdatedAtKeyBit(KeyBitBase):
4853
def get_data(self, **kwargs):
@@ -78,11 +83,11 @@ class PhotoViewSet(viewsets.ModelViewSet):
7883
serializer_class = PhotoSerializer
7984
pagination_class = StandardResultsSetPagination
8085

81-
@cache_response(key_func=CustomObjectKeyConstructor())
86+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
8287
def retrieve(self, *args, **kwargs):
8388
return super(PhotoViewSet, self).retrieve(*args, **kwargs)
8489

85-
@cache_response(key_func=CustomListKeyConstructor())
90+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
8691
def list(self, *args, **kwargs):
8792
return super(PhotoViewSet, self).list(*args, **kwargs)
8893

@@ -91,11 +96,11 @@ class FaceViewSet(viewsets.ModelViewSet):
9196
serializer_class = FaceSerializer
9297
pagination_class = StandardResultsSetPagination
9398

94-
@cache_response(key_func=CustomObjectKeyConstructor())
99+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
95100
def retrieve(self, *args, **kwargs):
96101
return super(FaceViewSet, self).retrieve(*args, **kwargs)
97102

98-
@cache_response(key_func=CustomListKeyConstructor())
103+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
99104
def list(self, *args, **kwargs):
100105
return super(FaceViewSet, self).list(*args, **kwargs)
101106

@@ -106,11 +111,11 @@ class FaceInferredViewSet(viewsets.ModelViewSet):
106111
serializer_class = FaceSerializer
107112
pagination_class = StandardResultsSetPagination
108113

109-
@cache_response(key_func=CustomObjectKeyConstructor())
114+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
110115
def retrieve(self, *args, **kwargs):
111116
return super(FaceInferredViewSet, self).retrieve(*args, **kwargs)
112117

113-
@cache_response(key_func=CustomListKeyConstructor())
118+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
114119
def list(self, *args, **kwargs):
115120
return super(FaceInferredViewSet, self).list(*args, **kwargs)
116121

@@ -120,11 +125,11 @@ class FaceLabeledViewSet(viewsets.ModelViewSet):
120125
serializer_class = FaceSerializer
121126
pagination_class = StandardResultsSetPagination
122127

123-
@cache_response(key_func=CustomObjectKeyConstructor())
128+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
124129
def retrieve(self, *args, **kwargs):
125130
return super(FaceLabeledViewSet, self).retrieve(*args, **kwargs)
126131

127-
@cache_response(key_func=CustomListKeyConstructor())
132+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
128133
def list(self, *args, **kwargs):
129134
return super(FaceLabeledViewSet, self).list(*args, **kwargs)
130135

@@ -135,74 +140,112 @@ class PersonViewSet(viewsets.ModelViewSet):
135140
serializer_class = PersonSerializer
136141
pagination_class = StandardResultsSetPagination
137142

138-
@cache_response(key_func=CustomObjectKeyConstructor())
143+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
139144
def retrieve(self, *args, **kwargs):
140145
return super(PersonViewSet, self).retrieve(*args, **kwargs)
141146

142-
@cache_response(key_func=CustomListKeyConstructor())
147+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
143148
def list(self, *args, **kwargs):
144149
return super(PersonViewSet, self).list(*args, **kwargs)
145150

146151

152+
153+
154+
155+
147156
class AlbumAutoViewSet(viewsets.ModelViewSet):
148157
queryset = AlbumAuto.objects.all().order_by('-timestamp')
149158
serializer_class = AlbumAutoSerializer
150159
pagination_class = StandardResultsSetPagination
151160

152-
@cache_response(key_func=CustomObjectKeyConstructor())
161+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
153162
def retrieve(self, *args, **kwargs):
154163
return super(AlbumAutoViewSet, self).retrieve(*args, **kwargs)
155164

156-
@cache_response(key_func=CustomListKeyConstructor())
165+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
157166
def list(self, *args, **kwargs):
158167
return super(AlbumAutoViewSet, self).list(*args, **kwargs)
159168

160-
161-
162169
class AlbumAutoListViewSet(viewsets.ModelViewSet):
163170
queryset = AlbumAuto.objects.all().order_by('-timestamp')
164171
serializer_class = AlbumAutoListSerializer
165172
pagination_class = StandardResultsSetPagination
166173

167-
@cache_response(key_func=CustomObjectKeyConstructor())
174+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
168175
def retrieve(self, *args, **kwargs):
169176
return super(AlbumAutoListViewSet, self).retrieve(*args, **kwargs)
170177

171-
@cache_response(key_func=CustomListKeyConstructor())
178+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
172179
def list(self, *args, **kwargs):
173180
return super(AlbumAutoListViewSet, self).list(*args, **kwargs)
174181

175182

176183

184+
185+
186+
187+
177188
class AlbumPersonViewSet(viewsets.ModelViewSet):
178189
queryset = Person.objects.all().order_by('name')
179190
serializer_class = AlbumPersonSerializer
180191
pagination_class = StandardResultsSetPagination
181192

182-
@cache_response(key_func=CustomObjectKeyConstructor())
193+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
183194
def retrieve(self, *args, **kwargs):
184195
return super(AlbumPersonViewSet, self).retrieve(*args, **kwargs)
185196

186-
@cache_response(key_func=CustomListKeyConstructor())
197+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
187198
def list(self, *args, **kwargs):
188199
return super(AlbumPersonViewSet, self).list(*args, **kwargs)
189200

190201

202+
class AlbumPersonListViewSet(viewsets.ModelViewSet):
203+
queryset = Person.objects.all().order_by('name')
204+
serializer_class = AlbumPersonListSerializer
205+
pagination_class = StandardResultsSetPagination
206+
207+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
208+
def retrieve(self, *args, **kwargs):
209+
return super(AlbumPersonListViewSet, self).retrieve(*args, **kwargs)
210+
211+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
212+
def list(self, *args, **kwargs):
213+
return super(AlbumPersonListViewSet, self).list(*args, **kwargs)
214+
215+
216+
217+
191218

192219

193220
class AlbumDateViewSet(viewsets.ModelViewSet):
194221
queryset = AlbumDate.objects.all().order_by('-date')
195222
serializer_class = AlbumDateSerializer
196223
pagination_class = StandardResultsSetPagination
197224

198-
@cache_response(key_func=CustomObjectKeyConstructor())
225+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
199226
def retrieve(self, *args, **kwargs):
200227
return super(AlbumDateViewSet, self).retrieve(*args, **kwargs)
201228

202-
@cache_response(key_func=CustomListKeyConstructor())
229+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
203230
def list(self, *args, **kwargs):
204231
return super(AlbumDateViewSet, self).list(*args, **kwargs)
205232

233+
234+
class AlbumDateListViewSet(viewsets.ModelViewSet):
235+
queryset = AlbumDate.objects.all().order_by('-date')
236+
serializer_class = AlbumDateListSerializer
237+
pagination_class = StandardResultsSetPagination
238+
239+
@cache_response(CACHE_TTL,key_func=CustomObjectKeyConstructor())
240+
def retrieve(self, *args, **kwargs):
241+
return super(AlbumDateListViewSet, self).retrieve(*args, **kwargs)
242+
243+
@cache_response(CACHE_TTL,key_func=CustomListKeyConstructor())
244+
def list(self, *args, **kwargs):
245+
return super(AlbumDateListViewSet, self).list(*args, **kwargs)
246+
247+
248+
206249
# API Views
207250

208251
class FaceToLabelView(APIView):

0 commit comments

Comments
 (0)