def fix_orders_vendor_uids(ctx=None):
    """Add vendor_uids attribute to order records.
    """
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    need_rebuild = False
    for item in data.values():
        update = False
        try:
            item.attrs['vendor_uids']
            if not isinstance(item.attrs['vendor_uids'], list)\
                    or not item.attrs['vendor_uids']:
                update = True
        except KeyError:
            update = True
        if not update:
            continue
        order_data = OrderData(portal, order=item)
        vendor_uids = set()
        for booking in order_data.bookings:
            vendor_uids.add(booking.attrs['vendor_uid'])
        item.attrs['vendor_uids'] = list(vendor_uids)
        need_rebuild = True
        logging.info(
            u"Added vendor_uids to order {0}".format(item.attrs['uid'])
        )
    if need_rebuild:
        soup.rebuild()
        logging.info("Rebuilt orders catalog")
Beispiel #2
0
def fix_orders_vendor_uids(ctx=None):
    """Add vendor_uids attribute to order records.
    """
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    need_rebuild = False
    for item in data.values():
        update = False
        try:
            item.attrs['vendor_uids']
            if not isinstance(item.attrs['vendor_uids'], list)\
                    or not item.attrs['vendor_uids']:
                update = True
        except KeyError:
            update = True
        if not update:
            continue
        order_data = OrderData(portal, order=item)
        vendor_uids = set()
        for booking in order_data.bookings:
            vendor_uids.add(booking.attrs['vendor_uid'])
        item.attrs['vendor_uids'] = list(vendor_uids)
        need_rebuild = True
        logging.info("Added vendor_uids to order {0}".format(
            item.attrs['uid']))
    if need_rebuild:
        soup.rebuild()
        logging.info("Rebuilt orders catalog")
def fix_payment_attrs(ctx=None):
    portal = getSite()
    payments = Payments(portal)
    orders_soup = get_orders_soup(portal)
    data = orders_soup.storage.data
    for item in data.values():
        try:
            item.attrs['payment_method']
            item.attrs['payment_label']
            continue
        except KeyError:
            payment_method = item.attrs['payment_selection.payment']
            payment = payments.get(payment_method)
            if payment:
                payment_label = payment.label
            else:
                payment_label = _('unknown', default=u'Unknown')
            item.attrs['payment_method'] = payment_method
            logging.info(
                u"Added payment_method {0} to booking {1}".format(
                    payment_method, item.attrs['uid']
                )
            )
            item.attrs['payment_label'] = payment_label
            logging.info(
                u"Added payment_label {0} to booking {1}".format(
                    payment_label, item.attrs['uid']
                )
            )
Beispiel #4
0
def fix_bookings_state_salaried_tid(ctx=None):
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    need_rebuild = False
    for item in data.values():
        order_data = OrderData(portal, order=item)
        try:
            state = item.attrs['state']
            state_exists = True
        except KeyError:
            state = None
            state_exists = False
        try:
            salaried = item.attrs['salaried']
            salaried_exists = True
        except KeyError:
            salaried = None
            salaried_exists = False
        try:
            tid = item.attrs['tid']
            tid_exists = True
        except KeyError:
            tid = 'none'  # tid default in b.p.payment
            tid_exists = False
        for booking in order_data.bookings:
            # add too booking node
            try:
                booking.attrs['state']
            except KeyError:
                booking.attrs['state'] = state
                need_rebuild = True
                logging.info("Added state {0} to booking {1}".format(
                    state, item.attrs['uid']))
            try:
                booking.attrs['salaried']
            except KeyError:
                booking.attrs['salaried'] = salaried
                need_rebuild = True
                logging.info("Added salaried {0} to booking {1}".format(
                    salaried, item.attrs['uid']))
            try:
                booking.attrs['tid']
            except KeyError:
                booking.attrs['tid'] = tid
                need_rebuild = True
                logging.info("Added tid {0} to booking {1}".format(
                    tid, item.attrs['uid']))
        # now, delete from order node
        if state_exists:
            del item.attrs['state']
        if salaried_exists:
            del item.attrs['salaried']
        if tid_exists:
            del item.attrs['tid']
    if need_rebuild:
        bookings_soup = get_bookings_soup(portal)
        bookings_soup.rebuild()
        logging.info("Rebuilt bookings catalog")
Beispiel #5
0
def fix_order_state_and_salaried(ctx=None):
    """Re-add state and salaried on order, needed for sorting in orders table
    """
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    for order in data.values():
        order_data = OrderData(portal, uid=order.attrs['uid'])
        bookings = order_data.bookings
        order.attrs['state'] = calculate_order_state(bookings)
        order.attrs['salaried'] = calculate_order_salaried(bookings)
    soup.rebuild()
def fix_order_state_and_salaried(ctx=None):
    """Re-add state and salaried on order, needed for sorting in orders table
    """
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    for order in data.values():
        order_data = OrderData(portal, uid=order.attrs['uid'])
        bookings = order_data.bookings
        order.attrs['state'] = calculate_order_state(bookings)
        order.attrs['salaried'] = calculate_order_salaried(bookings)
    soup.rebuild()
def fix_shipping_attrs(ctx=None):
    portal = getSite()
    orders_soup = get_orders_soup(portal)
    data = orders_soup.storage.data
    for item in data.values():
        try:
            item.attrs['shipping_method']
        except KeyError:
            item.attrs['shipping_method'] = 'unknown'
            logging.info(
                u"Added shipping_method {0} to booking {1}".format(
                    'unknown', item.attrs['uid']
                )
            )
        try:
            item.attrs['shipping_label']
        except KeyError:
            item.attrs['shipping_label'] = _('unknown', default=u'Unknown')
            logging.info(
                u"Added shipping_label {0} to booking {1}".format(
                    'unknown', item.attrs['uid']
                )
            )
        try:
            item.attrs['shipping_description']
        except KeyError:
            item.attrs['shipping_description'] = \
                _('unknown', default=u'Unknown')
            logging.info(
                u"Added shipping_description {0} to booking {1}".format(
                    'unknown', item.attrs['uid']
                )
            )
        try:
            item.attrs['shipping_net']
        except KeyError:
            item.attrs['shipping_net'] = item.attrs['shipping']
            logging.info(
                u"Added shipping_net {0} to booking {1}".format(
                    item.attrs['shipping'], item.attrs['uid']
                )
            )
        try:
            item.attrs['shipping_vat']
        except KeyError:
            item.attrs['shipping_vat'] = Decimal(0)
            logging.info(
                u"Added shipping_vat {0} to booking {1}".format(
                    Decimal(0), item.attrs['uid']
                )
            )
Beispiel #8
0
def reset_records(ctx=None):
    ignore_key = lambda x: x.startswith('____')
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    for order in data.values():
        reset_odict(order.attrs.storage, ignore_key=ignore_key)
        logging.info("Reset attributes storage on order {0}".format(
            order.attrs['uid'], ))
    soup = get_bookings_soup(portal)
    data = soup.storage.data
    for booking in data.values():
        reset_odict(booking.attrs.storage, ignore_key=ignore_key)
        logging.info("Reset attributes storage on booking {0}".format(
            booking.attrs['uid']))
Beispiel #9
0
def fix_bookings_state_salaried_tid(ctx=None):
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    need_rebuild = False
    for item in data.values():
        order_data = OrderData(portal, order=item)
        state = item.attrs.get('state', None)
        salaried = item.attrs.get('salaried', None)
        tid = item.attrs.get('tid', 'none')  # tid default in b.p.payment
        for booking in order_data.bookings:
            # add too booking node
            if 'state' not in booking.attrs:
                booking.attrs['state'] = state
                need_rebuild = True
                logging.info(
                    "Added state {0} to booking {1}".format(
                        state, item.attrs['uid']
                    )
                )
            if 'salaried' not in booking.attrs:
                booking.attrs['salaried'] = salaried
                need_rebuild = True
                logging.info(
                    "Added salaried {0} to booking {1}".format(
                        salaried, item.attrs['uid']
                    )
                )
            if 'tid' not in booking.attrs:
                booking.attrs['tid'] = tid
                need_rebuild = True
                logging.info(
                    "Added tid {0} to booking {1}".format(
                        tid, item.attrs['uid']
                    )
                )
        # now, delete from order node
        if 'state' in item.attrs:
            del item.attrs['state']
        if 'salaried' in item.attrs:
            del item.attrs['salaried']
        if 'tid' in item.attrs:
            del item.attrs['tid']
    if need_rebuild:
        bookings_soup = get_bookings_soup(portal)
        bookings_soup.rebuild()
        logging.info("Rebuilt bookings catalog")
def fix_discount_attrs(ctx=None):
    portal = getSite()
    # discount attrs on order
    orders_soup = get_orders_soup(portal)
    need_rebuild = False
    data = orders_soup.storage.data
    for item in data.values():
        try:
            item.attrs['cart_discount_net']
        except KeyError:
            need_rebuild = True
            item.attrs['cart_discount_net'] = Decimal(0)
            logging.info(
                u"Added cart_discount_net to order {0}".format(
                    item.attrs['uid']
                )
            )
        try:
            item.attrs['cart_discount_vat']
        except KeyError:
            need_rebuild = True
            item.attrs['cart_discount_vat'] = Decimal(0)
            logging.info(
                u"Added cart_discount_vat to order {0}".format(
                    item.attrs['uid']
                )
            )
    if need_rebuild:
        orders_soup.rebuild()
        logging.info("Rebuilt orders catalog")
    # discount attrs on bookings
    bookings_soup = get_bookings_soup(portal)
    need_rebuild = False
    data = bookings_soup.storage.data
    for item in data.values():
        try:
            item.attrs['discount_net']
        except KeyError:
            need_rebuild = True
            item.attrs['discount_net'] = Decimal(0)
            logging.info(
                u"Added discount_net to booking {0}".format(item.attrs['uid'])
            )
    if need_rebuild:
        bookings_soup.rebuild()
        logging.info("Rebuilt bookings catalog")
def reset_records(ctx=None):
    ignore_key = lambda x: x.startswith('____')
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    for order in data.values():
        reset_odict(order.attrs.storage, ignore_key=ignore_key)
        logging.info(
            "Reset attributes storage on order {0}".format(order.attrs['uid'],)
        )
    soup = get_bookings_soup(portal)
    data = soup.storage.data
    for booking in data.values():
        reset_odict(booking.attrs.storage, ignore_key=ignore_key)
        logging.info(
            "Reset attributes storage on booking {0}".format(
                booking.attrs['uid']
            )
        )
Beispiel #12
0
 def __call__(self):
     req = self.request
     ordernumber = req.form.get('content_auth_form.ordernumber', None)
     email = req.form.get('content_auth_form.email', None)
     order = None
     errs = []
     if ordernumber and email:
         orders_soup = get_orders_soup(self.context)
         order = orders_soup.query(Eq('ordernumber', ordernumber))
         try:
             # generator should have only one item
             order = next(order)
             try:
                 assert(order.attrs['personal_data.email'] == email)
             except AssertionError:
                 # Don't raise Unauthorized, as this allows to draw
                 # conclusions on existing ordernumbers
                 order = None
         except StopIteration:
             # order by ordernumber not exists
             order = None
     if not email:
         err = _('anon_auth_err_email',
                 default=u'Please provide the email adress you used for '
                         u'submitting the order.')
         errs.append(err)
     if not ordernumber:
         err = _('anon_auth_err_ordernumber',
                 default=u'Please provide the ordernumber')
         errs.append(err)
     if email and ordernumber and not order:
         err = _('anon_auth_err_order',
                 default=u'No order could be found for the given '
                         u'credentials')
         errs.append(err)
     if not ordernumber and not email:
         # first call of this form
         errs = []
     for err in errs:
         IStatusMessage(self.request).addStatusMessage(err, 'error')
     self.uid = order.attrs['uid'] if order else None
     return self.view_template(self)
Beispiel #13
0
def fix_discount_attrs(ctx=None):
    portal = getSite()
    # discount attrs on order
    orders_soup = get_orders_soup(portal)
    need_rebuild = False
    data = orders_soup.storage.data
    for item in data.values():
        try:
            item.attrs['cart_discount_net']
        except KeyError:
            need_rebuild = True
            item.attrs['cart_discount_net'] = Decimal(0)
            logging.info("Added cart_discount_net to order {0}".format(
                item.attrs['uid']))
        try:
            item.attrs['cart_discount_vat']
        except KeyError:
            need_rebuild = True
            item.attrs['cart_discount_vat'] = Decimal(0)
            logging.info("Added cart_discount_vat to order {0}".format(
                item.attrs['uid']))
    if need_rebuild:
        orders_soup.rebuild()
        logging.info("Rebuilt orders catalog")
    # discount attrs on bookings
    bookings_soup = get_bookings_soup(portal)
    need_rebuild = False
    data = bookings_soup.storage.data
    for item in data.values():
        try:
            item.attrs['discount_net']
        except KeyError:
            need_rebuild = True
            item.attrs['discount_net'] = Decimal(0)
            logging.info("Added discount_net to booking {0}".format(
                item.attrs['uid']))
    if need_rebuild:
        bookings_soup.rebuild()
        logging.info("Rebuilt bookings catalog")
Beispiel #14
0
def fix_payment_attrs(ctx=None):
    portal = getSite()
    payments = Payments(portal)
    orders_soup = get_orders_soup(portal)
    data = orders_soup.storage.data
    for item in data.values():
        try:
            item.attrs['payment_method']
            item.attrs['payment_label']
            continue
        except KeyError:
            payment_method = item.attrs['payment_selection.payment']
            payment = payments.get(payment_method)
            if payment:
                payment_label = payment.label
            else:
                payment_label = _('unknown', default=u'Unknown')
            item.attrs['payment_method'] = payment_method
            logging.info("Added payment_method {0} to booking {1}".format(
                payment_method, item.attrs['uid']))
            item.attrs['payment_label'] = payment_label
            logging.info("Added payment_label {0} to booking {1}".format(
                payment_label, item.attrs['uid']))
Beispiel #15
0
def fix_shipping_attrs(ctx=None):
    portal = getSite()
    orders_soup = get_orders_soup(portal)
    data = orders_soup.storage.data
    for item in data.values():
        try:
            item.attrs['shipping_method']
        except KeyError:
            item.attrs['shipping_method'] = 'unknown'
            logging.info("Added shipping_method {0} to booking {1}".format(
                'unknown', item.attrs['uid']))
        try:
            item.attrs['shipping_label']
        except KeyError:
            item.attrs['shipping_label'] = _('unknown', default=u'Unknown')
            logging.info("Added shipping_label {0} to booking {1}".format(
                'unknown', item.attrs['uid']))
        try:
            item.attrs['shipping_description']
        except KeyError:
            item.attrs['shipping_description'] = \
                _('unknown', default=u'Unknown')
            logging.info(
                "Added shipping_description {0} to booking {1}".format(
                    'unknown', item.attrs['uid']))
        try:
            item.attrs['shipping_net']
        except KeyError:
            item.attrs['shipping_net'] = item.attrs['shipping']
            logging.info("Added shipping_net {0} to booking {1}".format(
                item.attrs['shipping'], item.attrs['uid']))
        try:
            item.attrs['shipping_vat']
        except KeyError:
            item.attrs['shipping_vat'] = Decimal(0)
            logging.info("Added shipping_vat {0} to booking {1}".format(
                Decimal(0), item.attrs['uid']))
Beispiel #16
0
 def __call__(self):
     req = self.request
     ordernumber = req.form.get('order_auth_form.ordernumber', None)
     email = req.form.get('order_auth_form.email', None)
     order = None
     errs = []
     if ordernumber and email:
         orders_soup = get_orders_soup(self.context)
         order = orders_soup.query(Eq('ordernumber', ordernumber))
         order = order.next()  # generator should have only one item
         try:
             assert (order.attrs['personal_data.email'] == email)
         except AssertionError:
             # Don't raise Unauthorized, as this allows to draw conclusions
             # on existing ordernumbers
             order = None
     if not email:
         err = _('anon_auth_err_email',
                 default=u'Please provide the email adress you used for '
                 u'submitting the order.')
         errs.append(err)
     if not ordernumber:
         err = _('anon_auth_err_ordernumber',
                 default=u'Please provide the ordernumber')
         errs.append(err)
     if email and ordernumber and not order:
         err = _('anon_auth_err_order',
                 default=u'No order could be found for the given '
                 u'credentials')
         errs.append(err)
     if not ordernumber and not email:
         # first call of this form
         errs = []
     for err in errs:
         IStatusMessage(self.request).addStatusMessage(err, 'error')
     self.uid = order.attrs['uid'] if order else None
     return self.order_auth_template(self)
Beispiel #17
0
 def csv(self, request):
     # get orders soup
     orders_soup = get_orders_soup(self.context)
     # get bookings soup
     bookings_soup = get_bookings_soup(self.context)
     # fetch user vendor uids
     vendor_uids = get_vendor_uids_for()
     # base query for time range
     query = Ge('created', self.from_date) & Le('created', self.to_date)
     # filter by given vendor uid or user vendor uids
     vendor_uid = self.vendor
     if vendor_uid:
         vendor_uid = uuid.UUID(vendor_uid)
         # raise if given vendor uid not in user vendor uids
         if not vendor_uid in vendor_uids:
             raise Unauthorized
         query = query & Any('vendor_uids', [vendor_uid])
     else:
         query = query & Any('vendor_uids', vendor_uids)
     # filter by customer if given
     customer = self.customer
     if customer:
         query = query & Eq('creator', customer)
     # prepare csv writer
     sio = StringIO()
     ex = csv.writer(sio, dialect='excel-colon')
     # exported column keys as first line
     ex.writerow(ORDER_EXPORT_ATTRS +
                 COMPUTED_ORDER_EXPORT_ATTRS.keys() +
                 BOOKING_EXPORT_ATTRS +
                 COMPUTED_BOOKING_EXPORT_ATTRS.keys())
     # query orders
     for order in orders_soup.query(query):
         # restrict order bookings for current vendor_uids
         order_data = OrderData(self.context,
                                order=order,
                                vendor_uids=vendor_uids)
         order_attrs = list()
         # order export attrs
         for attr_name in ORDER_EXPORT_ATTRS:
             val = self.export_val(order, attr_name)
             order_attrs.append(val)
         # computed order export attrs
         for attr_name in COMPUTED_ORDER_EXPORT_ATTRS:
             cb = COMPUTED_ORDER_EXPORT_ATTRS[attr_name]
             val = cb(self.context, order_data)
             order_attrs.append(val)
         for booking in order_data.bookings:
             booking_attrs = list()
             # booking export attrs
             for attr_name in BOOKING_EXPORT_ATTRS:
                 val = self.export_val(booking, attr_name)
                 booking_attrs.append(val)
             # computed booking export attrs
             for attr_name in COMPUTED_BOOKING_EXPORT_ATTRS:
                 cb = COMPUTED_BOOKING_EXPORT_ATTRS[attr_name]
                 val = cb(self.context, booking)
                 booking_attrs.append(val)
             ex.writerow(order_attrs + booking_attrs)
             booking.attrs['exported'] = True
             bookings_soup.reindex(booking)
     # create and return response
     s_start = self.from_date.strftime('%G-%m-%d_%H-%M-%S')
     s_end = self.to_date.strftime('%G-%m-%d_%H-%M-%S')
     filename = 'orders-export-%s-%s.csv' % (s_start, s_end)
     self.request.response.setHeader('Content-Type', 'text/csv')
     self.request.response.setHeader('Content-Disposition',
                                     'attachment; filename=%s' % filename)
     return sio.getvalue().decode('utf8')
def fix_bookings_state_salaried_tid(ctx=None):
    portal = getSite()
    soup = get_orders_soup(portal)
    data = soup.storage.data
    need_rebuild = False
    for item in data.values():
        order_data = OrderData(portal, order=item)
        try:
            state = item.attrs['state']
            state_exists = True
        except KeyError:
            state = None
            state_exists = False
        try:
            salaried = item.attrs['salaried']
            salaried_exists = True
        except KeyError:
            salaried = None
            salaried_exists = False
        try:
            tid = item.attrs['tid']
            tid_exists = True
        except KeyError:
            tid = 'none'  # tid default in b.p.payment
            tid_exists = False
        for booking in order_data.bookings:
            # add too booking node
            try:
                booking.attrs['state']
            except KeyError:
                booking.attrs['state'] = state
                need_rebuild = True
                logging.info(
                    u"Added state {0} to booking {1}".format(
                        state, item.attrs['uid']
                    )
                )
            try:
                booking.attrs['salaried']
            except KeyError:
                booking.attrs['salaried'] = salaried
                need_rebuild = True
                logging.info(
                    u"Added salaried {0} to booking {1}".format(
                        salaried, item.attrs['uid']
                    )
                )
            try:
                booking.attrs['tid']
            except KeyError:
                booking.attrs['tid'] = tid
                need_rebuild = True
                logging.info(
                    u"Added tid {0} to booking {1}".format(
                        tid, item.attrs['uid']
                    )
                )
        # now, delete from order node
        if state_exists:
            del item.attrs['state']
        if salaried_exists:
            del item.attrs['salaried']
        if tid_exists:
            del item.attrs['tid']
    if need_rebuild:
        bookings_soup = get_bookings_soup(portal)
        bookings_soup.rebuild()
        logging.info("Rebuilt bookings catalog")
    def verify(self):
        #
        # Get Payment details
        #
        # Get order
        order = None
        tickets = is_context_ticket(self.context)
        skip_payment = False

        order_data = {
            "order_id": "",
            "total": "",
            "shipping": "",
            "currency": "",
            "tax": "",
            "ticket": tickets,
            "download_link": None,
            "verified": False
        }

        data = self.request.form
        ordernumber = data.get('orderID', '')
        if ordernumber:
            order_uid = IPaymentData(self.context).uid_for(ordernumber)
            if get_status_category(int(data['STATUS'])) != SUCCESS_STATUS:
                return order_data
        else:
            order_uid = data.get('order_uid', '')
            if order_uid:
                try:
                    order = OrderData(self.context, uid=order_uid)
                except:
                    order = None
                if order:
                    if order.total > 0:
                        return order_data
                    else:
                        skip_payment = True
                else:
                    return order_data
            else:
                return order_data
        
        #
        # SHA passphrase verification
        #
        signer = OmnikassaSignature(data, 'sha512', SHA_OUT_PASSWORD)
        payment = Payments(self.context).get('omnikassa_payment')
        
        if not order:
            try:
                order = OrderData(self.context, uid=order_uid)
            except:
                order = None

        # Check if order exists   
        if order_uid != None and order != None:
            order = OrderData(self.context, uid=order_uid)
            order_nr = order.order.attrs['ordernumber']

            # Build order data
            order_data = {  
                "ordernumber": str(order_nr),
                "order_id": str(order_uid),
                "total": str(order.total),
                "shipping": str(order.shipping),
                "currency": str(order.currency),
                "tax": str(order.vat),
                "ticket": tickets,
                "download_link": None,
                "verified": False,
                "already_sent":False,
                "bookings":json.dumps([])
            }

            order_bookings = []
           
            for booking in order.bookings:
                try:
                    booking_uid = booking.attrs['buyable_uid']
                    item_number = booking.attrs['item_number']

                    if item_number:
                        sku = str(item_number)
                    else:
                        sku = str(booking_uid)

                    item_category = "Product" # Default category
                    if tickets:
                        item_category = "E-Ticket"

                    order_bookings.append({
                        'id':sku,
                        'price': str(float(booking.attrs['net'])),
                        'name': str(booking.attrs['title']),
                        'category': item_category,
                        'quantity': int(booking.attrs['buyable_count']),
                    })
                except:
                    pass

            try:
                order_data['bookings'] = json.dumps(order_bookings)
            except:
                # Invalid JSON format
                order_data['bookings'] = json.dumps([])

            if tickets:
                base_url = self.context.portal_url()
                params = "?order_id=%s" %(str(order_uid))
                download_as_pdf_link = "%s/download_as_pdf?page_url=%s/tickets/etickets%s" %(base_url, base_url, params)
                order_data['download_link'] = download_as_pdf_link

        else:
            # Order doesn't exist in the database
            # return blank ticket
            order_data = {
                "order_id": "",
                "total": "",
                "shipping": "",
                "currency": "",
                "tax": "",
                "ticket": tickets,
                "download_link": None,
                "verified": False
            }
            return order_data

        shasign = data.get('SHASIGN', '')
        if shasign == signer.signature() or skip_payment:
            order_data['verified'] = True
            order = OrderData(self.context, uid=order_uid)
            order.salaried = ifaces.SALARIED_YES
            if order.order.attrs['email_sent'] != 'yes':
                order.order.attrs['email_sent'] = 'yes'
                orders_soup = get_orders_soup(self.context)
                order_record = order.order
                orders_soup.reindex(records=[order_record])
                transaction.get().commit()
                if not skip_payment:
                    payment.succeed(self.context, order_uid)
                return order_data
            else:
                return order_data
        else:
            payment.failed(self.context, order_uid)
            return order_data
Beispiel #20
0
 def csv(self, request):
     # get orders soup
     orders_soup = get_orders_soup(self.context)
     # get bookings soup
     bookings_soup = get_bookings_soup(self.context)
     # fetch user vendor uids
     vendor_uids = get_vendor_uids_for()
     # base query for time range
     query = Ge('created', self.from_date) & Le('created', self.to_date)
     # filter by given vendor uid or user vendor uids
     vendor_uid = self.vendor
     if vendor_uid:
         vendor_uid = uuid.UUID(vendor_uid)
         # raise if given vendor uid not in user vendor uids
         if vendor_uid not in vendor_uids:
             raise Unauthorized
         query = query & Any('vendor_uids', [vendor_uid])
     else:
         query = query & Any('vendor_uids', vendor_uids)
     # filter by customer if given
     customer = self.customer
     if customer:
         query = query & Eq('creator', customer)
     # prepare csv writer
     sio = StringIO()
     ex = csv.writer(sio, dialect='excel-colon', quoting=csv.QUOTE_MINIMAL)
     # exported column keys as first line
     ex.writerow(ORDER_EXPORT_ATTRS + COMPUTED_ORDER_EXPORT_ATTRS.keys() +
                 BOOKING_EXPORT_ATTRS +
                 COMPUTED_BOOKING_EXPORT_ATTRS.keys())
     # query orders
     for order in orders_soup.query(query):
         # restrict order bookings for current vendor_uids
         order_data = OrderData(self.context,
                                order=order,
                                vendor_uids=vendor_uids)
         order_attrs = list()
         # order export attrs
         for attr_name in ORDER_EXPORT_ATTRS:
             val = self.export_val(order, attr_name)
             order_attrs.append(val)
         # computed order export attrs
         for attr_name in COMPUTED_ORDER_EXPORT_ATTRS:
             cb = COMPUTED_ORDER_EXPORT_ATTRS[attr_name]
             val = cb(self.context, order_data)
             val = cleanup_for_csv(val)
             order_attrs.append(val)
         for booking in order_data.bookings:
             booking_attrs = list()
             # booking export attrs
             for attr_name in BOOKING_EXPORT_ATTRS:
                 val = self.export_val(booking, attr_name)
                 booking_attrs.append(val)
             # computed booking export attrs
             for attr_name in COMPUTED_BOOKING_EXPORT_ATTRS:
                 cb = COMPUTED_BOOKING_EXPORT_ATTRS[attr_name]
                 val = cb(self.context, booking)
                 val = cleanup_for_csv(val)
                 booking_attrs.append(val)
             ex.writerow(order_attrs + booking_attrs)
             booking.attrs['exported'] = True
             bookings_soup.reindex(booking)
     # create and return response
     s_start = self.from_date.strftime('%G-%m-%d_%H-%M-%S')
     s_end = self.to_date.strftime('%G-%m-%d_%H-%M-%S')
     filename = 'orders-export-%s-%s.csv' % (s_start, s_end)
     self.request.response.setHeader('Content-Type', 'text/csv')
     self.request.response.setHeader('Content-Disposition',
                                     'attachment; filename=%s' % filename)
     ret = sio.getvalue()
     sio.close()
     return ret