def contact(request): settings = helpers.get_settings() valid_emails = settings.get("VALID_CONTACT_RECIPIENTS", []) name = request.data.get("name", "") message = request.data.get("message", "") email = request.data.get("email", "") phone = request.data.get("phone", "") collaborator_code = request.data.get("collaborator_code", "") recipients = request.data.get("recipients", request.data.get("recipients[]", [])) context = {"name": name, "message": message, "email": email, "phone": phone, "collaborator_code": collaborator_code} if not type(recipients) is list: recipients = [recipients] # Check if all recipients are valid for recipient in recipients: if recipient not in valid_emails: return response.Response({"detail": "Invalid recipients."}, status.HTTP_400_BAD_REQUEST) contact = emails.ContactFormMail(recipients) contact.sendContact(context=context) return response.Response({"success": True})
class BaseMail: """ This class is responsible for firing emails """ from_email = get_settings().get('DEFAULT_FROM_EMAIL', '') def __init__(self, email_address, async_mail=None, locale=None): self.email_address = email_address self.async_mail = async_mail self.locale = locale or get_settings().get('LANGUAGE_CODE', 'en-us') def sendEmail(self, template_name, subject, context={}): if not is_email_enabled(template_name): return False self.__setLocale() subject = get_email_subject(template_name, subject) ctx = Context(inject_client_url(context)) text_content = get_template( 'email/{}.txt'.format(template_name)).render(ctx) html_content = get_template( 'email/{}.html'.format(template_name)).render(ctx) self.__resetLocale() msg = EmailMultiAlternatives(subject, text_content, self.from_email, [self.email_address]) msg.attach_alternative(html_content, "text/html") if self.async_mail: async_flag = "async" elif self.async_mail == None: async_flag = getattr(settings, "DEFAULT_SEND_EMAIL", "async") if async_flag == "async": t = EmailThread(msg) t.start() result = t else: result = msg.send() > 0 return result def __setLocale(self): self.__active_locale = translation.get_language() translation.activate(self.locale) def __resetLocale(self): translation.activate(self.__active_locale) self.__active_locale = None
def inject_client_url(ctx): s = get_settings() ctx['CLIENT_URL'] = s.get("CLIENT_URL", "") return ctx
def __init__(self, email_address, async_mail=None, locale=None): self.email_address = email_address self.async_mail = async_mail self.locale = locale or get_settings().get('LANGUAGE_CODE', 'en-us')
def update_address(sender, instance, **kwargs): if kwargs.get('raw', False): # pragma: no cover return None settings = helpers.get_settings() maps_language = settings.get('MAPS_API_LANGUAGE', 'en_US') addressline = instance.typed_address url = 'https://maps.googleapis.com/maps/api/geocode/json?language={}&address={}'.format( maps_language, addressline) key = os.environ.get('GOOGLE_MAPS_KEY', None) if key: #pragma: no cover url = '{}&key={}'.format(url, key) r = requests.get(url) data = r.json() # Iterate through address components instance.address_components.clear() if len(data['results']) > 0: for component in data['results'][0]['address_components']: # TODO: Do not work only with first result # Look for component with same name and type ac = AddressComponent.objects.annotate( count=Count('types')).filter( long_name=component['long_name'], short_name=component['short_name']) for component_type in component['types']: ac = ac.filter(types__name=component_type) ac = ac.filter(count=len(component['types'])) if not ac.count(): # Component not found, creating ac = AddressComponent(long_name=component['long_name'], short_name=component['short_name']) ac.save() else: ac = ac.first() ac.types.clear() ac.save() # Add types for component for ctype in component['types']: try: at = AddressComponentType.objects.get(name=ctype) except ObjectDoesNotExist: at = AddressComponentType(name=ctype) at.save() ac.types.add(at) instance.address_components.add(ac) try: if data['results'][0]['geometry']: GoogleAddress.objects.filter(pk=instance.pk).update( lat=data['results'][0]['geometry']['location']['lat'], lng=data['results'][0]['geometry']['location']['lng']) except: #pragma: no cover pass # Using update to avoid post_save signal GoogleAddress.objects.filter(pk=instance.pk).update( address_line=instance.get_address(), city_state=instance.get_city_state())
def __init__(self, project, async_mail=None): s = get_settings() email = s.get('ADMIN_MAIL', None) super(OrganizationAdminMail, self).__init__(email, async_mail)