def create_mail(request, account_id): if request.method == 'GET': account = get_object_or_404(Account, pk=account_id) contacts_list = list(account.contacts.all().values('email')) email_form = EmailForm() return render( request, 'create_mail_accounts.html', { 'account_id': account_id, 'contacts_list': contacts_list, 'email_form': email_form }) if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): send_email.delay(form.data.get('message_subject'), form.data.get('message_body'), from_email=account_id, recipients=form.data.get('recipients').split(',')) return JsonResponse({'error': False}) else: return JsonResponse({'error': True, 'errors': form.errors}) # @login_required # def get_account_details(request, account_id): # from django.core import serializers # import json # fields = ['name', 'email', 'phone', 'industry', 'billing_address_line', 'billing_street', 'billing_city', # 'billing_state', 'billing_postcode', 'billing_country', 'website', 'description', # 'created_by__email', 'created_on', 'tags__name', 'status', 'contacts__email', 'assigned_to__email'] # data = serializers.serialize('json', Account.objects.filter(id=account_id), fields=fields) # return JsonResponse({'data': json.loads(data)[0]})
def post(self, request, pk, *args, **kwargs): params = request.query_params if len( request.data) == 0 else request.data scheduled_later = params.get("scheduled_later") scheduled_date_time = params.get("scheduled_date_time") account = Account.objects.filter(id=pk).first() serializer = EmailSerializer( data=params, request_obj=request, # account=account, ) data = {} if serializer.is_valid(): email_obj = serializer.save(from_account=account) if scheduled_later not in ["", None, False, "false"]: if scheduled_date_time in ["", None]: return Response( { "error": True, "errors": serializer.errors }, status=status.HTTP_400_BAD_REQUEST, ) if params.get("recipients"): contacts = json.loads(params.get("recipients")) for contact in contacts: obj_contact = Contact.objects.filter(id=contact, org=request.org) if obj_contact.exists(): email_obj.recipients.add(contact) else: email_obj.delete() data["recipients"] = "Please enter valid recipient" return Response({"error": True, "errors": data}) if params.get("scheduled_later") != "true": send_email.delay(email_obj.id) else: email_obj.scheduled_later = True email_obj.scheduled_date_time = scheduled_date_time return Response( { "error": False, "message": "Email sent successfully" }, status=status.HTTP_200_OK, ) return Response( { "error": True, "errors": serializer.errors }, status=status.HTTP_400_BAD_REQUEST, )
def create_mail(request): if request.method == "GET": account = get_object_or_404(Account, pk=request.GET.get("account_id")) contacts_list = list(account.contacts.all().values("email")) TIMEZONE_CHOICES = [(tz, tz) for tz in pytz.common_timezones] email_form = EmailForm(account=account) return render( request, "create_mail_accounts.html", { "account_id": request.GET.get("account_id"), "contacts_list": contacts_list, "email_form": email_form, "timezones": TIMEZONE_CHOICES, "settings_timezone": settings.TIME_ZONE, }, ) if request.method == "POST": account = Account.objects.filter( id=request.POST.get("account_id")).first() if account: form = EmailForm(request.POST, account=account) if form.is_valid(): contacts = form.data.get("recipients").split(",") email_obj = Email.objects.create( from_account=account, message_body=form.cleaned_data.get("message_body"), message_subject=form.cleaned_data.get("message_subject"), from_email=form.cleaned_data.get("from_email"), timezone=form.cleaned_data.get("timezone"), ) email_obj.recipients.add(*contacts) if request.POST.get("scheduled_later") != "true": send_email.delay(email_obj.id) else: email_obj.scheduled_later = True email_obj.scheduled_date_time = form.cleaned_data.get( "scheduled_date_time") email_obj.save() return JsonResponse({"error": False}) else: return JsonResponse({"error": True, "errors": form.errors}) else: return JsonResponse({ "error": True, "message": "Account does not exist." })
def create_mail(request): if request.method == 'GET': account = get_object_or_404(Account, pk=request.GET.get('account_id')) contacts_list = list(account.contacts.all().values('email')) TIMEZONE_CHOICES = [(tz, tz) for tz in pytz.common_timezones] email_form = EmailForm(account=account) return render( request, 'create_mail_accounts.html', { 'account_id': request.GET.get('account_id'), 'contacts_list': contacts_list, 'email_form': email_form, "timezones": TIMEZONE_CHOICES, "settings_timezone": settings.TIME_ZONE }) if request.method == 'POST': account = Account.objects.filter( id=request.POST.get('account_id')).first() if account: form = EmailForm(request.POST, account=account) if form.is_valid(): contacts = form.data.get('recipients').split(',') email_obj = Email.objects.create( from_account=account, message_body=form.cleaned_data.get('message_body'), message_subject=form.cleaned_data.get('message_subject'), from_email=form.cleaned_data.get('from_email'), timezone=form.cleaned_data.get('timezone'), ) email_obj.recipients.add(*contacts) if request.POST.get('scheduled_later') != 'true': send_email.delay(email_obj.id) else: email_obj.scheduled_later = True email_obj.scheduled_date_time = form.cleaned_data.get( 'scheduled_date_time') email_obj.save() return JsonResponse({'error': False}) else: return JsonResponse({'error': True, 'errors': form.errors}) else: return JsonResponse({ 'error': True, 'message': "Account does not exist." })
def create_mail(request, account_id): if request.method == 'GET': account = get_object_or_404(Account, pk=account_id) contacts_list = list(account.contacts.all().values('email')) email_form = EmailForm() return render(request, 'create_mail_accounts.html', {'account_id': account_id, 'contacts_list': contacts_list, 'email_form': email_form}) if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): send_email.delay(form.data.get('message_subject'), form.data.get('message_body'), from_email=account_id, recipients=form.data.get('recipients').split(',')) return JsonResponse({'error': False}) else: return JsonResponse({'error': True, 'errors': form.errors})
def form_valid(self, form): email = form.cleaned_data['email'] try: user = User.objects.get(email=email) password = uuid.uuid4().hex user.set_password(password) user.save() user.password = password email_data = { 'to': [user.email], 'subject': 'Reset Password', 'context': {'user': user}, 'template_name': 'accounts/email/register_notification.html' } send_email.delay(**email_data) return super(ResetPasswordView, self).form_valid(form) except: messages.error(self.request, 'Email doesn\'t exists') return self.form_invalid(form)
def form_valid(self, form): data = form.cleaned_data params = { 'email': data['email'], 'first_name': data['first_name'], 'last_name': data['last_name'] } user = User(**params) password = uuid.uuid4().hex user.set_password(password) user.save() user.password = password email_data = { 'to': [user.email], 'subject': 'Registration Successfully', 'context': {'user': user}, 'template_name': 'accounts/reset_password.html' } send_email.delay(**email_data) return super(RegisterView, self).form_valid(form)