def edit(request, tracker_id): context_vars = dict() try: tracker = Tracker.objects.get(id=tracker_id) except ObjectDoesNotExist: raise Http404 form = TrackerForm(instance=tracker) if request.method == 'POST': form = TrackerForm(request.POST, request.FILES, instance=tracker) if form.is_valid(): tracker = form.save() return HttpResponseRedirect(reverse('tracker_index')) context_vars['form'] = form return direct_to_template(request, template='tracker/form.html', extra_context=context_vars)
def create_tracker(request): new_track_form = TrackerForm(request.POST) if request.method == 'POST' and new_track_form.is_valid(): tracker = Track() tracker.name = request.POST['name'] tracker.unit = request.POST['unit'] tracker.description = request.POST['description'] try: with transaction.atomic(): tracker.save() except IntegrityError: return render( request, 'tracker/trackers.html', { 'trackers_list': Track.objects.all(), 'form': TrackerForm(), 'form_error': "Tracking with name '%s' already exists" % (request.POST['name']), }) return HttpResponseRedirect(reverse('tracker:trackers')) else: fields = { 'trackers_list': Track.objects.all(), 'form': new_track_form, } if new_track_form.errors.get('name'): fields['name_error'] = new_track_form.errors.get('name')[0] if new_track_form.errors.get('unit'): fields['unit_error'] = new_track_form.errors.get('unit')[0] if new_track_form.errors.get('description'): fields['description_error'] = new_track_form.errors.get( 'description')[0] return render(request, 'tracker/trackers.html', fields)
def tracker_list(request): trackers = Tracker.objects.filter(createur=request.user.profil) form = TrackerForm(request.POST or None) if form.is_valid(): if trackers.filter(nom=form.cleaned_data['nom']).exists(): form.add_error('nom', 'Vous avez déjà créé un tracker du même nom.') else: tracker = form.save(commit=False) tracker.createur = request.user.profil tracker.save() return redirect('tracker:liste-tracker') return render(request, 'tracker/tracker_list.html', { 'trackers': trackers, 'form': form })
def add(request): context_vars = dict() context_vars['caption'] = 'Add tracker' form = TrackerForm() if request.method == 'POST': form = TrackerForm(request.POST, request.FILES) if form.is_valid(): tracker = form.save(commit=False) tracker.status = Tracker.PENDING tracker.muaccount = request.muaccount tracker.location = geocode(tracker.location) tracker.save() form.save_m2m() if not request.muaccount.owner.quotas.scratchpad_nothing: create_tasks = False if request.muaccount.owner.quotas.scratchpad_full: create_tasks = True create_scratchpad(tracker.name, request.muaccount, request.user, create_tasks) return HttpResponseRedirect(reverse('tracker_advanced_query', args=[tracker.id])) context_vars['form'] = form return direct_to_template(request, template='tracker/form.html', extra_context=context_vars)
def get_context_data(self, **kwargs): context = super(TrackersView, self).get_context_data() context['form'] = TrackerForm() return context