示例#1
0
    def get(self, message_queue_name):
        """ Liefert eine Übersicht über die Nachrichten in der Queue zurück.

        Kann in JSON, XML, oder plaintext abgefragt werden.
        Für eine Beschreibung des Formats siehe README.
        """
        self.on_access(message_queue_name)
        query = Message.query(
            Message.message_queue_name == message_queue_name, Message.deleted_at == None).order(
            Message.created_at)
        messages = query.fetch(self.max_messages)

        document = {
            'min_retry_interval': self.min_retry_interval,
            'max_retry_interval': self.max_retry_interval,
            'messages': [self._message_as_dict(msg) for msg in messages],
        }

        accept = self.request.headers.get('Accept', '')

        if accept.startswith('application/json'):
            self.response.headers["Content-Type"] = 'application/json'
            self.response.out.write(json.dumps(document))
        elif accept.startswith('application/xml'):
            self.response.headers["Content-Type"] = 'application/xml'
            self.response.out.write(dict2xml(document, listnames={'messages': 'message'}))
        else:
            self.response.headers["Content-Type"] = 'text/plain'
            self.response.out.write('\n'.join(x['url'] for x in document['messages']))
示例#2
0
 def toXML(self):
     return structured.dict2xml(self.toDict(),
                                roottag='resources',
                                listnames={
                                    'locations': 'location',
                                    'resources': 'resource',
                                    'study_areas': 'studyarea',
                                    'events': 'event'
                                },
                                pretty=True)
示例#3
0
    def _set_payload_data(self, out):
        if self.environ['is_json']:
            content_type = 'application/json'
            out = dumps(out)
        else:
            content_type = 'application/xml'
            out = dict2xml(out)

        self.response.headers['Content-Type'] = content_type
        self.response.payload = out
示例#4
0
    def _set_payload_data(self, out, status_code=OK):
        if self.environ['is_json']:
            content_type = 'application/json'
            out = dumps(out)
        else:
            content_type = 'application/xml'
            out = dict2xml(out)

        self.response.headers['Content-Type'] = content_type
        self.response.payload = out
        self.response.status_code = status_code
示例#5
0
    def _invoke_callbacks(self):
        callback_consumers = list(self.pubsub.impl.get_callback_consumers())
        self.logger.debug('Callback consumers found `%s`', callback_consumers)

        for consumer in callback_consumers:
            with self.lock(consumer.sub_key):
                msg_ids = []

                out = {'status': ZATO_OK, 'results_count': 0, 'results': []}

                messages = self.pubsub.get(
                    consumer.sub_key, get_format=PUB_SUB.GET_FORMAT.JSON.id)

                for msg in messages:
                    msg_ids.append(msg['metadata']['msg_id'])
                    out['results_count'] += 1
                    out['results'].append(msg)

                # messages is a generator so we still don't know if we had anything.
                if msg_ids:
                    outconn = self.outgoing.plain_http[consumer.callback_name]

                    if outconn.config['data_format'] == DATA_FORMAT.XML:
                        out = dict2xml(out)
                        content_type = 'application/xml'
                    else:
                        out = dumps(out)
                        content_type = 'application/json'

                    try:
                        response = outconn.conn.post(
                            self.cid,
                            data=out,
                            headers={'content-type': content_type})
                    except Exception, e:
                        self._reject(msg_ids, consumer.sub_key, consumer,
                                     format_exc(e))
                    else:
                        if response.status_code == OK:
                            self.pubsub.acknowledge(consumer.sub_key, msg_ids)
                        else:
                            self._reject(
                                msg_ids, consumer.sub_key, consumer,
                                '`{}` `{}`'.format(response.status_code,
                                                   response.text))
示例#6
0
    def _invoke_callbacks(self):
        callback_consumers = list(self.pubsub.impl.get_callback_consumers())
        self.logger.debug('Callback consumers found `%s`', callback_consumers)

        for consumer in callback_consumers:
            with self.lock(consumer.sub_key):
                msg_ids = []

                out = {
                    'status': ZATO_OK,
                    'results_count': 0,
                    'results': []
                }

                messages = self.pubsub.get(consumer.sub_key, get_format=PUB_SUB.GET_FORMAT.JSON.id)

                for msg in messages:
                    msg_ids.append(msg['metadata']['msg_id'])
                    out['results_count'] += 1
                    out['results'].append(msg)

                # messages is a generator so we still don't know if we had anything.
                if msg_ids:
                    outconn = self.outgoing.plain_http[consumer.callback_name]

                    if outconn.config['data_format'] == DATA_FORMAT.XML:
                        out = dict2xml(out)
                        content_type = 'application/xml'
                    else:
                        out = dumps(out)
                        content_type = 'application/json'

                    try:
                        response = outconn.conn.post(self.cid, data=out, headers={'content-type': content_type})
                    except Exception, e:
                        self._reject(msg_ids, consumer.sub_key, consumer, format_exc(e))
                    else:
                        if response.status_code == OK:
                            self.pubsub.acknowledge(consumer.sub_key, msg_ids)
                        else:
                            self._reject(
                                msg_ids, consumer.sub_key, consumer, '`{}` `{}`'.format(response.status_code, response.text))
示例#7
0
    def as_xml(self):
        """
        Return the data as an XML object
        """
        if not self.data:
            logger.warning("Data is empty. Call collect_data before exporting output.")
            return EMPTY_DATA_XML

        try:
            from huTools.structured import dict2xml
            from huTools.structured import list2xml
        except ImportError:
            logger.error("Report.as_xml could not run. Requires huTools.")
            return EMPTY_DATA_XML

        if isinstance(self.data, dict):
            return dict2xml(self.data, roottag="report")
        elif isinstance(self.data, list):
            return list2xml(self.data, root="report", elementname="item")
        else:
            raise Exception("Data must be a list or dict")
 def toXML(self):
     if self.full:
         return self.data.toxml()
     return structured.dict2xml(self.toDict(), roottag='book', pretty=True)
示例#9
0
 def toXML(self):
     return self._add_xml_header(structured.dict2xml(self.toDict(),roottag='siteindex',listnames={'pages': 'page'},pretty=True))
示例#10
0
 def toXML(self):
     return structured.dict2xml(self.toDict(),
                                roottag='Responses',
                                pretty=True)
 def toXML(self):
     return structured.dict2xml(self.toDict(),roottag='response',listnames={'databases': 'database'},pretty=True)
 def toXML(self):
     return structured.dict2xml(self.toDict(),roottag='analytics',pretty=True)
示例#13
0
 def toXML(self):
     return structured.dict2xml(self.toDict(),
                                roottag='subject',
                                pretty=True)
示例#14
0
def home(request):
    """Show the form to export data"""

    if request.method == 'POST':  # If the form has been submitted...
        form = ExportForm(request.user, request.POST)

        if form.is_valid():  # If the form is valid

            config = form.cleaned_data['config']
            all_config = form.cleaned_data['all_config']
            range = form.cleaned_data['range']
            file_type = form.cleaned_data['file_type']
            only_good = form.cleaned_data['only_good']

            if not request.user.is_superuser and all_config:
                raise Http404

            if not request.user.is_superuser and not request.user in config.allowed_users:
                raise Http404

            transactions = Transaction.objects.order_by('creation_date')

            file_name = u'Export_'

            if not all_config:
                transactions = transactions.filter(config=config)

                file_name += config.name + u'_'

            else:

                file_name += u'ALL_'

            if only_good:

                file_name += u'_Goods_'

                transactions = transactions.filter(
                    Q(postfinance_status__startswith='5')
                    | Q(postfinance_status__startswith='9'))

            if range == 'thismonth':
                start_date = now() + relativedelta(
                    day=1, minute=0, hour=0, second=0, microsecond=0)
                end_date = now() + relativedelta(day=1,
                                                 months=+1,
                                                 seconds=-1,
                                                 minute=0,
                                                 hour=0,
                                                 second=0,
                                                 microsecond=0)
            elif range == 'previousmonth':
                start_date = now() + relativedelta(day=1,
                                                   months=-1,
                                                   minute=0,
                                                   hour=0,
                                                   second=0,
                                                   microsecond=0)
                end_date = now() + relativedelta(day=1,
                                                 seconds=-1,
                                                 minute=0,
                                                 hour=0,
                                                 second=0,
                                                 microsecond=0)
            elif range == 'sincemonth':
                start_date = now() + relativedelta(months=-1)
                end_date = now()
            elif range == 'thisyear':
                start_date = now() + relativedelta(
                    day=1, month=1, minute=0, hour=0, second=0, microsecond=0)
                end_date = now() + relativedelta(day=1,
                                                 month=1,
                                                 years=+1,
                                                 seconds=-1,
                                                 minute=0,
                                                 hour=0,
                                                 second=0,
                                                 microsecond=0)
            elif range == 'sinceyear':
                start_date = now() + relativedelta(years=-1)
                end_date = now()
            elif range == 'custom':
                start_date = form.cleaned_data['custom_start']
                end_date = form.cleaned_data['custom_end']

            transactions = transactions.filter(
                creation_date__gte=start_date,
                creation_date__lt=end_date).all()

            file_name += start_date.strftime(
                '%Y-%m-%d_%H.%M.%S') + u' - ' + end_date.strftime(
                    '%Y-%m-%d_%H.%M.%S')

            # Commong for json and xml
            data = [
                dict(
                    tr.dump_api(add_config=all_config).items() + {
                        'logs': [
                            log.dump_api() for log in
                            tr.transactionlog_set.order_by('when').all()
                        ]
                    }.items()) for tr in transactions
            ]

            if file_type == 'json':

                response = HttpResponse(json.dumps(data),
                                        content_type='text/json')

                response[
                    'Content-Disposition'] = 'attachment; filename="%s.json"' % (
                        file_name, )

                return response

            elif file_type == 'xml':

                response = HttpResponse(dict2xml({'export': data},
                                                 pretty=True),
                                        content_type='text/xml')

                response[
                    'Content-Disposition'] = 'attachment; filename="%s.xml"' % (
                        file_name, )

                return response

            elif file_type == 'csv':

                response = HttpResponse(content_type='text/csv')
                response[
                    'Content-Disposition'] = 'attachment; filename="%s.csv"' % (
                        file_name, )

                writer = csv.writer(response)

                headers = [
                    'reference', 'extra_data', 'amount', 'amount_chf',
                    'postfinance_id', 'postfinance_status', 'internal_status',
                    'ipn_needed', 'creation_date', 'last_userforwarded_date',
                    'last_user_back_from_postfinance_date',
                    'last_postfinance_ipn_date', 'last_ipn_date',
                    'postfinance_status_text', 'internal_status_text'
                ]

                if all_config:
                    headers += ['config']

                writer.writerow(headers)

                for tr in transactions:
                    data = []
                    trdata = tr.dump_api(add_config=all_config)

                    for val in headers:
                        data.append(trdata[val])

                    writer.writerow(data)

                return response

    else:
        form = ExportForm(request.user)

    return render(request, 'export/home.html', {'form': form})
 def toXML(self):
     return structured.dict2xml(self.toDict(),
                                roottag='analytics',
                                pretty=True)
 def toXML(self):
     return structured.dict2xml(self.toDict(),roottag='resources', 
                                listnames={'locations': 'location','resources':'resource',
                                           'study_areas': 'studyarea', 'events':'event'}, pretty=True)
示例#17
0
def home(request):
    """Show the form to export data"""

    if request.method == 'POST':  # If the form has been submitted...
        form = ExportForm(request.user, request.POST)

        if form.is_valid():  # If the form is valid

            config = form.cleaned_data['config']
            all_config = form.cleaned_data['all_config']
            range = form.cleaned_data['range']
            file_type = form.cleaned_data['file_type']
            only_good = form.cleaned_data['only_good']

            if not request.user.is_superuser and all_config:
                raise Http404

            if not request.user.is_superuser and not request.user in config.allowed_users:
                raise Http404

            transactions = Transaction.objects.order_by('creation_date')

            file_name = u'Export_'

            if not all_config:
                transactions = transactions.filter(config=config)

                file_name += config.name + u'_'

            else:

                file_name += u'ALL_'

            if only_good:

                file_name += u'_Goods_'

                transactions = transactions.filter(Q(postfinance_status__startswith='5') | Q(postfinance_status__startswith='9'))

            if range == 'thismonth':
                start_date = now() + relativedelta(day=1, minute=0, hour=0, second=0, microsecond=0)
                end_date = now() + relativedelta(day=1, months=+1, seconds=-1, minute=0, hour=0, second=0, microsecond=0)
            elif range == 'previousmonth':
                start_date = now() + relativedelta(day=1, months=-1, minute=0, hour=0, second=0, microsecond=0)
                end_date = now() + relativedelta(day=1, seconds=-1, minute=0, hour=0, second=0, microsecond=0)
            elif range == 'sincemonth':
                start_date = now() + relativedelta(months=-1)
                end_date = now()
            elif range == 'thisyear':
                start_date = now() + relativedelta(day=1, month=1, minute=0, hour=0, second=0, microsecond=0)
                end_date = now() + relativedelta(day=1, month=1, years=+1, seconds=-1, minute=0, hour=0, second=0, microsecond=0)
            elif range == 'sinceyear':
                start_date = now() + relativedelta(years=-1)
                end_date = now()
            elif range == 'custom':
                start_date = form.cleaned_data['custom_start']
                end_date = form.cleaned_data['custom_end']

            transactions = transactions.filter(creation_date__gte=start_date, creation_date__lt=end_date).all()

            file_name += start_date.strftime('%Y-%m-%d_%H.%M.%S') + u' - ' + end_date.strftime('%Y-%m-%d_%H.%M.%S')

            # Commong for json and xml
            data = [dict(tr.dump_api(add_config=all_config).items() + {'logs': [log.dump_api() for log in tr.transactionlog_set.order_by('when').all()]}.items()) for tr in transactions]

            if file_type == 'json':

                response = HttpResponse(json.dumps(data), content_type='text/json')

                response['Content-Disposition'] = 'attachment; filename="%s.json"' % (file_name, )

                return response

            elif file_type == 'xml':

                response = HttpResponse(dict2xml({'export': data}, pretty=True), content_type='text/xml')

                response['Content-Disposition'] = 'attachment; filename="%s.xml"' % (file_name, )

                return response

            elif file_type == 'csv':

                response = HttpResponse(content_type='text/csv')
                response['Content-Disposition'] = 'attachment; filename="%s.csv"' % (file_name, )

                writer = csv.writer(response)

                headers = ['reference', 'extra_data', 'amount', 'amount_chf', 'postfinance_id', 'postfinance_status', 'internal_status', 'ipn_needed', 'creation_date', 'last_userforwarded_date', 'last_user_back_from_postfinance_date', 'last_postfinance_ipn_date', 'last_ipn_date', 'postfinance_status_text', 'internal_status_text']

                if all_config:
                    headers += ['config']

                writer.writerow(headers)

                for tr in transactions:
                    data = []
                    trdata = tr.dump_api(add_config=all_config)

                    for val in headers:
                        data.append(trdata[val])

                    writer.writerow(data)

                return response

    else:
        form = ExportForm(request.user)

    return render(request, 'export/home.html', {'form': form})
示例#18
0
 def toXML(self):
     return structured.dict2xml(self.toDict(),roottag='libraryhours',listnames={'times': 'time'},pretty=True)