Esempio n. 1
0
    def on_item_changed(self, item_value):
        old_active = self.active
        result, exception = self.check_condition(item_value)
        if self.active and not result:
            self.active = False
            self.active_on = None
            self.save()
        elif not self.active and result:
            self.active = True
            self.active_on = time_utils.datetime_now()
            self.save()

        if old_active != self.active:
            email = [u.email for u in self.alert_user_set.all()]
            if email:
                send_mail(
                    _("BanBrick Alert"),
                    "",
                    getattr(settings, "EMAIL_HOST_USER"),
                    email,
                    fail_silently=True,
                    html_message=loader.render_to_string(
                        "trigger_alerts.html", {"model": alert_context(self)},
                    )
                )
Esempio n. 2
0
    def post(self, request):
        logger.info("User[%s] require authentication", request.data.get("u"))
        serializer = self.serializer_class(data={
            "username": request.data.get("u"),
            "password": request.data.get("p"),
        })
        if not serializer.is_valid():
            return Response(
                serializer.errors,
                status=status.HTTP_400_BAD_REQUEST,
            )

        now = datetime_now()
        with transaction.atomic():
            Token.objects.filter(
                user=serializer.validated_data['user'],
            ).delete()
            token = Token.objects.create(
                created=now, user=serializer.validated_data['user'],
            )
        logger.info("User[%s] authenticate successed", token.user)

        return Response({'token': token.key})