def handle(self, *args, **options):
     qs = Aid.objects.live()
     serializer = AidSerializerLatest(qs, many=True)
     aids_api_url = urljoin(get_base_url(), reverse('aids-list'))
     api_url = urljoin(get_base_url(), reverse('aids-all'))
     data = {
         'timestamp': timezone.now(),
         'api_url': api_url,
         'aids_api_url': aids_api_url,
         'count': qs.count(),
         'results': serializer.data,
     }
     json_data = JSONRenderer().render(data)
     file_obj = ContentFile(json_data)
     storage.save(settings.ALL_AIDS_DUMP_FILE_PATH, file_obj)
Esempio n. 2
0
def send_connection_email(user_email, body_template='emails/login_token.txt'):
    """Send a login email to the user.

    The email contains a token that can be used once to login.

    We use the default django token generator, that is usually used for
    password resets.
    """
    try:
        user = User.objects.get(email=user_email)
    except User.DoesNotExist:
        # In case we could not find any valid user with the given email
        # we don't raise any exception, because we can't give any hints
        # about whether or not any particular email has an account
        # on our site.
        return

    user_uid = urlsafe_base64_encode(force_bytes(user.pk))
    login_token = default_token_generator.make_token(user)
    login_url = reverse('token_login', args=[user_uid, login_token])
    base_url = get_base_url()
    full_login_url = '{base_url}{url}'.format(base_url=base_url, url=login_url)

    login_email_body = render_to_string(
        body_template, {
            'base_url': base_url,
            'user_name': user.full_name,
            'full_login_url': full_login_url
        })
    send_email(subject=LOGIN_SUBJECT,
               body=login_email_body,
               recipient_list=[user.email],
               from_email=settings.DEFAULT_FROM_EMAIL,
               tags=['connexion', settings.ENV_NAME],
               fail_silently=False)
Esempio n. 3
0
def send_alert_confirmation_email(user_email, alert_token):
    """Send an alert confirmation link to the user.

    The email contains a token that can be used to validate the
    email ownership.
    """
    try:
        alert = Alert.objects.get(token=alert_token)
    except (Alert.DoesNotExist):
        # In case we could not find any valid user with the given email
        # we don't raise any exception, because we can't give any hints
        # about whether or not any particular email has an account
        # on our site.
        return

    # Use the search form to parse the search querydict and
    # extract the perimeter
    querydict = QueryDict(alert.querystring)
    search_form = AidSearchForm(querydict)
    if search_form.is_valid():
        perimeter = search_form.cleaned_data.get('perimeter', None)
    else:
        perimeter = None

    base_url = get_base_url()
    alert_validation_link = reverse('alert_validate_view', args=[alert_token])
    alert_date = '{:%d/%m/%Y %H:%M:%S}'.format(alert.date_created)
    subject = _('Please confirm your Aides-territoires alert (%(date)s)') % {
        'date': alert_date
    }

    if alert.alert_frequency == Alert.FREQUENCIES.daily:
        frequency = _(
            'You will receive a daily email whenever new matching aids will be published.'
        )  # noqa
    else:
        frequency = _(
            'You will receive a weekly email whenever new matching aids will be published.'
        )  # noqa

    text_body = render_to_string(
        TEMPLATE, {
            'base_url': base_url,
            'alert': alert,
            'frequency': frequency,
            'perimeter': perimeter,
            'alert_validation_link': '{}{}'.format(base_url,
                                                   alert_validation_link)
        })
    send_email(subject=subject,
               body=text_body,
               recipient_list=[user_email],
               from_email=settings.DEFAULT_FROM_EMAIL,
               tags=['alerte_confirmation', settings.ENV_NAME],
               fail_silently=False)
Esempio n. 4
0
def send_alert_confirmation_email(user_email, alert_token):
    """Send an alert confirmation link to the user.

    The email contains a token that can be used to validate the
    email ownership.
    """
    try:
        alert = Alert.objects.get(token=alert_token)
    except (Alert.DoesNotExist):
        # In case we could not find any valid user with the given email
        # we don't raise any exception, because we can't give any hints
        # about whether or not any particular email has an account
        # on our site.
        return

    # Use the search form to parse the search querydict and
    # extract the perimeter
    querydict = QueryDict(alert.querystring)
    search_form = AidSearchForm(querydict)
    perimeter = ''
    if search_form.is_valid():
        perimeter = search_form.cleaned_data.get('perimeter') or ''
        perimeter = str(perimeter)

    base_url = get_base_url()
    alert_validation_link = reverse('alert_validate_view', args=[alert_token])
    alert_date = '{:%d/%m/%Y %H:%M:%S}'.format(alert.date_created)

    if alert.alert_frequency == Alert.FREQUENCIES.daily:
        frequency = 'quotidien'
    else:
        frequency = 'hebdomadaire'

    data = {
        'URL_SITE': base_url,
        'FREQUENCE': frequency,
        'PERIMETRE': perimeter,
        'DATE_ALERTE': alert_date,
        'LIEN_VALIDATION': '{}{}'.format(base_url, alert_validation_link)
    }

    send_email_with_template(
        recipient_list=[user_email],
        template_id=settings.SIB_ALERT_CONFIRMATION_EMAIL_TEMPLATE_ID,
        data=data,
        tags=['alerte_confirmation', settings.ENV_NAME],
        fail_silently=False)
Esempio n. 5
0
def send_alert_confirmation_email(user_email, bookmark_id):
    """Send a login email to the user.

    The email contains a token that can be used once to login.

    We use the default django token generator, that is usually used for
    password resets.
    """
    try:
        user = User.objects.get(email=user_email)
        bookmark = Bookmark.objects.get(id=bookmark_id)
    except (User.DoesNotExist, Bookmark.DoesNotExist):
        # In case we could not find any valid user with the given email
        # we don't raise any exception, because we can't give any hints
        # about whether or not any particular email has an account
        # on our site.
        return

    user_uid = urlsafe_base64_encode(force_bytes(user.pk))
    login_token = default_token_generator.make_token(user)
    login_url = reverse('token_login', args=[user_uid, login_token])
    base_url = get_base_url()
    full_login_url = '{base_url}{url}'.format(base_url=base_url, url=login_url)

    if bookmark.alert_frequency == Bookmark.FREQUENCIES.daily:
        frequency = _(
            'You will receive a daily email whenever new matching aids will be published.'
        )  # noqa
    else:
        frequency = _(
            'You will receive a weekly email whenever new matching aids will be published.'
        )  # noqa

    login_email_body = render_to_string(
        TEMPLATE, {
            'base_url': base_url,
            'user_name': user.full_name,
            'full_login_url': full_login_url,
            'bookmark': bookmark,
            'frequency': frequency
        })
    send_mail(SUBJECT,
              login_email_body,
              settings.DEFAULT_FROM_EMAIL, [user.email],
              fail_silently=False)