Exemplo n.º 1
0
 def test_ContactForm(self):
     self.client = Client()
     form = SimpleContactForm(
         data={
             'full_name': 'jagadeesh',
             'message': 'sample',
             'email': '*****@*****.**',
             'phone': '94'
         })
     self.assertTrue(form.is_valid())
Exemplo n.º 2
0
def contact(request):
    if request.method == 'GET':
        raise Http404
    validate_simplecontact = SimpleContactForm(request.POST)
    validate_contact = ContactForm(request.POST)

    if 'category' in request.POST.keys():
        if validate_simplecontact.is_valid() and validate_contact.is_valid():
            contact = simplecontact.objects.create(full_name=request.POST.get('full_name'), message=request.POST.get('message'),\
                                                    email=request.POST.get('email'), phone=request.POST.get('phone') if request.POST.get('phone') else False)
            Contact.objects.create(contact_info=contact, category=request.POST.get('category'), domain=request.POST.get('domain'),\
                domain_url=request.POST.get('domain_url'), skype=request.POST.get('skype'), country=request.POST.get('country'),\
                budget=request.POST.get('budget'), technology=request.POST.get('technology'),\
                requirements=request.POST.get('requirements'), enquery_type=request.POST.get('enquery_type'))
        else:
            errors = {}
            errors = dict((validate_simplecontact.errors).items() +
                          (validate_contact.errors).items())
            data = {'error': True, 'errinfo': errors}
            return HttpResponse(json.dumps(data),
                                content_type='application/json; charset=utf-8')

    if validate_simplecontact.is_valid():
        simplecontact.objects.get_or_create(full_name=request.POST.get('full_name'), message=request.POST.get('message'), email=request.POST.get('email'),\
                                                phone=request.POST.get('phone') if request.POST.get('phone') else False)
    else:
        errors = {}
        errors = dict((validate_simplecontact.errors).items())
        data = {'error': True, 'errinfo': errors}
        return HttpResponse(json.dumps(data),
                            content_type='application/json; charset=utf-8')

    message = "<p>From: " + request.POST.get(
        'full_name') + "</p><p>Email Id: " + request.POST.get(
            'email') + "</p><p>Message: " + request.POST.get(
                'message') + "</p>"

    if request.POST.get('phone'):
        message += "<p>Contact Number: " + request.POST.get('phone') + "</p>"

    if 'category' in request.POST.keys():
        message += "<p>Skype ID: "+request.POST.get('skype')+"</p><p>Country: "+request.POST.get('country')+"</p><p><b>Domain Details: </b></p><p>Domain: "+\
                    request.POST.get('domain')+ "</p><p>Domain URL: "+request.POST.get('domain_url')+"</p>"

        message += "<p><b>General Information: </b></p><p>Category: "+request.POST.get('category')+"</p><p>Requirements: "+\
                    request.POST.get('requirements')+"</p><p>Technology: "+request.POST.get('technology')+"</p><p>Enquery Type: "+\
                    request.POST.get('enquery_type')+"</p><p>Budget: "+request.POST.get('budget')+"</p>"

    sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)

    contact_msg = sendgrid.Mail()
    contact_msg.set_subject("Thank u for ur message")
    contact_msg.set_text(
        'Thank you for contacting us. We will get back to you soon!!!')
    contact_msg.set_from("*****@*****.**")
    contact_msg.add_to(request.POST.get('email'))
    sg.send(contact_msg)

    sending_msg = sendgrid.Mail()
    sending_msg.set_subject("Contact Request")
    sending_msg.set_html(message)
    sending_msg.set_text('Contact Request')
    sending_msg.set_from(request.POST.get('email'))
    sending_msg.add_to("*****@*****.**")
    sg.send(sending_msg)

    data = {'error': False, 'response': 'submitted successfully'}
    return HttpResponse(json.dumps(data),
                        content_type='application/json; charset=utf-8')