Advertisement
marty331

hellowebapptest

May 8th, 2017
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. models.py
  2. class WeeklyTime(models.Model):
  3.     SAVED = 'Saved'
  4.     PENDING = 'Pending'
  5.     APPROVED = 'Approved'
  6.     REJECTED = 'Rejected'
  7.     PUSHBACK = 'Pushed Back'
  8.     SUBMIT_STATUS_CHOICES = (
  9.         (SAVED, 'Saved'),
  10.         (PENDING, 'Pending'),
  11.         (APPROVED, 'Approved'),
  12.         (REJECTED, 'Rejected'),
  13.         (PUSHBACK, 'Pushed Back'),
  14.         )
  15.     username = models.ForeignKey(User, blank=False, null=False)
  16.     consultant = models.CharField(max_length=255)
  17.     client = models.CharField(max_length=255)
  18.     time_period = models.CharField(max_length=25)
  19.  
  20. forms.py
  21. class WeeklyTimeForm(ModelForm):
  22.     client = forms.ModelChoiceField(queryset=Clients.objects.all(), empty_label="(Select Client)")
  23.     #time_period = forms.DateField(widget=AdminDateWidget())
  24.     class Meta:
  25.         model = WeeklyTime
  26.         fields = ['consultant', 'client', 'time_period', 'mon_hours', 'tues_hours', 'wed_hours', 'thur_hours', 'fri_hours', 'sat_hours', 'sun_hours', 'total_hours', 'comments']
  27.  
  28. views.py
  29. @login_required
  30. def timeedit(request, slug):
  31.     activate(settings.TIME_ZONE)
  32.     #get the time detail
  33.     week = WeeklyTime.objects.get(slug=slug)
  34.     #set the form
  35.     form_class = WeeklyTimeForm
  36.  
  37.     #if coming from submitted timesheet
  38.     if request.method == 'POST':
  39.         #get the data
  40.         form = form_class(data=request.POST, instance=week)
  41.         if(form.is_valid()):
  42.             #save the data
  43.             form.save()
  44.             messages.success(request, 'Your timesheet was updated successfully.')
  45.             return redirect('time_detail', slug=week.slug)
  46.         else:
  47.             messages.error(request, 'Your timesheet was not updated, please try again.')
  48.             return redirect('time_detail', slug=week.slug)
  49.     #otherwise create the form
  50.     else:
  51.         print("client="+str(week.client))
  52.         form = form_class(instance=week)
  53.  
  54.     #render the template
  55.     return render(request, 'timeviews/week_edit.html', {'week': week, 'form': form,})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement