Example #1
0
 def create(self):
     customer = self.request.organizer.customers.create(
         email=self.cleaned_data['email'],
         name_parts=self.cleaned_data['name_parts'],
         is_active=True,
         is_verified=False,
         locale=get_language_without_region(),
     )
     customer.set_unusable_password()
     customer.save()
     customer.log_action('pretix.customer.created', {})
     ctx = customer.get_email_context()
     token = TokenGenerator().make_token(customer)
     ctx['url'] = build_absolute_uri(
         self.request.organizer, 'presale:organizer.customer.activate'
     ) + '?id=' + customer.identifier + '&token=' + token
     mail(
         customer.email,
         _('Activate your account at {organizer}').format(
             organizer=self.request.organizer.name),
         self.request.organizer.settings.mail_text_customer_registration,
         ctx,
         locale=customer.locale,
         customer=customer,
         organizer=self.request.organizer,
     )
     return customer
Example #2
0
def guess_country(event):
    # Try to guess the initial country from either the country of the merchant
    # or the locale. This will hopefully save at least some users some scrolling :)
    country = event.settings.region or event.settings.invoice_address_from_country
    if not country:
        country = get_country_by_locale(get_language_without_region())
    return country
Example #3
0
    def process_request(self, request: HttpRequest):
        language = get_language_from_request(request)
        # Normally, this middleware runs *before* the event is set. However, on event frontend pages it
        # might be run a second time by pretix.presale.EventMiddleware and in this case the event is already
        # set and can be taken into account for the decision.
        if not request.path.startswith(get_script_prefix() + 'control'):
            if hasattr(request, 'event'):
                settings_holder = request.event
            elif hasattr(request, 'organizer'):
                settings_holder = request.organizer
            else:
                settings_holder = None

            if settings_holder:
                if language not in settings_holder.settings.locales:
                    firstpart = language.split('-')[0]
                    if firstpart in settings_holder.settings.locales:
                        language = firstpart
                    else:
                        language = settings_holder.settings.locale
                        for lang in settings_holder.settings.locales:
                            if lang.startswith(firstpart + '-'):
                                language = lang
                                break
                if '-' not in language and settings_holder.settings.region:
                    language += '-' + settings_holder.settings.region
        else:
            gs = global_settings_object(request)
            if '-' not in language and gs.settings.region:
                language += '-' + gs.settings.region

        translation.activate(language)
        request.LANGUAGE_CODE = get_language_without_region()

        tzname = None
        if hasattr(request, 'event'):
            tzname = request.event.settings.timezone
        elif hasattr(request, 'organizer'
                     ) and 'timezone' in request.organizer.settings._cache():
            tzname = request.organizer.settings.timezone
        elif request.user.is_authenticated:
            tzname = request.user.timezone
        if tzname:
            try:
                timezone.activate(pytz.timezone(tzname))
                request.timezone = tzname
            except pytz.UnknownTimeZoneError:
                pass
        else:
            timezone.deactivate()
Example #4
0
def guess_country(event):
    # Try to guess the initial country from either the country of the merchant
    # or the locale. This will hopefully save at least some users some scrolling :)
    locale = get_language_without_region()
    country = event.settings.region or event.settings.invoice_address_from_country
    if not country:
        valid_countries = countries.countries
        if '-' in locale:
            parts = locale.split('-')
            # TODO: does this actually work?
            if parts[1].upper() in valid_countries:
                country = Country(parts[1].upper())
            elif parts[0].upper() in valid_countries:
                country = Country(parts[0].upper())
        else:
            if locale.upper() in valid_countries:
                country = Country(locale.upper())
    return country
Example #5
0
 def _send_invite(self, instance):
     try:
         mail(
             instance.email,
             _('pretix account invitation'),
             'pretixcontrol/email/invitation.txt',
             {
                 'user': self,
                 'organizer': self.context['organizer'].name,
                 'team': instance.team.name,
                 'url': build_absolute_uri('control:auth.invite', kwargs={
                     'token': instance.token
                 })
             },
             event=None,
             locale=get_language_without_region()  # TODO: expose?
         )
     except SendMailException:
         pass  # Already logged
Example #6
0
    def __iter__(self):
        """
        Iterate through countries, sorted by name, but cache the results based on the locale.
        django-countries performs a unicode-aware sorting based on pyuca which is incredibly
        slow.
        """
        cache_key = "countries:all:{}".format(get_language_without_region())
        if self.cache_subkey:
            cache_key += ":" + self.cache_subkey
        if cache_key in self._cached_lists:
            yield from self._cached_lists[cache_key]
            return

        val = cache.get(cache_key)
        if val:
            self._cached_lists[cache_key] = val
            yield from val
            return

        val = list(super().__iter__())
        self._cached_lists[cache_key] = val
        cache.set(cache_key, val, 3600 * 24 * 30)
        yield from val
Example #7
0
def test_set_region():
    with language('de'):
        assert get_language() == 'de'
        assert get_language_without_region() == 'de'
    with language('de', 'US'):
        assert get_language() == 'de-us'
        assert get_language_without_region() == 'de'
    with language('de', 'DE'):
        assert get_language() == 'de-de'
        assert get_language_without_region() == 'de'
    with language('de-informal', 'DE'):
        assert get_language() == 'de-informal'
        assert get_language_without_region() == 'de-informal'
    with language('pt', 'PT'):
        assert get_language() == 'pt-pt'
        assert get_language_without_region() == 'pt-pt'
    with language('pt-pt', 'BR'):
        assert get_language() == 'pt-pt'
        assert get_language_without_region() == 'pt-pt'