def ajax_get_date(request):
    if request.is_ajax() and request.method == "GET":
        response = {}
        request_date = request.GET["date"]
        request_user = request.GET["userId"]

        if DiaryEntry.objects.filter(user_profile_id=request_user,
                                     entry_date=request_date).exists():
            entry = DiaryEntry.objects.get(user_profile_id=request_user,
                                           entry_date=request_date)
            response.update({
                "whole_foods": entry.whole_foods,
                "processed_foods": entry.processed_foods,
                "notes": entry.notes
            })
            return HttpResponse(json.dumps(response),
                                content_type="application/json")
        else:
            new_entry = DiaryEntry(user_profile_id=request_user,
                                   entry_date=request_date,
                                   notes="")
            new_entry.save()
            response.update({
                "whole_foods": new_entry.whole_foods,
                "processed_foods": new_entry.processed_foods,
                "notes": new_entry.notes
            })
            return HttpResponse(json.dumps(response),
                                content_type="application/json")
    else:
        return Http404
Beispiel #2
0
def diary(request, user_profile_id):
    if DiaryEntry.objects.filter(user_profile_id=user_profile_id, entry_date=datetime.date.today()).exists():
        curr_entry = DiaryEntry.objects.get(user_profile_id=user_profile_id, entry_date=datetime.date.today())
        return render_to_response('diary/diary_detail.html', {'user_profile_id': user_profile_id,
            'username': request.user.username, 'whole_foods': range(1, curr_entry.whole_foods + 1),
            'processed_foods': range(1, curr_entry.processed_foods + 1), 'notes': curr_entry.notes})
    else:
        u = DiaryEntry(user_profile_id=user_profile_id, entry_date=datetime.date.today(), notes="")
        u.save()
        return render_to_response('diary/diary_detail.html', {'user_profile_id': user_profile_id,
                                                            'username': request.user.username})