def edit_ng_report(request, display_name='', year=None, month=None, day=None, id=None): user = request.user created = False initial = {} if not id: report = NGReport() created = True initial = {'location': '%s, %s, %s' % (user.userprofile.city, user.userprofile.region, user.userprofile.country), 'latitude': user.userprofile.lat, 'longitude': user.userprofile.lon} else: report = get_object_or_404( NGReport, pk=id, user__userprofile__display_name=display_name) report_form = forms.NGReportForm(request.POST or None, instance=report, initial=initial) if report_form.is_valid(): if created: report.user = user messages.success(request, 'Report successfully created.') else: messages.success(request, 'Report successfully updated.') report_form.save() return redirect(report.get_absolute_url()) return render(request, 'edit_ng_report.html', {'report_form': report_form, 'pageuser': user, 'report': report, 'created': created, 'campaign_trigger': ACTIVITY_CAMPAIGN})
def test_base(self): user = UserFactory.create() activity = ActivityFactory.create() campaign = CampaignFactory.create() functional_area = FunctionalAreaFactory.create() data = { 'report_date': '25 March 2012', 'activity': activity.id, 'campaign': campaign.id, 'longitude': 44.33, 'latitude': 55.66, 'location': 'world', 'link': 'https://example.com', 'link_description': 'Test link.', 'activity_description': 'Test activity', 'functional_areas': functional_area.id, } form = NGReportForm(data, instance=NGReport(user=user)) ok_(form.is_valid()) db_obj = form.save() eq_(db_obj.report_date, date(2012, 03, 25)) eq_(db_obj.activity, activity) eq_(db_obj.longitude, 44.33) eq_(db_obj.latitude, 55.66) eq_(db_obj.location, 'world') eq_(db_obj.link, 'https://example.com') eq_(db_obj.link_description, 'Test link.') eq_(db_obj.activity_description, 'Test activity'), eq_(db_obj.functional_areas.all().count(), 1) eq_(db_obj.functional_areas.all()[0], functional_area) eq_(db_obj.mentor, user.userprofile.mentor)