def generic_vote(request, type, id): try: if not request.user.is_active: raise PermissionDenied currentUser = auth.get_user(request) contentType = ContentType.objects.get(model=type) if Vote.objects.filter(entity_id=id, voter=currentUser, content_type=contentType).exists(): raise SuspiciousOperation vote = Vote(entity_id=id, content_type=contentType, status='approve', voter=currentUser) vote.save() data = {'result': 'success', 'message': '投票完成!'} except PermissionDenied: data = {'result': 'failed', 'errorMessage': '無存取權限!'} except SuspiciousOperation: data = {'result': 'failed', 'errorMessage': '一部微電影只能投一票喔!'} response = json.dumps(data) return HttpResponse(response, mimetype='application/x-javascript')
def activity_photo_vote(request, type, photo_id): try: if not request.user.is_active: raise PermissionDenied photo = ImageUpload.objects.get(id=photo_id, content_type=ContentType.objects.get(model=type)) if not 'voting' == photo.entity_object.status: raise PermissionDenied currentUser = auth.get_user(request) if Vote.objects.filter(entity_id=photo_id, voter=currentUser, content_type=ContentType.objects.get(model='imageupload')).exists(): raise SuspiciousOperation vote = Vote(entity_object=photo, status='approve', voter=currentUser) vote.save() data = {'result': 'success', 'message': '投票完成!'} except ObjectDoesNotExist: data = {'result': 'failed', 'errorMessage': '照片不存在!'} except PermissionDenied: data = {'result': 'failed', 'errorMessage': '無存取權限!'} except SuspiciousOperation: data = {'result': 'failed', 'errorMessage': '一張照片只能投一票喔!'} response = json.dumps(data) return HttpResponse(response, mimetype='application/x-javascript')