def post(self, request, *args, **kwargs): subject = request.data.get('subject') message = request.data.get('message') from_email = request.data.get('from_email') to_email = request.data.get('to_email') if subject and message and from_email and to_email: send_email(subject, message, from_email, to_email) else: raise ValidationError( {"message": "Enter subject, message, from_email and to_email"})
def send_viral_loop_email(): verifier_objects = Verifier.objects.filter(counter__lte=3) for verifier_obj in verifier_objects: # check if verifier is not an existing employee employee_exists = Employee.objects.filter( email=str(verifier_obj.email), deleted_at=None, ).first() if not employee_exists: username = str(verifier_obj.email).split('@')[0] company = verifier_obj.employee.all().first().company employee_objects = company.employee_set.all().order_by('-id')[:3] emp_names = "" for emp in employee_objects: emp_names = emp_names + str(emp.name).split(' ')[0] + ", " unsubscribe_token = "" response = send_email( "Activate your " + str(company.name).split(' ')[0] + " Salary Wallet", "viral_loop" if verifier_obj.counter == 0 else "reminder_viral_loop", [str(verifier_obj.email)], '*****@*****.**', None, username, str(company.name).split(' ')[0], str(employee_objects.count()), str(emp_names), str(unsubscribe_token) ) if response.status_code == 202: verifier_obj.counter += 1
def verify_email(employee_object): if employee_object and employee_object.email: base_url = settings.BASE_URL mail_token = employee_object.mail_token if not mail_token: mail_token = utilities.get_random_time_based_token() employee_object.mail_token = mail_token employee_object.save() response = send_email( "Daily Salary : Verify email address", # subject "verify_email", # template [str(employee_object.email)], # to_emails '*****@*****.**', # from_email None, str(employee_object.name) if employee_object.name else '', str(mail_token), str(employee_object.unsubscribe_token), str(employee_object.id), str(base_url) ) return response
def auto_credited_email(employee_object): # send email to employee for auto credited money to wallet ( not a cron job) date_time_string = datetime.datetime.now().strftime('%b %d, %Y (%a)') # get email attachment file_path = os.path.join( BASE_DIR, 'static/images/auto_credited.png') with open(file_path, 'rb') as f: image = f.read() f.close() image_encoded = base64.b64encode(image).decode() attachment = Attachment() attachment.file_content = FileContent(image_encoded) attachment.file_type = FileType('image/png') attachment.file_name = FileName('auto_credited.png') attachment.disposition = Disposition('inline') attachment.content_id = ContentId('auto_credited') get_daily_salary = employee_object.get_daily_salary() base_url = settings.BASE_URL if employee_object and employee_object.email: date_string = datetime.datetime.now().strftime('%b %-d, %Y') company_first_name = utilities.get_company_first_name(employee_object) response = send_email( "Rs." + str(get_daily_salary) + " salary credited for " + str(date_string), # subject "auto_credited", # template [str(employee_object.email)], # to_emails '*****@*****.**', # from_email attachment, str(employee_object.name) if employee_object.name else '', int(get_daily_salary), int(employee_object.balance), str(employee_object.unsubscribe_token), str(base_url), str(company_first_name) ) return response
def post(self, request, *args, **kwargs): employee_id = request.data.get('employee_id', None) template = request.data.get('template', None) if not template: raise ValidationError({"status": "False", "message": "Email Template is required"}) if template: if template == "verify_email": if not employee_id: raise ValidationError({ "mail_verified": "False", "status": "False", "message": "EmployeeId is required"}) if employee_id: employee_obj = Employee.objects.filter(id=employee_id, deleted_at=None).first() if not employee_obj: raise ValidationError({"status": "False", "message": "Employee does not exists"}) if employee_obj.mail_verified: raise ValidationError({ "mail_verified": "True", "status": "True", "message": "Email already verified" }) response = background_jobs.verify_email(employee_obj) if response.status_code == 202: return JsonResponse({ "mail_verified": "False", "status": "True", "message": "Email has been sent successfully" }) raise ValidationError({ "mail_verified": "False", "status": "False", "message": "Can't sent email, Please Try again !"}) if template == "quess_registration_request": name = request.data.get('name', None) email = request.data.get('company_email', None) employee_id = request.data.get('employee_id', None) phone = request.data.get('phone', None) company_name = request.data.get('company_name', None) net_monthly_salary = request.data.get('net_monthly_salary', None) salary_day = request.data.get('salary_day', None) bank_name = request.data.get('bank_name', None) bank_account_name = request.data.get('bank_account_name', None) bank_account_number1 = request.data.get('bank_account_number1', None) bank_account_number2 = request.data.get('bank_account_number2', None) ifsc = request.data.get('ifsc', None) utm_source = request.data.get('utm_source', None) utm_medium = request.data.get('utm_medium', None) utm_campaign = request.data.get('utm_campaign', None) if bank_account_number2 and bank_account_number1: if not (bank_account_number2 == bank_account_number1): return JsonResponse({ "status": "False", "message": "Bank account number not matched" }) response = send_email( "Quess Registration Request", "quess_registration_request", ['*****@*****.**'], '*****@*****.**', None, str(name) if name else "", str(employee_id) if employee_id else "", str(phone) if phone else "", str(company_name) if company_name else "", str(net_monthly_salary) if net_monthly_salary else "", str(bank_name) if bank_name else "", str(bank_account_name) if bank_account_name else "", str(bank_account_number1) if bank_account_number1 else "", str(ifsc) if ifsc else "", str(salary_day) if salary_day else "", str(email) if email else "", str(utm_source) if utm_source else "", str(utm_medium) if utm_medium else "", str(utm_campaign) if utm_campaign else "", ) if response and response.status_code == 202: return JsonResponse({ "status": "True", "message": "Employee registered successfully" }) else: raise ValidationError({"status": "False", "message": "Something Went Wrong"}) raise ValidationError({"status": "False", "message": "Template Not Found"}) raise ValidationError({"status": "False", "message": "Template Not Given"})
def create(self, request, *args, **kwargs): name = request.data.get('name', None) if not name: raise ValidationError({ "status": 'False', "message": "Name is required." }) phone = request.data.get('phone', None) if not phone: raise ValidationError({ "status": 'False', "message": "Phone is required." }) if phone: if len(phone) != 10: raise ValidationError({ "status": 'False', "message": "Phone length should be 10 digits." }) email = request.data.get('email', None) if not email: raise ValidationError({ "status": 'False', "message": "Email is required" }) if email: regex = '^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$' match = re.match(regex, email) if not match: raise ValidationError({ "status": 'False', "message": "Email is Invalid" }) company = request.data.get('company', None) category = request.data.get('category', None) booking_data = { "name": name, "phone": phone, "email": email, "company": company, "category": category, } serializer = BookingSerializer(data=booking_data) if serializer.is_valid(): serializer.save() company_first_name = company.split(' ')[0] # mail to user send_email( "Activate your " + str(company_first_name) + " Employee Wallet", "booking_email", [email], '*****@*****.**', None, name, company_first_name, ) # mail to admin send_email( str(name) + " from " + str(company) + " wants to connect with you", "booking_admin_email", [ '*****@*****.**', '*****@*****.**', '*****@*****.**' ], '*****@*****.**', None, name, company, email, phone, ) return JsonResponse({ "status": 'True', "message": "Booking created successfully." }) raise ValidationError({ "status": 'False', "message": "Invalid arguments" })
def create(self, request, *args, **kwargs): description = request.data.get('description', None) credit = request.data.get('credit', None) debit = request.data.get('debit', None) status = request.data.get('status', None) employee = request.data.get('employee', None) company = request.data.get('company', None) source = self.request.query_params.get('source', None) fieldset = self.request.query_params.get('fieldset', None) device_name = request.data.get('device_name', None) device_id = request.data.get('device_id', None) ip_address = request.data.get('ip_address', None) disbursement_id = request.data.get('disbursement_id', None) loan_id = request.data.get('loan_id', None) if not description: raise ValidationError({"message": "Description is required."}) if not employee: raise ValidationError({"message": "Employee is required field."}) if not company: raise ValidationError({"message": "Company is required field."}) if employee: employee_object = Employee.objects.filter(id=employee, deleted_at=None).first() if not employee_object: raise ValidationError({"message": "Employee does not exists."}) if not employee_object.mail_enabled: raise ValidationError({"message": "Please Verify Your Email."}) if not employee_object.ifs or not employee_object.bank_account_number: raise ValidationError( {"message": "Please Add Your Bank Details."}) if company: try: Company.objects.get(id=company) except: raise ValidationError({"message": "Company does not exists."}) if source == 'app' and fieldset == 'debit': if debit: if float(debit) <= 0: raise ValidationError({ "status": "False", "message": "Invalid Transfer Amount" }) if float(debit) > float( employee_object.get_available_balance()): raise ValidationError({ "status": "False", "message": "Insufficient Balance" }) if float(debit) < 500: raise ValidationError({ "status": "False", "message": "Minimum withdrawal amount: Rs.500" }) digital_time_stamp = None if employee_object.name and device_name and device_id and ip_address: digital_time_stamp = utilities.generate_digital_time_stamp( employee_object.name, device_name, device_id, ip_address) fees, gst = calculate_fees_with_gst(debit) total_debit = debit + fees + gst last_statement = Statement.objects.filter( employee=employee_object).last() if last_statement: balance = last_statement.balance + total_debit current_due = last_statement.current_due previous_due = last_statement.previous_due else: balance = total_debit current_due = 0 previous_due = 0 statement_data = { "description": description, "debit": total_debit, "withdraw": debit, "fees": fees, "gst": gst, "balance": balance, "current_due": current_due, "previous_due": previous_due, "employee": employee, "company": company, "digital_time_stamp": digital_time_stamp } serializer = StatementSerializer(data=statement_data) if serializer.is_valid(): serializer.save() employee_object.balance = float( employee_object.balance) - float(debit) employee_object.debited = float( employee_object.debited) + float(total_debit) employee_object.withdraw = float( employee_object.withdraw) + float(debit) employee_object.fees = float( employee_object.fees) + float(fees) employee_object.gst = float(employee_object.gst) + float(gst) employee_object.save() # send email to Admin for withdraw request if employee_object: send_email( "Withdraw Request", # subject "withdraw_request", # template [ '*****@*****.**', '*****@*****.**', '*****@*****.**' ], # to_emails '*****@*****.**', None, # attachment=None str(employee_object.name), str(debit), str(employee_object.ifs.bank.name), str(employee_object.bank_account_number), str(employee_object.ifs.code), str(employee_object.id), ) return JsonResponse({ "status": "True", "message": "Statement created successfully.", "description": serializer.data.get('description', None), "debit": debit }) raise ValidationError({ "status": "False", "message": "Invalid Arguments." }) raise ValidationError({ "status": "False", "message": "Missing source and fieldset Params." })