def create_answer(request): if request.method == 'POST': if request.FILES: access_token = request.POST['access_token'] user_data = UserData.objects.get(access_token=access_token) #Connect to S3 conn = boto.connect_s3() bucket = conn.get_bucket('dittles') k = Key(bucket) #Create filename for S3 s3_key = datetime.datetime.now().strftime( '%Y%m%d-%H%M%S-%f') + user_data.user.username + '.jpg' k.key = 'answers/' + s3_key k.set_metadata("Content-Type", 'image/jpeg') dict_keys = request.FILES.keys() k.set_contents_from_string(request.FILES[dict_keys[0]].read()) #Store Question metadata in DB image_url = 'https://s3.amazonaws.com/dittles/' + k.key text = '' if ('text' in request.POST): text = request.POST['text'] if ('question_id' in request.POST): question_id = request.POST['question_id'] else: return HttpResponse('Question_id not found in post') q = Question.objects.get(id=question_id) a = Answer(question=q, text=text, user=user_data.user, image_url=image_url, pub_date=datetime.datetime.now()) a.save() action = Action(sender=user_data.user, question=q, answer=a, pub_date=datetime.datetime.now(), type='answer') action.save() return HttpResponse(request.FILES[dict_keys[0]].read()) else: return HttpResponse("Nothing, just nothing!")
def like_answer(request): if request.method == 'POST': if 'answer_id' in request.POST and 'access_token' in request.POST: access_token = request.POST['access_token'] user_data = UserData.objects.get(access_token=access_token) answer_id = request.POST['answer_id'] answer = Answer.objects.get(pk=answer_id) like = AnswerLike(answer=answer, user=user_data.user, like_date=datetime.datetime.now()) like.save() action = Action(sender=user_data.user, question=answer.question, answer=answer, pub_date=datetime.datetime.now(), type='like') action.save() return HttpResponse('AnswerLike created') else: return HttpResponse('Answer_id and/or access_token not posted') else: return HttpResponse('Answer_id and/or access_token not posteod')