how to print looped data from a method - odoo

i created a method as below: but it only takes the last loop data not all the data to xml for generating pdf,so need a way to generate pdf on a loop from this method
def bsku_asin_picking(self):
website = self.env['website'].search([('company_id', '=', self.env.company.id)], limit=1)
# print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%', website.label_type)
# vals = []
for rec in self.move_ids_without_package:
product = self.env['product.product'].browse(rec.product_id.id)
data = {
'quantity': rec.product_uom_qty,
'display_name': product.display_name,
'product': product,
}
if website.label_type == 'bsku':
data['barcode_value'] = product.bsku
else:
data['barcode_value'] = product.asin
# vals.append(data)
return self.env.ref('product_label_bsku_sin.report_product_template_label').report_action(product, data=data)

The variables passed through data will be available in the rendering context.
Example:
def bsku_asin_picking(self):
website = self.env['website'].search([('company_id', '=', self.env.company.id)], limit=1)
vals = []
for rec in self.move_ids_without_package:
product = rec.product_id
product_data = {
'quantity': rec.product_uom_qty,
'display_name': product.display_name,
'product': product,
}
if website.label_type == 'bsku':
product_data['barcode_value'] = product.bsku
else:
product_data['barcode_value'] = product.asin
vals.append(product_data)
datas = {'vals': vals}
return self.env.ref('product_label_bsku_sin.action_report_product_template_label').report_action(self, data=datas)
Then you can loop over vals and print product data.
Example:
<t t-foreach="vals" t-as="product_data">
<p t-out="product_data['quantity']"/>
<p t-out="product_data['display_name']"/>
<p t-out="product_data['product']"/>
<p t-out="product_data['barcode_value']"/>
</t>
Alternative
You can also call report_action without data and loop over the move_ids_without_package:
Example:
<t t-set="website_label_type" t-value="doc.env['website'].search([('company_id', '=', doc.env.company.id)], limit=1)"/>
<t t-foreach="doc.move_ids_without_package" t-as="line">
<p t-out="line.product_uom_qty"/>
<p t-out="line.display_name"/>
<p t-out="line.product_id.name"/>
<t t-if="website_label_type == 'bsku'">
<span t-out="line.product_id.bsku"/>
</t>
<t t-else="">
<span t-out="line.product_id.asin"/>
</t>
</t>

Related

how to implement a section block in eLearning module - odoo 14

i was tryin to add a section block which allows me to block all the possible sections (except for the first) and then if all the slides in the section are complete, makes the next one available
I tried with the templates but it don't seem the way to approach this problem cause I cannot find any way to check the previous slides, I was trying to figure it out with javascript maybe but I'm not that good with JS
that's my actual progress:
<odoo>
<data>
<template id="section_lock" inherit_id='website_slides.course_slides_list_slide'>
<xpath expr="//div[#class='text-truncate mr-auto']" position="replace">
<div class="text-truncate mr-auto">
<a t-if="slide.is_preview or slide.previous_category_complete is False or channel.can_publish"
class="o_wslides_js_slides_list_slide_link" t-attf-href="/slides/slide/#{slug(slide)}">
<span t-field="slide.name"/>
</a>
<span t-else="">
<span t-esc="slide.name"/>
</span>
</div>
</xpath>
</template>
</data>
and then the model with the flag needed for the check
class Slide(models.Model):
_inherit = 'slide.slide'
previous_category_complete = fields.Boolean(default=True)
how do u suggest to do it? I think i'm way off th road with this
The relationship between an e-Course and its section is based on the same model (slide.slide) using these relational fields :
category_id = fields.Many2one('slide.slide', string="Section", compute="_compute_category_id", store=True)
slide_ids = fields.One2many('slide.slide', "category_id", string="Slides")
#api.depends('channel_id.slide_ids.is_category', 'channel_id.slide_ids.sequence')
def _compute_category_id(self):
self.category_id = False # initialize whatever the state
channel_slides = {}
for slide in self:
if slide.channel_id.id not in channel_slides:
channel_slides[slide.channel_id.id] = slide.channel_id.slide_ids
for cid, slides in channel_slides.items():
current_category = self.env['slide.slide']
slide_list = list(slides)
slide_list.sort(key=lambda s: (s.sequence, not s.is_category))
for slide in slide_list:
if slide.is_category:
current_category = slide
elif slide.category_id != current_category:
slide.category_id = current_category.id
To know wether a specific student has attended an e-course, there is an existing field in the model 'slide.slide.partner':
class SlidePartnerRelation(models.Model):
_name = 'slide.slide.partner'
slide_id = fields.Many2one('slide.slide', ondelete="cascade", index=True, required=True)
partner_id = fields.Many2one('res.partner', index=True, required=True, ondelete='cascade')
completed = fields.Boolean('Completed')
So you can check wether all the previous Slide Sections are completed for the current partner_id, inheriting the class WebsiteSlides located in website_slides/controllers/main.py > def channel(...)... :
from odoo.addons.website_slides.controllers.main import WebsiteSlides
class WebsiteSlidesCustom(WebsiteSlides):
#http.route()
def channel(self, channel, category=None, tag=None, page=1, slide_type=None, uncategorized=False, sorting=None, search=None, **kw):
res_super = super(WebsiteSlidesCustom, self).channel(channel, category=None, tag=None, page=1, slide_type=None, uncategorized=False, sorting=None, search=None, **kw)
values = res_super.qcontext
sections_completion = request.env['slide.slide.partner'].sudo().search([
('channel_id', '=', current_channel_id),
('is_category','=', True),
('partner_id', '=', request.env.user.partner_id.id)
])
dic_sections_completion={}
for sec in sections_completion.sorted('sequence'):
dic_sections_completion[sec.id] = sec.completed
values['sections_completion'] = dic_sections_completion
return request.render('website_slides.course_main', values)
in your module directory views, create a new xml file : course_main_custom.xml
<template id="course_main_custom" inherit_id="website_slides.course_main" name="Course Main" >
<xpath expr="//t[#t-set='category']" position="after">
...
</xpath>
</template>

Trying to scrape texts with the same divs and no other info

This html has 3 divs with the same name accounts-table__count but different types of information.
I'm trying to get the posts count and and follower count of this page. is there a way to take the texts using css selector?
site;https://mastodon.online/explore
<div class='directory__card__extra'>
<div class='accounts-table__count'>
629
<small>posts</small>
</div>
<div class='accounts-table__count'>
72
<small>followers</small>
</div>
<div class='accounts-table__count'>
<time class='time-ago' datetime='2021-05-18' title='May 18, 2021'>May 18, 2021</time>
<small>last active</small>
</div>
</div>
my code;
def parse(self, response):
for users in response.css('div.directory__card'):
yield {
'id': users.css('span::text').get().replace('#','').replace('.','-'),
'name': users.css('strong.p-name::text').get(),
'posts': '' // this is the post count //
'followers': '' // this is the follower count //
'description': users.css('p::text').get(),
'fediverse': users.css('span::text').get(),
'link': users.css('a.directory__card__bar__name').attrib['href'],
'image': users.css('img.u-photo').attrib['src'],
'bg-image': users.css('img').attrib['src'],
}
for nextpage in response.css('span.next'):
next_page = nextpage.css('a').attrib['href']
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
As example, iterate over card, for each one get the values in shape of text and filter out the values.
raw_data = response.css(".directory__card")[0].css(".accounts-table__count::text").getall()
values = list(filter(lambda s: s != "", map(lambda s: s.strip(), raw_data)))
Some values from css selector of .accounts-table__count::text are empty, because div elements with this class has no text, but other html elements in it.

Beautiful Soup - How to find tags after a specific item in HTML?

I need to find tags after a specific item on a website. So, is there a way to skip the tag objects until this specific one, then find the matching ones to given criteria? I need all p with class XYZ after the div with class ABC.
response = requests.get(url).text
soup = BeautifulSoup(response)
items = soup.find_all('p', {'class': 'MessageTextSize js-message-text message-text'}) # only return the ones after the div with class of "Text 2"
Edit: You can see a sample code block below which is part response. The aim is finding the last two paragraphs (Text 3 & Text 4) despite the first one (Text 1) also has the same p class with them. So, I need to look for the parameter of find_all function after the Text 2 (class MessageTextSize js-message-text message-text).
<div class="js-message-text-container">
<p class="MessageTextSize js-message-text message-text" data-aria-label-part="0">Text 1</p>
</div>
<div class="js-message-text-container">
<p class="MessageTextSize MessageTextSize--jumbo js-message-text message-text" data-aria-label-part="0">Text 2</p>
</div>
<div class="js-message-text-container">
<p class="MessageTextSize js-message-text message-text" data-aria-label-part="0">Text 3</p>
</div>
<div class="js-message-text-container">
<p class="MessageTextSize js-message-text message-text" data-aria-label-part="0">Text 4</p>
</div>
p.s. bs4 version is 4.8.1, which is the latest release.
You can always use a custom function (or a lambda expression) inside find_all. The following is self-explanatory (IMO).
result = soup.find_all(
lambda x: x.name == 'p' and
'XYZ' in x.get('class', '') and
x.find_previous('div', class_='ABC')
)
Example
from bs4 import BeautifulSoup
html = """
<p class="XYZ">Text 1</p>
<p class="XYZ">Text 2</p>
<div class="ABC"></div>
<p class="XYZ">Text 3</p>
<p class="XYZ">Text 4</p>
"""
soup = BeautifulSoup(html, 'html.parser')
result = soup.find_all(
lambda x: x.name == 'p' and
'XYZ' in x.get('class', '') and
x.find_previous('div', class_='ABC')
)
print(result)
Output
[<p class="XYZ">Text 3</p>, <p class="XYZ">Text 4</p>]
EDIT
MessageTextSize js-message-text message-text represents three classes, not one.
x.get('class', '') returns a list of classes -
['MessageTextSize', 'js-message-text', 'message-text']
In your particular case, you have to target a p tag not a div, if I understood correctly.
So, you have to use
result = soup.find_all(
lambda x: x.name == 'p' and
'MessageTextSize js-message-text message-text' in ' '.join(x.get('class', ''))
and x.find_previous('p', class_='MessageTextSize MessageTextSize--jumbo js-message-text message-text')
)
Ref:
find_previous()
Function as filter
If I understand you correctly, this should work:
item = soup.select_one('p[class*="MessageTextSize--jumbo"]')
sibs = item.parent.find_next_siblings()
for sib in sibs:
print(sib.text.strip())
Output:
Text 3
Text 4

Odoo11 Custom Report with Wizard

I would like to print some product barcodes with the custom configuration.
Now, the problem is, I have created the wizard but when I click the print button there is no data is transferred to the report.
class BarcodeConfig(models.Model):
_name = 'report.barcode.print'
#api.model
def _default_product_line(self):
active_ids = self._context.get('active_ids', [])
products = self.env['product.product'].browse(active_ids)
return [(0, 0, {'product_id': x.id, 'qty': 1}) for x in products]
product_line = fields.One2many('product.print.wizard', 'barcode_id', string="Products",
default=_default_product_line)
#api.multi
def print_report(self):
data = self.read()[0]
ids = [x.product_id.id for x in self.product_line]
config = self.env['barcode.config'].get_values()
data.update({
'config': config
})
datas = {'ids': ids,
'form': data
}
return self.env.ref('odoo_barcode.barcode_report').report_action(self,data=datas)
In the above code, I had ids as well for product.product model.
<template id="report_barcode_temp">
<t t-call="web.html_container">
<t t-call="web.internal_layout">
<div class="page">
<h2>Report title</h2>
<strong t-esc="docs"/>
<strong t-esc="doc_ids"/>
<span t-esc="data"/>
<t t-foreach="docs" t-as="o">
<span t-esc="o"/>
<p>This object's name is
<span t-field="o.name"/>
</p>
</t>
</div>
</t>
</t>
In the above code, I'm just trying to print what the data's I've, but I've nothing in docs.
<strong t-esc="docs"/>
this line print product.product model as well. but ids not here, That's the problem I have.
Can anyone help me to tell why this happens? Or is there any other way to achieve this?
I have solved this by get_report_values method.
class classname(models.AbstractModel):
_name = 'report.modulename.templatename'
#api.model
def get_report_values(self, docids, data=None):
# docids: pass from the wizard print button.
records = self.env[objectname].browse(docids)
return {
'doc_ids': docids,
'doc_model': objectname,
'docs': records,
'data': data,}

Odoo : Make changes on Survey module

I am working on editing the survey module in odoo.
When I answer a survey which is composed of pages and then click on submit survey, it calculates the whole score of the survey and displays it. What I want is that it calculates the score of each page and display it when I click on submit survey for example:
It displays: Your score is: 200 points.
And I want it to display: Score of first page : 20 points, Second page: 50 points and so on...
Here is the code that I want to change but I don't know how should I change this function.
questionnaire.py (survey.py) : class survey_user_input
class survey_user_input(osv.Model):
''' Metadata for a set of one user's answers to a particular survey '''
_name = "questionnaire.user_input"
_rec_name = 'date_create'
_description = 'Survey User Input'
def _quizz_get_score(self, cr, uid, ids, name, args, context=None):
ret = dict()
for user_input in self.browse(cr, uid, ids, context=context):
ret[user_input.id] = sum([uil.quizz_mark for uil in user_input.user_input_line_ids] or [0.0])
return ret
_columns = {
'survey_id': fields.many2one('questionnaire.questionnaire', 'Questionnaire', required=True,
readonly=1, ondelete='restrict'),
'date_create': fields.datetime('Date de creation', required=True,
readonly=1, copy=False),
'deadline': fields.datetime("Date limite",
oldname="date_deadline"),
'type': fields.selection([('manually', 'Manuellement'), ('link', 'Lien')],
'Type de reponse', required=1, readonly=1,
oldname="response_type"),
'state': fields.selection([('new', 'Pas encore commence'),
('skip', 'Partiellement acheve'),
('done', 'Termine')],
'Statut',
readonly=True),
'test_entry': fields.boolean('Entree de test', readonly=1),
'token': fields.char("Piece d'identite", readonly=1, required=1, copy=False),
# Optional Identification data
'partner_id': fields.many2one('res.partner', 'Partenaire', readonly=1),
'email': fields.char("E-mail", readonly=1),
# Displaying data
'last_displayed_page_id': fields.many2one('questionnaire.page',
'Derniere page affichee'),
# The answers !
'user_input_line_ids': fields.one2many('questionnaire.user_input_line',
'user_input_id', 'Reponses', copy=True),
# URLs used to display the answers
'result_url': fields.related('survey_id', 'result_url', type='char',
string="Lien public aux resultats du sondage"),
'print_url': fields.related('survey_id', 'print_url', type='char',
string="Lien public au sondage vide"),
'quizz_score': fields.function(_quizz_get_score, type="float", string="Score pour le quiz", store=True)
}
_defaults = {
'date_create': fields.datetime.now,
'type': 'manually',
'state': 'new',
'token': lambda s, cr, uid, c: uuid.uuid4().__str__(),
'quizz_score': 0.0,
}
questionnaire.py (survey.py) : class survey_user_input_line
_name = 'questionnaire.user_input_line'
_description = 'Survey User Input Line'
_rec_name = 'date_create'
_columns = {
'user_input_id': fields.many2one('questionnaire.user_input', 'Entree de l\'utilisateur',
ondelete='cascade', required=1),
'question_id': fields.many2one('questionnaire.question', 'Question',
ondelete='restrict', required=1),
'page_id': fields.related('question_id', 'page_id', type='many2one',
relation='questionnaire.page', string="Page"),
'survey_id': fields.related('user_input_id', 'survey_id',
type="many2one", relation="questionnaire.questionnaire",
string='Questionnaire', store=True),
'date_create': fields.datetime('Date de creation', required=1),
'skipped': fields.boolean('Ignore'),
'answer_type': fields.selection([('text', 'Texte'),
('number', 'Nombre'),
('date', 'Date'),
('free_text', 'Texte Libre'),
('suggestion', 'Suggestion')],
'Type de reponse'),
'value_text': fields.char("Reponse texte"),
'value_number': fields.float("Reponse numerique"),
'value_date': fields.datetime("Reponse date"),
'value_free_text': fields.text("Reponse texte libre"),
'value_suggested': fields.many2one('questionnaire.label', "Reponse suggeree"),
'value_suggested_row': fields.many2one('questionnaire.label', "Reponse en ligne"),
'quizz_mark': fields.float("Score donne pour ce choix")
}
survey_templates.xml
<!-- "Thank you" message when the survey is completed -->
<template id="sfinished" name="Survey Finished">
<t t-call="website.layout">
<div class="wrap">
<div class="container">
<t t-call="questionnaire.back" />
<div class="jumbotron mt32">
<h1>Thank you!</h1>
<div t-field="questionnaire.thank_you_message" class="oe_no_empty" />
<div> You scored <t t-esc="user_input.quizz_score" /> points.</div>
<div>If you want you can <a t-att-href="'/questionnaire/print/%s/%s' % (slug(questionnaire), token)">review your answers</a>.</div>
</div>
</div>
</div>
</t>
</template>
One alternative way is to use pure qweb rather than function field; like this:
<!-- First finding which page is related to the survey. This is a bit odd but I couldn't find any other way!! -->
<t t-set="pages" t-value="dict((l.id, l.page_id.id) for l in user_input.user_input_line_ids).values()" />
<!-- Then print the score per each page -->
<t t-foreach='pages' t-as='p'>
<t t-set="page_score" t-value="sum([(uil.quizz_mark or 0.0) if uil.page_id.id == p else 0.0 for uil in user_input.user_input_line_ids])" />
<div> You scored <t t-esc="page_score" /> points in page <t t-esc="p" />.</div>
</t>
I have not tested this code but logically it seems to work fine.
Thank you so much #MICROCOM .. The line that display the score for each page is diplayed more than once ..
The result Of The code - 1
So i added a condition to just display the score of a certain page once (I don't know if it's correct but it works :p ) .. Here is the code :
<!-- First finding which page is related to the survey. This is a bit odd but I couldn't find any other way!! -->
<t t-set="pages" t-value="dict((l.id, l.page_id.title) for l in user_input.user_input_line_ids).values()" />
<!-- Then print the score per each page -->
<t t-set="previous" t-value="void" />
<t t-foreach='pages' t-as='p'>
<t t-if="p != previous">
<t t-set="page_score" t-value="sum([(uil.quizz_mark or 0.0) if uil.page_id.title == p else 0.0 for uil in user_input.user_input_line_ids])" />
<div> You scored <t t-esc="page_score" /> points in page <t t-esc="p" />.</div>
</t>
<t t-set="previous" t-value="p" />
</t>
The result Of The code - 2
Thank you so much for your help #MICROCOM ^^