Created
June 21, 2011 19:10
-
-
Save ildus/1038615 to your computer and use it in GitHub Desktop.
http response json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def view_vote(request, aid = 0): | |
result = {'success': False} | |
cookie_set = False | |
try: | |
interview = Interview.objects.latest() | |
answer = Answer.objects.get(id = aid) | |
except: | |
result['message'] = 'Answer or interview not founded' | |
else: | |
if answer.interview.id != interview.id or interview.on == False: | |
result['message'] = 'Answer is incorrect' | |
else: | |
result['success'] = True | |
if request.session.get('voted', False): | |
result['message'] = 'You are voted' | |
elif request.COOKIES.get('v%s'%interview.id, '0') == '1': | |
result['message'] = 'You are voted' | |
else: | |
request.session['voted'] = True | |
cookie_set = True | |
answer.count += 1 | |
answer.save() | |
result['message'] = 'Vote is success' | |
json = json.dumps(result) | |
response = HttpResponse(json, mimetype = 'application/json') | |
if cookie_set: | |
response.set_cookie('v%s'%interview.id, '1') | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment