示例#1
0
 def form_valid(self, form):
     alert = form.save()
     send_alert_confirmation_email.delay(alert.email, alert.token)
     message = _('We just sent you an email to validate your alert.')
     self.messages.success(message)
     redirect_url = reverse('search_view')
     return HttpResponseRedirect('{}?{}'.format(redirect_url,
                                                alert.querystring))
    def handle(self, *args, **options):
        # csv file
        csv_path = os.path.abspath(options['csv_file'][0])
        csv_delimiter = options['csv_delimiter']
        # alert details
        alert_querystring = options['querystring']
        alert_title = options['title']
        alert_latest_alert_date = options['latest_alert_date']
        if alert_latest_alert_date:
            alert_latest_alert_date = datetime \
                .strptime(options['latest_alert_date'], '%Y-%m-%d') \
                .replace(hour=12, minute=0)
            alert_latest_alert_date = timezone.make_aware(
                alert_latest_alert_date)
        alert_frequency = options['frequency']
        alert_validated = options['validated']

        # build list of alerts
        self.stdout.write('Building the list of alerts...')
        alerts = []
        with open(csv_path) as csv_file:
            csvreader = csv.DictReader(csv_file, delimiter=csv_delimiter)
            if 'email' not in csvreader.fieldnames:
                raise KeyError('\'email\' column missing')
            for index, row in enumerate(csvreader):
                alert = Alert(email=row['email'])
                if alert_querystring:
                    alert.querystring = alert_querystring
                if alert_title:
                    alert.title = alert_title
                if alert_latest_alert_date:
                    alert.latest_alert_date = alert_latest_alert_date
                if alert_frequency:
                    alert.alert_frequency = alert_frequency
                if alert_validated:
                    alert.validate()
                alerts.append(alert)
                self.stdout.write("Build alert '{}' for {}".format(
                    alert.title, alert.email))

        # create all the alerts simultaneously
        alerts_created = Alert.objects.bulk_create(alerts)

        # send confirmation email
        alerts_created_not_validated = [
            alert for alert in alerts_created if not alert.validated
        ]  # noqa
        self.stdout.write('Sending validation emails for {} alerts'.format(
            len(alerts_created_not_validated)))
        for alert in alerts_created_not_validated:
            send_alert_confirmation_email.delay(alert.email, alert.token)

        self.stdout.write(
            self.style.SUCCESS('Done! %d alerts created.' %
                               (len(alerts_created))))
示例#3
0
    def form_valid(self, form):
        alert = form.save(commit=False)
        if self.request.user.is_authenticated:
            alert.validated = True
        alert.save()

        if not self.request.user.is_authenticated:
            send_alert_confirmation_email.delay(alert.email, alert.token)
            message = 'Nous venons de vous envoyer un e-mail afin de valider votre alerte.'
        else:
            message = 'Votre alerte a bien été créée !'
        self.messages.success(message)
        redirect_url = reverse('search_view')
        if alert.source == 'aides-territoires':
            redirect_url += '?{}'.format(alert.querystring)
        return HttpResponseRedirect(redirect_url)