示例#1
0
def form(request, id=False):
    if id:
        instance = get_object_or_404(Contact, id=id, deleted=False)
        msg = _("Successfully edited contact")
    else:
        instance = Contact()
        msg = _("Successfully added new contact")

    #Save and set to active, require valid form
    if request.method == 'POST':
        form = ContactForm(request.POST, request.FILES, instance=instance)
        if form.is_valid():
            o = form.save(commit=False)
            o.owner = request.user
            o.save()
            form.save_m2m()
            request.message_success(msg)

            return redirect(view, o.id)
    else:
        form = ContactForm(instance=instance)

    return render(request, "contacts/form.html", {
        'title': _("Contact"),
        'form': form,
        'contact': instance,
    })
示例#2
0
def test_append_contact_email():
    contact = Contact(username='******',
                      first_name='Guido',
                      last_name='van Rossum',
                      emails=[
                          ContactEmail(email='*****@*****.**'),
                      ])
    contact.emails.append(ContactEmail(email='*****@*****.**'))
    assert len(contact.emails) == 2
示例#3
0
def test_contact_email_count():
    contact = Contact(username='******',
                      first_name='Guido',
                      last_name='van Rossum')
    contact_emails = [
        ContactEmail(email='*****@*****.**'),
        ContactEmail(email='*****@*****.**')
    ]
    contact.emails.extend(contact_emails)
    assert len(contact.emails) == 2
示例#4
0
 def create(self, request):
     instance = Contact()
     form = ContactForm(request.POST, request.FILES, instance=instance)
     if form.is_valid():
         contact = form.save(commit=False)
         contact.owner = request.user
         contact.save()
         form.save_m2m()
         return contact
     else:
         return form.errors
示例#5
0
 def create_contact(self, data):
     email_list = data.pop('emails', list())
     contact = Contact(**data)
     contact_emails = [
         ContactEmail(email=d.get('email')) for d in email_list
     ]
     contact.emails.extend(contact_emails)
     db.session.add(contact)
     db.session.add_all(contact_emails)
     db.session.commit()
     self.instance = contact
示例#6
0
def generate_random_contact():
    contact_data = {
        'username': get_random_name(),
        'first_name': get_random_name(),
        'last_name': get_random_name(),
    }
    emails = generate_random_emails(2)

    contact = Contact(**contact_data)
    contact_emails = [
        ContactEmail(email=email, contact=contact) for email in emails
    ]
    contact.emails.extend(contact_emails)
    db.session.add_all(contact_emails)
    db.session.add(contact)
    db.session.commit()
示例#7
0
def contact_item(session):
    contact = Contact(
        username='******',
        first_name='Sam',
        last_name='Henry',
    )

    contact_emails = [ContactEmail(email='*****@*****.**')]

    contact.emails.extend(contact_emails)
    session.add(contact)
    session.add_all(contact_emails)
    yield contact

    # Cleanup
    Contact.query.filter(Contact.username == contact.username).delete()
示例#8
0
def contact_with_multiple_emails(session, contact_email_1, contact_email_2):
    contact = Contact(
        username='******',
        first_name='John',
        last_name='William',
    )

    contact_emails = [contact_email_1, contact_email_2]

    contact.emails.extend(contact_emails)
    session.add(contact)
    session.add_all(contact_emails)
    yield contact

    # Cleanup
    Contact.query.filter(Contact.username == contact.username).delete()
示例#9
0
def add_ajax(request):
    form = ContactForm(request.POST, instance=Contact())

    if form.is_valid():
        a = form.save()

        return HttpResponse(simplejson.dumps({
            'name': a.name,
            'valid': True,
            'id': a.id
        }),
                            mimetype='application/json')

    else:
        errors = dict([(field, errors[0])
                       for field, errors in form.errors.items()])

        return HttpResponse(simplejson.dumps({
            'errors': errors,
            'valid': False
        }),
                            mimetype='application/json')

    return HttpResponse("ERROR")