def new(request): user_profile = request.user.userprofile_set.filter()[0] form_categories = Category.objects.filter(owner=user_profile) if request.method == "GET": current_user = request.user form = ContactForm(current_user=current_user) user_profile = request.user.userprofile_set.filter()[0] form_categories = Category.objects.filter(owner=user_profile) print(len(form_categories)) return render(request, 'contacts/new_contact_form.html', { 'form': form, 'title': "New Contact", 'categories': form_categories }) else: form = ContactForm(data=request.POST, current_user=request.user) if form.is_valid(): try: user_profile = get_user_profile(request) age = form.cleaned_data['age'] name = form.cleaned_data['name'] public = form.cleaned_data['public'] category_id = form.cleaned_data['category'] category = Category.objects.filter(id=category_id)[0] contact = Contact(age=age, name=name, public=public, owner=user_profile, category=category) contact.save() messages.add_message(request, messages.SUCCESS, "Contact successfully created") return redirect('/contacts/') except: messages.add_message(request, messages.ERROR, "Contact creation unsuccessful") return render( request, 'contacts/new_contact_form.html', { 'form': form, 'title': "New Contact", 'categories': form_categories }) else: messages.add_message(request, messages.ERROR, "Contact creation unsuccessful") return render( request, 'contacts/new_contact_form.html', { 'form': form, 'title': "New Contact", 'categories': form_categories })
def edit(request, id): if request.method == "GET": contact = Contact.objects.get(id=id) current_user = request.user user_profile = current_user.userprofile_set.filter()[0] categories = Category.objects.filter(owner=user_profile) edit_contact_form = ContactForm( current_user, { 'name': contact.name, 'age': contact.age, 'public': contact.public, 'category': contact.category.id }) return render( request, 'contacts/edit_contact_form.html', { 'form': edit_contact_form, 'contact': contact, 'title': 'Edit Contact', 'categories': categories }) else: contact = Contact.objects.get(id=id) edit_contact_form = ContactForm(request.user, request.POST) if edit_contact_form.is_valid(): contact.name = edit_contact_form.cleaned_data['name'] contact.age = edit_contact_form.cleaned_data['age'] contact.public = edit_contact_form.cleaned_data['public'] category_id = edit_contact_form.cleaned_data['category'] category = Category.objects.filter(id=category_id)[0] contact.category = category contact.owner = get_user_profile(request) contact.save() messages.add_message(request, messages.SUCCESS, "Contact successfully edited") return redirect('/contacts/') else: current_user = request.user user_profile = current_user.userprofile_set.filter()[0] categories = Category.objects.filter(owner=user_profile) messages.add_message(request, messages.ERROR, "Contact editing unsuccessful") return render( request, 'contacts/edit_contact_form.html', { 'form': edit_contact_form, 'contact': contact, 'title': "Edit Contact", 'categories': categories })
def edit(request, id): if request.method == "GET": contact = Contact.objects.get(id=id) current_user = request.user user_profile = current_user.userprofile_set.filter()[0] categories = Category.objects.filter(owner=user_profile) edit_contact_form = ContactForm(current_user, {'name':contact.name, 'age': contact.age, 'public': contact.public, 'category': contact.category.id}) return render(request, 'contacts/edit_contact_form.html', {'form':edit_contact_form, 'contact':contact, 'title': 'Edit Contact', 'categories':categories}) else: contact = Contact.objects.get(id=id) edit_contact_form = ContactForm(request.user, request.POST) if edit_contact_form.is_valid(): contact.name = edit_contact_form.cleaned_data['name'] contact.age = edit_contact_form.cleaned_data['age'] contact.public = edit_contact_form.cleaned_data['public'] category_id = edit_contact_form.cleaned_data['category'] category = Category.objects.filter(id=category_id)[0] contact.category = category contact.owner = get_user_profile(request) contact.save() messages.add_message(request, messages.SUCCESS, "Contact successfully edited" ) return redirect('/contacts/') else: current_user = request.user user_profile = current_user.userprofile_set.filter()[0] categories = Category.objects.filter(owner=user_profile) messages.add_message(request, messages.ERROR, "Contact editing unsuccessful" ) return render(request, 'contacts/edit_contact_form.html', {'form':edit_contact_form, 'contact':contact, 'title':"Edit Contact", 'categories':categories})
def new(request): user_profile = request.user.userprofile_set.filter()[0] form_categories = Category.objects.filter(owner=user_profile) if request.method == "GET": current_user = request.user form = ContactForm(current_user=current_user) user_profile = request.user.userprofile_set.filter()[0] form_categories = Category.objects.filter(owner=user_profile) return render(request, 'contacts/new_contact_form.html', {'form':form, 'title':"New Contact", 'categories':form_categories}) else: form = ContactForm(data=request.POST, current_user=request.user) if form.is_valid(): try: user_profile = get_user_profile(request) age = form.cleaned_data['age'] name = form.cleaned_data['name'] public = form.cleaned_data['public'] category_id = form.cleaned_data['category'] category = Category.objects.filter(id=category_id)[0] contact = Contact(age=age, name=name, public=public, owner=user_profile, category=category) contact.save() messages.add_message(request, messages.SUCCESS, "Contact successfully created" ) return redirect('/contacts/') except: messages.add_message(request, messages.ERROR, "Contact creation unsuccessful" ) return render(request, 'contacts/new_contact_form.html', {'form':form, 'title':"New Contact", 'categories': form_categories}) else: messages.add_message(request, messages.ERROR, "Contact creation unsuccessful" ) return render(request, 'contacts/new_contact_form.html', {'form':form, 'title':"New Contact", 'categories': form_categories})
def test_if_empty_contact_form_is_valid(self): form = ContactForm({}) self.assertFalse(form.is_valid())