def download_attachment(self, announcement, attachment): """ Route for download attachment """ if attachment.res_id == announcement.id: filecontent = base64.b64decode(attachment.datas) if not filecontent: response = request.not_found() else: filename = attachment.name response = request.make_response(filecontent, [('Content-Type', 'application/octet-stream'), ('Content-Disposition', content_disposition(filename))]) else: response = request.not_found() return response
def rss_xml_index(self): cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context ira = request.registry['ir.attachment'] iuv = request.registry['ir.ui.view'] product_obj = request.registry['product.template'] mimetype = 'application/xml;charset=utf-8' content = None product_rss = ira.search_read(cr, uid, [('name', '=', '/product_rss.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context) if product_rss: # Check if stored version is still valid server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT create_date = datetime.datetime.strptime( product_rss[0]['create_date'], server_format) delta = datetime.datetime.now() - create_date if delta < PRODUCT_RSS_CACHE_TIME: content = product_rss[0]['datas'].decode('base64') if not content: # Remove all RSS in ir.attachments as we're going to regenerate product_rss_ids = ira.search( cr, uid, [('name', '=like', '/product_rss%.xml'), ('type', '=', 'binary')], context=context) if product_rss_ids: ira.unlink(cr, uid, product_rss_ids, context=context) pages = 0 first_page = None values = {} product_ids = product_obj.search( cr, uid, [('website_published', '=', True)]) if product_ids: values['products'] = product_obj.browse( cr, uid, product_ids, context) values['url_root'] = request.httprequest.url_root urls = iuv.render(cr, uid, 'website_product_rss.product_rss_locs', values, context=context) if urls: page = iuv.render(cr, uid, 'website_product_rss.product_rss_xml', dict(content=urls), context=context) if not first_page: first_page = page pages += 1 self.create_product_rss('/product_rss-%d.xml' % pages, page) if not pages: return request.not_found() elif pages == 1: content = first_page return request.make_response(content, [('Content-Type', mimetype)])
def download_attachment(self, attachment_id): # Check if this is a valid attachment id attachment = request.env['ir.attachment'].sudo().search_read([ ('id', '=', int(attachment_id)) ], [ "name", "datas", "file_type", "res_model", "res_id", "type", "url" ]) if attachment: attachment = attachment[0] else: return redirect(self.orders_page) # Check if the user has bought the associated product res_model = attachment['res_model'] res_id = attachment['res_id'] purchased_products = request.env[ 'account.invoice.line'].get_digital_purchases(request.uid) if res_model == 'product.product': if res_id not in purchased_products: return redirect(self.orders_page) # Also check for attachments in the product templates elif res_model == 'product.template': P = request.env['product.product'] template_ids = map(lambda x: P.browse(x).product_tmpl_id.id, purchased_products) if res_id not in template_ids: return redirect(self.orders_page) else: return redirect(self.orders_page) # The client has bought the product, otherwise it would have been blocked by now if attachment["type"] == "url": if attachment["url"]: return redirect(attachment["url"]) else: return request.not_found() elif attachment["datas"]: data = StringIO(base64.standard_b64decode(attachment["datas"])) return http.send_file(data, filename=attachment['name'], as_attachment=True) else: return request.not_found()
def download_attachment(self, attachment_id): # Check if this is a valid attachment id attachment = request.env['ir.attachment'].sudo().search_read( [('id', '=', int(attachment_id))], ["name", "datas", "file_type", "res_model", "res_id", "type", "url"] ) if attachment: attachment = attachment[0] else: return redirect(self.orders_page) # Check if the user has bought the associated product res_model = attachment['res_model'] res_id = attachment['res_id'] purchased_products = request.env['account.invoice.line'].get_digital_purchases(request.uid) if res_model == 'product.product': if res_id not in purchased_products: return redirect(self.orders_page) # Also check for attachments in the product templates elif res_model == 'product.template': P = request.env['product.product'] template_ids = map(lambda x: P.browse(x).product_tmpl_id.id, purchased_products) if res_id not in template_ids: return redirect(self.orders_page) else: return redirect(self.orders_page) # The client has bought the product, otherwise it would have been blocked by now if attachment["type"] == "url": if attachment["url"]: return redirect(attachment["url"]) else: return request.not_found() elif attachment["datas"]: data = StringIO(base64.standard_b64decode(attachment["datas"])) return http.send_file(data, filename=attachment['name'], as_attachment=True) else: return request.not_found()
def rss_xml_index(self): cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context ira = request.registry['ir.attachment'] user_obj = request.registry['res.users'] user_brw = user_obj.browse(cr, uid, [uid], context=context) iuv = request.registry['ir.ui.view'] product_obj = request.registry['product.template'] mimetype = 'application/xml;charset=utf-8' content = None product_rss = ira.search_read(cr, uid, [ ('name', '=', '/product_rss.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context) if product_rss: # Check if stored version is still valid server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT create_date = datetime.datetime.strptime( product_rss[0]['create_date'], server_format) delta = datetime.datetime.now() - create_date if delta < PRODUCT_RSS_CACHE_TIME: content = product_rss[0]['datas'].decode('base64') if not content: # Remove all RSS in ir.attachments as we're going to regenerate product_rss_ids = ira.search(cr, uid, [ ('name', '=like', '/product_rss%.xml'), ('type', '=', 'binary')], context=context) if product_rss_ids: ira.unlink(cr, uid, product_rss_ids, context=context) pages = 0 first_page = None values = {} product_ids = product_obj.search(cr, uid, [ ('website_published', '=', True)]) if product_ids: values['products'] = product_obj.browse(cr, uid, product_ids, context) values['company'] = user_brw[0].company_id values['url_root'] = request.httprequest.url_root urls = iuv.render(cr, uid, 'website_product_rss.product_rss_locs', values, context=context) if urls: page = iuv.render(cr, uid, 'website_product_rss.product_rss_xml', dict(content=urls), context=context) if not first_page: first_page = page pages += 1 self.create_product_rss('/product_rss-%d.xml' % pages, page) if not pages: return request.not_found() elif pages == 1: content = first_page return request.make_response(content, [('Content-Type', mimetype)])
def edit_announcement(self, announcement): """ Route for process edit announcement request """ cr, uid, context, registry = request.cr, request.uid, request.context, request.registry user = registry.get('res.users').browse(cr, uid, uid, context=context) if user and announcement.partner_id.id == user.partner_id.id or uid == SUPERUSER_ID: response = http.request.website.render('website_project_weezer.edit_announcement', self._get_edit_announcement_dict(cr, uid, registry, announcement, context=context)) else: response = request.not_found() return response
def new_announcement(self): """ Route to process create new announcement request """ cr, uid, context, registry = request.cr, request.uid, request.context, request.registry user = registry.get('res.users').browse(cr, uid, uid, context=context) if user and user.partner_id: response = http.request.website.render('website_project_weezer.new_announcement', self._get_new_announcement_dict(cr, uid, registry, user.partner_id, context=context)) else: response = request.not_found() return response
def save_announcement(self, announcement, **post): """ Route to process save announcement request """ cr, uid, context, registry = request.cr, request.uid, request.context, request.registry user = registry.get('res.users').browse(cr, uid, uid, context=context) if user and announcement.partner_id == user.partner_id or uid == SUPERUSER_ID: self._prepare_save_announcemet_param(cr, uid, request, post) res = self._parse_and_save_announcement(cr, uid, registry, announcement, post, context=context) response = http.request.website.render(res['template_id'], res['response']) else: response = request.not_found() return response
def partners_detail(self, partner_id, **post): _, partner_id = unslug(partner_id) if partner_id: partner = request.registry['res.partner'].browse(request.cr, SUPERUSER_ID, partner_id, context=request.context) is_website_publisher = request.registry['res.users'].has_group(request.cr, request.uid, 'base.group_website_publisher') if partner.exists() and (partner.website_published or is_website_publisher): values = { 'main_object': partner, 'partner': partner, 'edit_page': False } return request.website.render("website_partner.partner_page", values) return request.not_found()
def picking_download(self, picking_id, **kwargs): picking_obj = request.registry["stock.picking"] cr, uid, context = request.cr, request.uid, request.context picking = picking_obj.browse(cr, uid, picking_id, context=context) label = picking.carrier_label if not label: return request.not_found() pdfdata = base64.b64decode(label) return request.make_response( pdfdata, [('Content-Type', 'application/pdf'), ('Content-Disposition', content_disposition("label.pdf"))])
def partners_detail(self, partner_id, **post): _, partner_id = unslug(partner_id) if partner_id: partner = request.registry['res.partner'].browse( request.cr, SUPERUSER_ID, partner_id, context=request.context) is_website_publisher = request.registry['res.users'].has_group( request.cr, request.uid, 'base.group_website_publisher') if partner.exists() and (partner.website_published or is_website_publisher): values = { 'main_object': partner, 'partner': partner, 'edit_page': False } return request.website.render("website_partner.partner_page", values) return request.not_found()
def esl_download(self, id): try: ESLWizard = request.registry['account.vat.esl'] esl = ESLWizard.browse(request.cr, request.uid, int(id)) if esl: filecontent = esl.esl_csv_data() period_name = esl.period_from.name filename = 'esl_%s.csv' % (period_name,) return request.make_response( filecontent, headers=[ ('Content-Type', 'text/csv'), ('Content-Disposition', content_disposition(filename)), ], ) else: return request.not_found() except Exception, e: error = { 'code': 200, 'message': 'Odoo Server Error', 'data': _serialize_exception(e), } return request.make_response(html_escape(simplejson.dumps(error)))
def rss_xml_index(self): cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context ira = request.registry['ir.attachment'] iuv = request.registry['ir.ui.view'] user_obj = request.registry['res.users'] blog_obj = request.registry['blog.blog'] config_obj = request.registry['ir.config_parameter'] blog_ids = blog_obj.search(cr, uid, [], context=context) user_brw = user_obj.browse(cr, uid, [uid], context=context) blog_post_obj = request.registry['blog.post'] mimetype = 'application/xml;charset=utf-8' content = None blog_rss = ira.search_read(cr, uid, [('name', '=', '/blog_rss.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context) if blog_rss: # Check if stored version is still valid server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT create_date = datetime.datetime.strptime( blog_rss[0]['create_date'], server_format) delta = datetime.datetime.now() - create_date if delta < BLOG_RSS_CACHE_TIME: content = blog_rss[0]['datas'].decode('base64') if not content: # Remove all RSS in ir.attachments as we're going to regenerate blog_rss_ids = ira.search(cr, uid, [('name', '=like', '/blog_rss%.xml'), ('type', '=', 'binary')], context=context) if blog_rss_ids: ira.unlink(cr, uid, blog_rss_ids, context=context) pages = 0 first_page = None values = {} post_ids = blog_post_obj.search(cr, uid, [('website_published', '=', True)]) if post_ids: values['posts'] = blog_post_obj.browse(cr, uid, post_ids, context) if blog_ids: blog = blog_obj.browse(cr, uid, blog_ids, context=context)[0] values['blog'] = blog values['company'] = user_brw[0].company_id values['website_url'] = config_obj.get_param( cr, uid, 'web.base.url') values['url_root'] = request.httprequest.url_root urls = iuv.render(cr, uid, 'website_blog_rss.blog_rss_locs', values, context=context) if urls: page = iuv.render(cr, uid, 'website_blog_rss.blog_rss_xml', dict(content=urls), context=context) if not first_page: first_page = page pages += 1 self.create_blog_rss('/blog_rss-%d.xml' % pages, page) if not pages: return request.not_found() elif pages == 1: content = first_page return request.make_response(content, [('Content-Type', mimetype)])
def download_start(self, download_id=None, **kwargs): cr, uid, context = request.cr, request.uid, request.context if isinstance(download_id, basestring): download_id = int(download_id) # check for download key perm_obj = request.registry["portal.download.perm"] user_obj = request.registry["res.users"] perm_ids = None download_key = kwargs.get("key") if download_key: perm_ids = perm_obj.search( cr, SUPERUSER_ID, [("download_key_intern", "=", download_key)]) if perm_ids: perm = perm_obj.browse(cr, SUPERUSER_ID, perm_ids[0]) uid = user_obj.search_id( cr, SUPERUSER_ID, [("partner_id", "=", perm.partner_id.id)]) if not uid: return request.not_found() try: download_obj = request.registry["portal.download"] download = download_obj.browse(cr, uid, download_id, context=context) if not download or not download.active: return request.not_found() except: return request.not_found() att_obj = request.registry["ir.attachment"] att_ids = att_obj.search(cr, uid, [("res_model", "=", "portal.download"), ("res_id", "=", download.id)], limit=1) if not att_ids: return request.not_found() att = att_obj.browse(cr, SUPERUSER_ID, att_ids[0], context=context) att_mimetype = mimetypes.guess_type( att.datas_fname or "")[0] or "application/octet-stream" att_disp = content_disposition(att.datas_fname) file_out = None if att.store_fname: full_path = att_obj._full_path(cr, uid, att.store_fname) try: file_out = open(full_path, 'rb') except IOError: _logger.exception("Unable to load file %s", full_path) return request.not_found() else: return request.not_found() if not perm_ids: user = user_obj.browse(cr, SUPERUSER_ID, uid, context=context) perm_ids = perm_obj.search( cr, SUPERUSER_ID, [("partner_id", "=", user.partner_id.id), ("download_id", "=", download_id)]) if perm_ids: for perm_id in perm_ids: download_count = perm_obj.read( cr, SUPERUSER_ID, perm_id, ["download_count"], context=context)["download_count"] perm_obj.write(cr, SUPERUSER_ID, perm_id, {"download_count": download_count + 1}, context=context) return request.make_response(file_out, [("Content-Type", att_mimetype), ("Content-Disposition", att_disp)])