Ejemplo n.º 1
0
    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.set_password(self.cleaned_data['password1'])
        user.is_active = False
        #testmail = send_mail("registration", "activation link", "*****@*****.**", [user.email,], fail_silently=False)
        
        activation_code = md5(str(uuid.uuid4())).hexdigest()
        
        current_site = Site.objects.get_current()
        
        HOSTNAME = current_site.domain
        url = "http://" + HOSTNAME + "/member/register/" + activation_code + "/active/"
            
        if commit:
            
            try:
                from string import Template
                user.save()
                ActivationCode.objects.create(user=user,code=activation_code, status=0)
              #   email_body_tmpl = Template(mtn.sms_reply_email_body)
              #   email_body = email_body_tmpl.substitute(name=sender_name)
                send_templated_email(
                    subject = "Champion email confirmation",
                    email_template_name='templated_email/champemail_reg.html',
                    sender = '*****@*****.**',
                    recipients=user.email,

                    email_context={
                        'activation_url': url,
                    }
                )
                
            except:
                pass
        return user
Ejemplo n.º 2
0
def reply_message(request):
    
    tw_number_from = request.POST.get("From", '+8801743505740')
    account_sid = request.POST.get("AccountSid",'AC8792a347f8748e735fa0b70a790e5974') # 'ACee27bb0ac9c27566f6b4ed74a44fbc4c') #dev
    tw_number_to = request.POST.get("To",'+14805259964') # "+14807254656") #dev
    
    message_body = request.POST.get("Body",'Mamun [email protected]')
    sms_sid = request.POST.get("SmsSid", "CA22ac0234c1892e9b7c9cd327bdea9533")

    mtn = MemberTwilioNumber.objects.get(twilio_number=tw_number_to, tw_account_sid=account_sid)
    user = mtn.member
    auth_token = mtn.tw_auth_token
    site = request.META['HTTP_HOST']

    client = TwilioRestClient(account_sid, auth_token)

    if message_body:
        # sms = Template(mtn.message_body)
        sms = mtn.message_body
        # sms_body = sms.substitute(name=sender_name, email=sender_email)
        
        messages = message_body.split(" ")
        sender_email = messages[-1]

        try:
            contact = Contact.objects.get(contact_number=tw_number_from)
            lead = Lead.objects.create(contact=contact)
        except:
            contact = Contact.objects.create(member=user, contact_number=tw_number_from, contact_email=sender_email)
            lead = Lead.objects.create(contact=contact)
            pass

        
        if len(messages) == 3:
            sender_fname = messages[0]
            sender_lname = messages[1]
            contact.contact_fname = sender_fname
            contact.contact_lname = sender_lname
        else:
            sender_fname = messages[0]
            contact.contact_fname = sender_fname

        # save contact and lead

        contact.save()
        lead.lead_type = 'sms'
        lead.lead_sms = messages
        lead.email = sender_email
        lead.name = sender_fname
        lead.sms_sid = sms_sid
        lead.called = tw_number_to
        try:
            lead.save()
            
        except:
            pass


        # Prepare sms body
        sms_body = "Hi "+sender_fname + ", " + sms
           
    else: 
        sms_body = mtn.message_body

    if mtn.active_sms_reply:
        sms_to_send = sms_body
        if user.membercurrentbalance.total_sms > 0:
            message = client.messages.create(
                to=tw_number_from, 
                from_=tw_number_to, 
                body=sms_body,
                # status_callback = 'http://'+ site +'/mytwilio/reply_message/status/'
            )

            if message.sid:
                mcb = MemberCurrentBalance.objects.get(member=user)
                mcb.total_sms = mcb.total_sms -1
                mcb.save()

        else:
            message = client.messages.create(
                to=tw_number_to, 
                from_='', 
                body='You sms balance is empty. Please recharge.'                
            )


    # if mtn.mms_reply_active:
    #     mms_url = mtn.mms_file.path
    #     mms_body = "This is MMS"

    #     if user.membercurrentbalance.total_mms > 0:
    #         message = client.messages.create(
    #             to=tw_number_from, 
    #             from_=tw_number_to, 
    #             body=mms_body,
    #             # status_callback = 'http://'+ site +'/mytwilio/reply_message/status/'
    #         )
    #         if message.sid:
    #             mcb = MemberCurrentBalance.objects.get(member=user)
    #             mcb.total_sms = mcb.total_sms -1
    #             mcb.save()
    #     else:
    #         message = client.messages.create(
    #             to=tw_number_to, 
    #             from_='', 
    #             body='You mms balance is empty. Please recharge.'                
    #         )

    if mtn.sms_reply_email_active:
        from django.template import Template, loader, Context
        try:
            email_body = mtn.sms_reply_email_body
            attach = mtn.sms_reply_email_attachement.path
            
        except:
            attach = None
            pass
        if user.membercurrentbalance.total_email > 0:
            tmpl = loader.get_template('templated_email/reply_message_email.html')
            con = Context({"name": sender_fname, "body": email_body})
            email_content = strip_tags(tmpl.render(con))
            email = EmailMessage(
                'Email from champion', 
                email_content, '*****@*****.**', 
                [sender_email,], 
                headers = {'Reply-To': '*****@*****.**'}
            )
            if attach:

                f = open(attach)
                content_type = mimetypes.guess_type(attach)[0]
                email.attach(f.name, f.read(), content_type)
                email.send()
            else:
                email.send()

        elif  user.membercurrentbalance.total_email <= 0:
                send_templated_email(
                    subject = "No balance for email",
                    email_template_name='templated_email/no-balance-for-digital-content.html',
                    sender = '*****@*****.**',
                    recipients=user.email,
                    #files = attach,
                    email_context={
                        'body': 'You have no balance for digital content',
                    },
                )
        
    return HttpResponse(message.sid)