def new_event(request): if request.method == 'POST': form = EventForm(request.POST) if not form.is_valid(): messages.error(request, 'There was a problem recording the event.') else: event = form.save(commit=False) event.user = request.user event.save() messages.success( request, 'Event recorded: %s at %s' % (event.description, event.time)) return HttpResponseGetAfterPost('%s#recorded' % (request.get_full_path())) else: form = EventForm() recent = Event.objects.filter(user=request.user)\ .order_by('time') events = Event.objects.filter(user=request.user)\ .extra(select={'lower_desc':'lower(description)'})\ .values('description')\ .order_by('lower_desc')\ .annotate(total=Count('id')) context = { 'form': form, 'events': events, 'recent': recent, 'advanced_form': True, } return render(request, 'record/edit_event.html', context)
def new_item(request): if request.method == 'POST': form = RecordForm(request.POST) if not form.is_valid(): messages.error(request, 'There was a problem recording the item.') else: record = form.save(commit=False) record.user = request.user record.save() messages.success( request, 'Item recorded: %s at %s' % (record.description, record.time)) return HttpResponseGetAfterPost('%s#recorded' % (request.get_full_path())) else: form = RecordForm() recent = request.user.recent_items.all() items = Item.objects.all().extra(select={ 'lower_desc': 'lower(description)' }).order_by('lower_desc') context = { 'form': form, 'items': items, 'recent': recent, 'advanced_form': True, } return render(request, 'record/edit_record.html', context)
def register(request): if request.user.is_authenticated(): return HttpResponseTemporaryRedirect(reverse('home')) if request.method == 'POST': form = UserCreationForm(request.POST) if not form.is_valid(): messages.error(request, 'There was a problem with the registration.') else: user = form.save(commit=False) user.first_name = user.username user.save() if user.is_active: user = authenticate( username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password1')) login(request, user) return HttpResponseGetAfterPost(reverse('home')) else: form = UserCreationForm() context = { 'form': form, } return render(request, 'user/register.html', context)
def profile(request): password_form = PasswordChangeForm(request.user) user_form = UserChangeForm(instance=request.user) if request.method == 'POST': if request.GET.get('type', 'password'): password_form = PasswordChangeForm(request.user, request.POST) if not password_form.is_valid(): messages.error(request, 'There was a problem changing the password.') else: messages.success(request, 'Password changed.') return HttpResponseGetAfterPost('%s' % (request.path)) if request.GET.get('type', 'details'): user_form = PasswordChangeForm(request.POST, instance=request.user) if not user_form.is_valid(): messages.error(request, 'There was a problem updating your details.') else: messages.success(request, 'Details updated.') return HttpResponseGetAfterPost('%s' % (request.path)) context = {'password_form': password_form, 'user_form': user_form} return render(request, 'user/profile.html', context)
def delete_item(request): record_pk = request.GET.get('id', None) record = get_object_or_404(Record, pk=record_pk) if request.method == 'POST': record.delete() messages.success( request, 'Item deleted: %s at %s' % (record.description, record.time)) return HttpResponseGetAfterPost('%s#deleted' % (reverse(view_items))) context = { 'record': record, } return render(request, 'record/delete_record.html', context)
def delete_event(request): event_pk = request.GET.get('id', None) event = get_object_or_404(Event, pk=event_pk) if request.method == 'POST': event.delete() messages.success( request, 'Item deleted: %s at %s' % (event.description, event.time)) return HttpResponseGetAfterPost('%s#deleted' % (reverse(view_events))) context = { 'event': event, } return render(request, 'record/delete_event.html', context)
def edit_item(request): record_pk = request.GET.get('id', None) record = get_object_or_404(Record, pk=record_pk) if request.method == 'POST': form = RecordForm(request.POST, instance=record) if not form.is_valid(): messages.error(request, 'There was a problem editing the item.') else: record = form.save() messages.success( request, 'Item edited: %s at %s' % (record.description, record.time)) return HttpResponseGetAfterPost('%s#edited' % (request.get_full_path())) else: form = RecordForm(instance=record) context = { 'form': form, } return render(request, 'record/edit_record.html', context)
def edit_event(request): event_pk = request.GET.get('id', None) event = get_object_or_404(Event, pk=event_pk) if request.method == 'POST': form = EventForm(request.POST, instance=event) if not form.is_valid(): messages.error(request, 'There was a problem editing the note.') else: event = form.save() messages.success( request, 'Note edited: %s at %s' % (event.description, event.time)) return HttpResponseGetAfterPost('%s#edited' % (request.get_full_path())) else: form = EventForm(instance=event) context = { 'form': form, } return render(request, 'record/edit_event.html', context)
def logout(request): logout_user(request) return HttpResponseGetAfterPost(reverse('login'))