def get_rss_news(self, context): url = getattr(context.site_root, 'backoffice_rss_news_uri', None) if url is None: return [] # Flux de news RSS try: f = urlopen(url) except Exception: return [] try: feed = RSSFile(string=f.read()) except (XMLError, IndexError): return [] rss_news = [] for item in feed.items[:5]: item['ago'] = datetime_to_ago(item['pubDate']) if item.get('pubDate'): item['pubDate'] = format_date(item['pubDate'], context.accept_language) else: item['pubDate'] = None item['description'] = XMLParser( item['description'].encode('utf-8')) rss_news.append(item) return rss_news
def generate_pdf_resupply(self, resource, context, form): from datetime import datetime from itools.i18n import format_date from shop.utils import format_for_pdf from itools.pdf import stl_pmltopdf accept = context.accept_language creation_date = datetime.now() creation_date = format_date(creation_date, accept=accept) document = resource.get_resource('/ui/modules/stock/pdf_resupply.xml') # Build namespace namespace = {'creation_date': creation_date, 'lines': []} # Supplier #supplier = form['supplier'] #supplier = resource.get_resource(supplier) namespace['supplier'] = { 'title': '', #supplier.get_title(), 'address': '' } #format_for_pdf(supplier.get_property('address'))} # Get references references_number = context.get_form_value('references_number', type=Integer) or 0 for i in range(1, references_number + 1): kw = { 'id': i, 'reference': form['reference_%s' % i], 'title': form['title_%s' % i], 'quantity_to_order': form['new_stock_%s' % i], 'declinations': [] } nb_declinations = form['nb_declinations_%s' % i] or 0 for j in range(1, nb_declinations + 1): suffix = '_%s_%s' % (i, j) name_declination = form['name' + suffix] stock_declination = form['new_stock' + suffix] or 0 title_declination = form['title' + suffix] kw['declinations'].append({ 'reference': name_declination, 'title': title_declination, 'quantity_to_order': stock_declination }) namespace['lines'].append(kw) # Generate pdf pdf = stl_pmltopdf(document, namespace=namespace) #metadata = {'title': {'en': u'Bill'}} #if resource.get_resource('bill.pdf', soft=True): # resource.del_resource('bill.pdf') #PDF.make_resource(PDF, resource, 'bill.pdf', body=pdf, **metadata) return pdf
def generate_pdf_resupply(self, resource, context, form): from datetime import datetime from itools.i18n import format_date from shop.utils import format_for_pdf from itools.pdf import stl_pmltopdf accept = context.accept_language creation_date = datetime.now() creation_date = format_date(creation_date, accept=accept) document = resource.get_resource('/ui/modules/stock/pdf_resupply.xml') # Build namespace namespace = {'creation_date': creation_date, 'lines': []} # Supplier #supplier = form['supplier'] #supplier = resource.get_resource(supplier) namespace['supplier'] = { 'title': '',#supplier.get_title(), 'address': ''}#format_for_pdf(supplier.get_property('address'))} # Get references references_number = context.get_form_value('references_number', type=Integer) or 0 for i in range(1, references_number+1): kw = {'id': i, 'reference': form['reference_%s' % i], 'title': form['title_%s' % i], 'quantity_to_order': form['new_stock_%s' % i], 'declinations': []} nb_declinations = form['nb_declinations_%s' % i] or 0 for j in range(1, nb_declinations + 1): suffix = '_%s_%s' % (i, j) name_declination = form['name'+ suffix] stock_declination = form['new_stock'+ suffix] or 0 title_declination = form['title'+ suffix] kw['declinations'].append({'reference': name_declination, 'title': title_declination, 'quantity_to_order': stock_declination}) namespace['lines'].append(kw) # Generate pdf pdf = stl_pmltopdf(document, namespace=namespace) #metadata = {'title': {'en': u'Bill'}} #if resource.get_resource('bill.pdf', soft=True): # resource.del_resource('bill.pdf') #PDF.make_resource(PDF, resource, 'bill.pdf', body=pdf, **metadata) return pdf
def get_rss_news(self, context): url = getattr(context.site_root, 'backoffice_rss_news_uri', None) if url is None: return [] # Flux de news RSS try: f = urlopen(url) except Exception: return [] try: feed = RSSFile(string=f.read()) except (XMLError, IndexError): return [] rss_news = [] for item in feed.items[:5]: item['ago'] = datetime_to_ago(item['pubDate']) if item.get('pubDate'): item['pubDate'] = format_date(item['pubDate'], context.accept_language) else: item['pubDate'] = None item['description'] = XMLParser(item['description'].encode('utf-8')) rss_news.append(item) return rss_news
def format_date(self, date): return format_date(date, accept=self.accept_language)
def get_namespace(self, context): # Get some resources shop = get_shop(self) order_products = self.get_resource('products') # Get creation date accept = context.accept_language creation_date = self.get_property('creation_datetime') creation_date = format_date(creation_date, accept=accept) # Build namespace namespace = { 'products': [], 'reference': self.get_reference(), 'creation_date': creation_date, 'price': { 'shippings': { 'with_tax': decimal(0), 'without_tax': decimal(0) }, 'products': { 'with_tax': decimal(0), 'without_tax': decimal(0) }, 'total': { 'with_tax': decimal(0), 'without_tax': decimal(0) } } } # Build order products namespace get_value = order_products.handler.get_record_value for record in order_products.handler.get_records(): kw = {'id': record.id, 'href': None, 'category': None} for key in BaseOrdersProducts.record_properties.keys(): kw[key] = get_value(record, key) name = get_value(record, 'name') product_resource = context.root.get_resource(name, soft=True) if product_resource: kw['href'] = context.get_link(product_resource) kw['key'] = product_resource.handler.key kw['cover'] = product_resource.get_cover_namespace(context) kw['category'] = product_resource.parent.get_title() # Declination if kw['declination']: declination = product_resource.get_resource(str( kw['declination']), soft=True) if declination: kw['declination'] = declination.get_declination_title() else: kw['cover'] = None # Get product prices unit_price_with_tax = kw['pre-tax-price'] * ((kw['tax'] / 100) + 1) unit_price_without_tax = kw['pre-tax-price'] total_price_with_tax = unit_price_with_tax * kw['quantity'] total_price_without_tax = unit_price_without_tax * kw['quantity'] kw['price'] = { 'unit': { 'with_tax': format_price(unit_price_with_tax), 'without_tax': format_price(unit_price_without_tax) }, 'total': { 'with_tax': format_price(total_price_with_tax), 'without_tax': format_price(total_price_without_tax) } } namespace['products'].append(kw) # Calcul order price namespace['price']['products']['with_tax'] += total_price_with_tax namespace['price']['products'][ 'without_tax'] += total_price_without_tax # Format price shipping_price = self.get_property('shipping_price') namespace['price']['total']['with_tax'] = format_price( namespace['price']['products']['with_tax'] + shipping_price) namespace['price']['products']['with_tax'] = format_price( namespace['price']['products']['with_tax']) namespace['price']['products']['without_tax'] = format_price( namespace['price']['products']['without_tax']) namespace['price']['shippings']['with_tax'] = format_price( shipping_price) # Customer customer_id = self.get_property('customer_id') user = context.root.get_user(customer_id) namespace['customer'] = { 'id': customer_id, 'title': user.get_title(), 'email': user.get_property('email'), 'phone1': user.get_property('phone1'), 'phone2': user.get_property('phone2') } # Addresses addresses = shop.get_resource('addresses').handler get_address = addresses.get_record_namespace bill_address = self.get_property('bill_address') delivery_address = self.get_property('delivery_address') namespace['delivery_address'] = get_address(delivery_address) namespace['bill_address'] = get_address(bill_address) # Carrier namespace['carrier'] = u'xxx' namespace['payment_way'] = u'xxx' return namespace
def get_namespace(self, context): # Get some resources shop = get_shop(self) order_products = self.get_resource('products') # Get creation date accept = context.accept_language creation_date = self.get_property('creation_datetime') creation_date = format_date(creation_date, accept=accept) # Build namespace namespace = {'products': [], 'reference': self.get_reference(), 'creation_date': creation_date, 'price': {'shippings': {'with_tax': decimal(0), 'without_tax': decimal(0)}, 'products': {'with_tax': decimal(0), 'without_tax': decimal(0)}, 'total': {'with_tax': decimal(0), 'without_tax': decimal(0)}}} # Build order products namespace get_value = order_products.handler.get_record_value for record in order_products.handler.get_records(): kw = {'id': record.id, 'href': None, 'category': None} for key in BaseOrdersProducts.record_properties.keys(): kw[key] = get_value(record, key) name = get_value(record, 'name') product_resource = context.root.get_resource(name, soft=True) if product_resource: kw['href'] = context.get_link(product_resource) kw['key'] = product_resource.handler.key kw['cover'] = product_resource.get_cover_namespace(context) kw['category'] = product_resource.parent.get_title() # Declination if kw['declination']: declination = product_resource.get_resource( str(kw['declination']), soft=True) if declination: kw['declination'] = declination.get_declination_title() else: kw['cover'] = None # Get product prices unit_price_with_tax = kw['pre-tax-price'] * ((kw['tax']/100)+1) unit_price_without_tax = kw['pre-tax-price'] total_price_with_tax = unit_price_with_tax * kw['quantity'] total_price_without_tax = unit_price_without_tax * kw['quantity'] kw['price'] = { 'unit': {'with_tax': format_price(unit_price_with_tax), 'without_tax': format_price(unit_price_without_tax)}, 'total': {'with_tax': format_price(total_price_with_tax), 'without_tax': format_price(total_price_without_tax)}} namespace['products'].append(kw) # Calcul order price namespace['price']['products']['with_tax'] += total_price_with_tax namespace['price']['products']['without_tax'] += total_price_without_tax # Format price shipping_price = self.get_property('shipping_price') namespace['price']['total']['with_tax'] = format_price( namespace['price']['products']['with_tax'] + shipping_price) namespace['price']['products']['with_tax'] = format_price( namespace['price']['products']['with_tax']) namespace['price']['products']['without_tax'] = format_price( namespace['price']['products']['without_tax']) namespace['price']['shippings']['with_tax'] = format_price( shipping_price) # Customer customer_id = self.get_property('customer_id') user = context.root.get_user(customer_id) namespace['customer'] = {'id': customer_id, 'title': user.get_title(), 'email': user.get_property('email'), 'phone1': user.get_property('phone1'), 'phone2': user.get_property('phone2')} # Addresses addresses = shop.get_resource('addresses').handler get_address = addresses.get_record_namespace bill_address = self.get_property('bill_address') delivery_address = self.get_property('delivery_address') namespace['delivery_address'] = get_address(delivery_address) namespace['bill_address'] = get_address(bill_address) # Carrier namespace['carrier'] = u'xxx' namespace['payment_way'] = u'xxx' return namespace
def get_namespace(self, order, shipping_way, record, context): history = shipping_way.get_resource('history').handler get_value = history.get_record_value ts = get_value(record, 'ts') return {'num_colissimo': get_value(record, 'num_colissimo'), 'date': format_date(ts, context.accept_language)}