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
class DeleteDraftsCronTests(TestCase): def setUp(self): super(DeleteDraftsCronTests, self).setUp() self.c = get_user(username='******') grace_days = (datetime.now() - timedelta(days=settings.DIARY_GRACE_DAYS + 1)) self.draft = DiaryEntry(creator=self.c, created=grace_days, is_draft=True) self.draft.save() self.draft_pk = self.draft.pk draft_pic = DiaryPicture(entry=self.draft) draft_pic2 = DiaryPicture(entry=self.draft) self.entry = DiaryEntry(creator=self.c, is_draft=False) self.entry.save() self.entry_pk = self.entry.pk entry_pic = DiaryPicture(entry=self.entry) entry_pic2 = DiaryPicture(entry=self.entry) with open('apps/diary/tests/media/django.jpg') as f: img = File(f) draft_pic.image.save('cowboy', img, save=False) draft_pic.thumbnail.save('cowboy', img, save=False) entry_pic.image.save('cowboy_new', img, save=False) entry_pic.thumbnail.save('cowboy_new', img, save=False) draft_pic.save() entry_pic.save() with open('apps/diary/tests/media/hawk.jpg') as f: img = File(f) draft_pic2.image.save('hawk', img, save=False) draft_pic2.thumbnail.save('hawk', img, save=False) entry_pic2.image.save('hawk_new', img, save=False) entry_pic2.thumbnail.save('hawk_new', img, save=False) draft_pic2.save() entry_pic2.save() def tearDown(self): super(DeleteDraftsCronTests, self).tearDown() DiaryPicture.objects.all().delete() def test_cron_old_drafts(self): delete_old_drafts() # Assert cron function deleted draft. fn = lambda: DiaryEntry.objects.get(pk=self.draft_pk) self.assertRaises(DiaryEntry.DoesNotExist, fn) # Assert cron function did not delete self.entry or its pictures. entry = DiaryEntry.objects.get(pk=self.entry_pk) eq_(2, entry.pictures.count())
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})
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