def log_action(self, action, data=None, user=None, api_token=None, auth=None, save=True): """ Create a LogEntry object that is related to this object. See the LogEntry documentation for details. :param action: The namespaced action code :param data: Any JSON-serializable object :param user: The user performing the action (optional) """ from .log import LogEntry from .event import Event from .devices import Device from pretix.api.models import OAuthAccessToken, OAuthApplication from .organizer import TeamAPIToken from ..notifications import get_all_notification_types from ..services.notifications import notify from pretix.api.webhooks import get_all_webhook_events, notify_webhooks event = None if isinstance(self, Event): event = self elif hasattr(self, 'event'): event = self.event if user and not user.is_authenticated: user = None kwargs = {} if isinstance(auth, OAuthAccessToken): kwargs['oauth_application'] = auth.application elif isinstance(auth, OAuthApplication): kwargs['oauth_application'] = auth elif isinstance(auth, TeamAPIToken): kwargs['api_token'] = auth elif isinstance(auth, Device): kwargs['device'] = auth elif isinstance(api_token, TeamAPIToken): kwargs['api_token'] = api_token logentry = LogEntry(content_object=self, user=user, action_type=action, event=event, **kwargs) if data: logentry.data = json.dumps(data, cls=CustomJSONEncoder) if save: logentry.save() no_types = get_all_notification_types() wh_types = get_all_webhook_events() no_type = None wh_type = None typepath = logentry.action_type while (not no_type or not wh_types) and '.' in typepath: wh_type = wh_type or wh_types.get(typepath + ('.*' if typepath != logentry.action_type else '')) no_type = no_type or no_types.get(typepath + ('.*' if typepath != logentry.action_type else '')) typepath = typepath.rsplit('.', 1)[0] if no_type: notify.apply_async(args=(logentry.pk,)) if wh_type: notify_webhooks.apply_async(args=(logentry.pk,)) return logentry
def __init__(self, *args, **kwargs): organizer = kwargs.pop('organizer') super().__init__(*args, **kwargs) self.fields['limit_events'].queryset = organizer.events.all() self.fields['events'].choices = [ ( a.action_type, mark_safe('{} – <code>{}</code>'.format(a.verbose_name, a.action_type)) ) for a in get_all_webhook_events().values() ] if self.instance: self.fields['events'].initial = list(self.instance.listeners.values_list('action_type', flat=True))
def __init__(self, *args, **kwargs): organizer = kwargs.pop('organizer') super().__init__(*args, **kwargs) self.fields['limit_events'].queryset = organizer.events.all() self.fields['events'].choices = [ (a.action_type, mark_safe('{} – <code>{}</code>'.format(a.verbose_name, a.action_type))) for a in get_all_webhook_events().values() ] if self.instance: self.fields['events'].initial = list( self.instance.listeners.values_list('action_type', flat=True))
def log_action(self, action, data=None, user=None, api_token=None, auth=None, save=True): """ Create a LogEntry object that is related to this object. See the LogEntry documentation for details. :param action: The namespaced action code :param data: Any JSON-serializable object :param user: The user performing the action (optional) """ from pretix.api.models import OAuthAccessToken, OAuthApplication from pretix.api.webhooks import get_all_webhook_events, notify_webhooks from ..notifications import get_all_notification_types from ..services.notifications import notify from .devices import Device from .event import Event from .log import LogEntry from .organizer import TeamAPIToken event = None if isinstance(self, Event): event = self elif hasattr(self, 'event'): event = self.event if user and not user.is_authenticated: user = None kwargs = {} if isinstance(auth, OAuthAccessToken): kwargs['oauth_application'] = auth.application elif isinstance(auth, OAuthApplication): kwargs['oauth_application'] = auth elif isinstance(auth, TeamAPIToken): kwargs['api_token'] = auth elif isinstance(auth, Device): kwargs['device'] = auth elif isinstance(api_token, TeamAPIToken): kwargs['api_token'] = api_token logentry = LogEntry(content_object=self, user=user, action_type=action, event=event, **kwargs) if isinstance(data, dict): sensitivekeys = ['password', 'secret', 'api_key'] for sensitivekey in sensitivekeys: for k, v in data.items(): if (sensitivekey in k) and v: data[k] = "********" logentry.data = json.dumps(data, cls=CustomJSONEncoder, sort_keys=True) elif data: raise TypeError("You should only supply dictionaries as log data.") if save: logentry.save() no_types = get_all_notification_types() wh_types = get_all_webhook_events() no_type = None wh_type = None typepath = logentry.action_type while (not no_type or not wh_types) and '.' in typepath: wh_type = wh_type or wh_types.get(typepath + ( '.*' if typepath != logentry.action_type else '')) no_type = no_type or no_types.get(typepath + ( '.*' if typepath != logentry.action_type else '')) typepath = typepath.rsplit('.', 1)[0] if no_type: notify.apply_async(args=(logentry.pk, )) if wh_type: notify_webhooks.apply_async(args=(logentry.pk, )) return logentry
def to_internal_value(self, data): types = get_all_webhook_events() for d in data: if d not in types: raise ValidationError('Invalid action type "%s".' % d) return {'action_types': data}