class AlertLimitReached(Event): cache_key_fmt = "stock_alert_%s_%s" identifier = "alert_limit_reached" name = _("Alert Limit Reached") supplier = Variable(_("Supplier"), type=Model("wshop.Supplier")) product = Variable(_("Product"), type=Model("wshop.Product")) dispatched_last_24hs = Variable( _("Fired in the last 24 hours?"), type=Boolean, help_text=_( "This will be True if this event was already dispatched " "in the last 24 hours for the same product and supplier. " "This is useful to prevent sending identical notifications in a short " "period of time.")) def __init__(self, **variable_values): cache_key = self.cache_key_fmt % (variable_values["supplier"].pk, variable_values["product"].pk) last_dispatch_time = cache.get(cache_key) if last_dispatch_time: last_dispatch = int((time() - last_dispatch_time) / 60 / 60) variable_values["dispatched_last_24hs"] = (last_dispatch < 24) else: variable_values["dispatched_last_24hs"] = False super(AlertLimitReached, self).__init__(**variable_values) def run(self, shop): cache_key = self.cache_key_fmt % (self.variable_values["supplier"].pk, self.variable_values["product"].pk) cache.set(cache_key, time(), timeout=(60 * 60 * 24)) super(AlertLimitReached, self).run(shop=shop)
class PaymentCreated(Event): identifier = "payment_created" name = _("Payment Created") order = Variable(_("Order"), type=Model("wshop.Order")) customer_email = Variable(_("Customer Email"), type=Email) customer_phone = Variable(_("Customer Phone"), type=Phone) language = Variable(_("Language"), type=Language) payment_status = Variable(_("Order Payment Status"), type=Enum(PaymentStatus)) payment = Variable(_("Payment"), type=Model("wshop.Payment"))
class ShipmentDeleted(Event): identifier = "shipment_deleted" name = _("Shipment Deleted") order = Variable(_("Order"), type=Model("wshop.Order")) customer_email = Variable(_("Customer Email"), type=Email) customer_phone = Variable(_("Customer Phone"), type=Phone) language = Variable(_("Language"), type=Language) shipment = Variable(_("Shipment"), type=Model("wshop.Shipment")) shipping_status = Variable(_("Order Shipping Status"), type=Enum(ShippingStatus))
class RegistrationReceived(Event): identifier = "registration_received" name = _("Registration Received") customer = Variable(_("Customer"), type=Model("wshop.Contact")) customer_email = Variable(_("Customer Email"), type=Email) activation_url = Variable(_("Activation URL"), type=URL, required=False) user_is_active = Variable(_("Is User Active"), type=Boolean)
class OrderReceived(Event): identifier = "order_received" name = _("Order Received") order = Variable(_("Order"), type=Model("wshop.Order")) customer_email = Variable(_("Customer Email"), type=Email) customer_phone = Variable(_("Customer Phone"), type=Phone) shop_email = Variable(_("Shop Email"), type=Email) shop_phone = Variable(_("Shop Phone"), type=Phone) language = Variable(_("Language"), type=Language)
class AddOrderLogEntry(Action): identifier = "add_order_log_entry" order = Binding("Order", Model("wshop.Order"), constant_use=ConstantUse.VARIABLE_ONLY) message = Binding("Message", Text, constant_use=ConstantUse.VARIABLE_OR_CONSTANT) message_identifier = Binding("Message Identifier", Text, constant_use=ConstantUse.VARIABLE_OR_CONSTANT) def execute(self, context): order = self.get_value(context, "order") if not order: # pragma: no cover return message = self.get_value(context, "message") message_identifier = self.get_value(context, "message_identifier") or None assert isinstance(order, Order) order.add_log_entry(message=message, identifier=message_identifier)
class AddNotification(Action): identifier = "add_notification" recipient_type = Binding( "Recipient Type", type=Enum(RecipientType), constant_use=ConstantUse.CONSTANT_ONLY, default=RecipientType.ADMINS ) recipient = Binding( "Recipient", type=Model(settings.AUTH_USER_MODEL), constant_use=ConstantUse.VARIABLE_OR_CONSTANT, required=False ) priority = Binding("Priority", type=Enum(Priority), constant_use=ConstantUse.CONSTANT_ONLY, default=Priority.NORMAL) message = TemplatedBinding("Message", type=Text, constant_use=ConstantUse.CONSTANT_ONLY, required=True) message_identifier = Binding("Message Identifier", Text, constant_use=ConstantUse.CONSTANT_ONLY, required=False) url = Binding("URL", type=URL, constant_use=ConstantUse.VARIABLE_OR_CONSTANT) def execute(self, context): """ :type context: wshop.notify.script.Context """ values = self.get_values(context) if values["recipient_type"] == RecipientType.SPECIFIC_USER: if not values["recipient"]: context.log(logging.WARN, "Misconfigured AddNotification -- no recipient for specific user") return Notification.objects.create( recipient_type=values["recipient_type"], recipient=values["recipient"], priority=values["priority"], identifier=values.get("message_identifier"), message=values["message"][:140], url=values["url"], shop=context.shop )
def test_model_type_matching(): assert empty_iterable( Binding("x", type=Model("wshop.Contact")).get_matching_types( ATestEvent.variables)) assert Binding("x", type=Model("wshop.Order")).get_matching_types( ATestEvent.variables) == {"order"}