def multiexport(self, organizer: Organizer, user: User, fileid: str, provider: str, form_data: Dict[str, Any]) -> None: def set_progress(val): if not self.request.called_directly: self.update_state( state='PROGRESS', meta={'value': val} ) file = CachedFile.objects.get(id=fileid) with language(user.locale), override(user.timezone): allowed_events = user.get_events_with_permission('can_view_orders') events = allowed_events.filter(pk__in=form_data.get('events')) responses = register_multievent_data_exporters.send(organizer) for receiver, response in responses: if not response: continue ex = response(events, set_progress) if ex.identifier == provider: d = ex.render(form_data) if d is None: raise ExportError( gettext('Your export did not contain any data.') ) file.filename, file.type, data = d file.file.save(cachedfile_name(file, file.filename), ContentFile(data)) file.save() return file.pk
def exporters(self): exporters = [] events = (self.request.auth or self.request.user).get_events_with_permission('can_view_orders', request=self.request).filter( organizer=self.request.organizer ) responses = register_multievent_data_exporters.send(self.request.organizer) for ex in sorted([response(events, self.request.organizer) for r, response in responses if response], key=lambda ex: str(ex.verbose_name)): ex._serializer = JobRunSerializer(exporter=ex, events=events) exporters.append(ex) return exporters
def multiexport(self, organizer: Organizer, user: User, device: int, token: int, fileid: str, provider: str, form_data: Dict[str, Any]) -> None: if device: device = Device.objects.get(pk=device) if token: device = TeamAPIToken.objects.get(pk=token) allowed_events = (device or token or user).get_events_with_permission('can_view_orders') def set_progress(val): if not self.request.called_directly: self.update_state(state='PROGRESS', meta={'value': val}) file = CachedFile.objects.get(id=fileid) if user: locale = user.locale timezone = user.timezone region = None # todo: add to user? else: e = allowed_events.first() if e: locale = e.settings.locale timezone = e.settings.timezone region = e.settings.region else: locale = settings.LANGUAGE_CODE timezone = settings.TIME_ZONE region = None with language(locale, region), override(timezone): if isinstance(form_data['events'][0], str): events = allowed_events.filter(slug__in=form_data.get('events'), organizer=organizer) else: events = allowed_events.filter(pk__in=form_data.get('events')) responses = register_multievent_data_exporters.send(organizer) for receiver, response in responses: if not response: continue ex = response(events, set_progress) if ex.identifier == provider: d = ex.render(form_data) if d is None: raise ExportError( gettext('Your export did not contain any data.')) file.filename, file.type, data = d file.file.save(cachedfile_name(file, file.filename), ContentFile(data)) file.save() return file.pk
def exporters(self): exporters = [] if isinstance(self.request.auth, (Device, TeamAPIToken)): perm_holder = self.request.auth else: perm_holder = self.request.user events = perm_holder.get_events_with_permission( 'can_view_orders', request=self.request).filter(organizer=self.request.organizer) responses = register_multievent_data_exporters.send( self.request.organizer) for ex in sorted([ response(events, self.request.organizer) for r, response in responses if response ], key=lambda ex: str(ex.verbose_name)): ex._serializer = JobRunSerializer(exporter=ex, events=events) exporters.append(ex) return exporters
def multiexport(organizer: Organizer, user: User, fileid: str, provider: str, form_data: Dict[str, Any]) -> None: file = CachedFile.objects.get(id=fileid) with language(user.locale), override(user.timezone): allowed_events = user.get_events_with_permission('can_view_orders') events = allowed_events.filter(pk__in=form_data.get('events')) responses = register_multievent_data_exporters.send(organizer) for receiver, response in responses: ex = response(events) if ex.identifier == provider: d = ex.render(form_data) if d is None: raise ExportError( gettext('Your export did not contain any data.')) file.filename, file.type, data = d file.file.save(cachedfile_name(file, file.filename), ContentFile(data)) file.save() return file.pk
def handle(self, *args, **options): try: o = Organizer.objects.get(slug=options['organizer_slug']) except Organizer.DoesNotExist: self.stderr.write(self.style.ERROR('Organizer not found.')) sys.exit(1) locale = options.get("locale", None) timezone = pytz.timezone( options['timezone']) if options.get('timezone') else None with scope(organizer=o): if options['event_slug']: try: e = o.events.get(slug=options['event_slug']) except Event.DoesNotExist: self.stderr.write(self.style.ERROR('Event not found.')) sys.exit(1) if not locale: locale = e.settings.locale if not timezone: timezone = e.settings.timezone signal_result = register_data_exporters.send(e) else: e = o.events.all() if options['event_slugs']: e = e.filter(slug__in=options['event_slugs']) not_found = set(options['event_slugs']).difference( event.slug for event in e) if not_found: self.stderr.write( self.style.ERROR( 'The following events were not found: {}'. format(", ".join(not_found)))) sys.exit(1) if not e.exists(): self.stderr.write(self.style.ERROR('No events found.')) sys.exit(1) if not locale: locale = e.first().settings.locale self.stderr.write( self.style.WARNING( "Guessing locale '{}' based on event '{}'.".format( locale, e.first().slug))) if not timezone: timezone = e.first().settings.timezone self.stderr.write( self.style.WARNING( "Guessing timezone '{}' based on event '{}'.". format(timezone, e.first().slug))) signal_result = register_multievent_data_exporters.send(o) pbar = tqdm(total=100) def report_status(val): pbar.update(round(val, 2) - pbar.n) with language(locale), override(timezone): for receiver, response in signal_result: ex = response(e, o, report_status) if ex.identifier == options['export_provider']: params = json.loads(options.get('parameters') or '{}') with open(options['output_file'], 'wb') as f: try: ex.render(form_data=params, output_file=f) except TypeError: self.stderr.write( self.style.WARNING( 'Provider does not support direct file writing, need to buffer export in memory.' )) d = ex.render(form_data=params) if d is None: self.stderr.write( self.style.ERROR('Empty export.')) sys.exit(2) f.write(d[2]) sys.exit(0) pbar.close() self.stderr.write(self.style.ERROR('Export provider not found.')) sys.exit(1)