def addContact(): form = AddContactForm() customer_choices = [(a.id, a.name) for a in Customer.query.filter_by(customer_type=CUSTOMER_TYPES['TYPE_CUSTOMER']).all()] customer_choices = [(0, '')] + customer_choices form.customer.choices = customer_choices if form.validate_on_submit(): contact = Contact() contact.first_name = form.first_name.data contact.surname = form.surname.data contact.phone = form.phone.data contact.email = form.email.data if form.customer.data and form.customer.data != '' and form.customer.data != 0: contact.customer_id = form.customer.data else: contact.customer_id = None db.session.add(contact) db.session.commit() flash(gettext("New contact successfully added.")) return redirect(url_for("contacts")) return render_template('settings/addContact.html', title=gettext("Add New Contact"), form=form)
def editContact(request, id): contact_edit = contact.objects.filter(user=request.user).get(id=id) form = AddContactForm(request.POST or None, instance=contact_edit) if form.is_valid(): save_it = form.save(commit=False) save_it.user = request.user save_it.save() return redirect('/contact/') return render_to_response('contact.html', locals(), context_instance=RequestContext(request))
def addContact(request): contact_list = contact.objects.filter(user=request.user) form = AddContactForm(request.POST or None) if form.is_valid(): save_it = form.save(commit=False) save_it.user = request.user save_it.save() return HttpResponseRedirect('') return render_to_response('contact.html', locals(), context_instance=RequestContext(request))
def add_contact_form(): form = AddContactForm() if request.method == 'POST' and form.validate_on_submit(): connect(mongo) new_contact = Contact() new_contact.name = form.name.data new_contact.phone = form.phone.data new_contact.email = form.email.data new_contact.registration_date = str(date.today()) new_contact.save() return 'Contact added' return render_template("add_contact.html", form=form)
def addcontact(): form = AddContactForm() if form.validate_on_submit(): client = MongoClient(MONGODB_URI) db = client.get_default_database() contacts = db.contacts newContact = { "name": form.name.data, "number": form.number.data, "description": form.description.data, "user_id": current_user.id } contacts.insert(newContact) return redirect('/index') return render_template('addcontact.html', user = current_user, title = 'Add New Contact', form = form)
def editContact(id=0): contact = Contact.query.filter_by(id=id).first() if contact == None: flash(gettext('Contact not found.')) return redirect(url_for('contacts')) form = AddContactForm(obj=contact) customer_choices = [(a.id, a.name) for a in Customer.query.filter_by(customer_type=CUSTOMER_TYPES['TYPE_CUSTOMER']).all()] customer_choices = [(0, '')] + customer_choices form.customer.choices = customer_choices if form.is_submitted(): #delete contact if 'delete' in request.form: db.session.delete(contact) db.session.commit() return redirect(url_for("contacts")) if form.validate(): #update contact contact.first_name = form.first_name.data contact.surname = form.surname.data contact.phone = form.phone.data contact.email = form.email.data if form.customer.data and form.customer.data != '' and form.customer.data != 0: contact.customer_id = form.customer.data else: contact.customer_id = None db.session.add(contact) db.session.commit() flash(gettext("Contact successfully changed.")) return redirect(url_for("contacts")) selected = contact.customer.id if contact.customer else 0 return render_template('settings/editContact.html', title=gettext("Edit Contact"), contact=contact, selected=selected, form=form)