[REF] website_slides: extract code to have slides per category
Purpose of this commit is to have a method allowing to fetch slides and have them ordered by category with some info on the category. It fully supports uncategorized slides. This commit also fixes the lessons list view of a training course. When having rights to edit the channel the category name was not editable. We now use a real browse record allowing to edit the category names directly in this reorderable list view. Commit linked to task ID 1941250 and PR #31708 .
This commit is contained in:
@@ -376,64 +376,12 @@ class WebsiteSlides(WebsiteProfile):
|
||||
# of them but unreachable ones won't be clickable (+ slide controller will crash anyway)
|
||||
# documentation mode may display less slides than content by category but overhead of
|
||||
# computation is reasonable
|
||||
if not category and not uncategorized:
|
||||
category_data = []
|
||||
all_categories = request.env['slide.category'].search([('channel_id', '=', channel.id)])
|
||||
all_slides = request.env['slide.slide'].sudo().search(domain, order=order)
|
||||
|
||||
uncategorized_slides = all_slides.filtered(lambda slide: not slide.category_id)
|
||||
displayed_slides = uncategorized_slides
|
||||
if channel.channel_type == 'documentation':
|
||||
displayed_slides = uncategorized_slides[:self.SLIDES_PER_CATEGORY]
|
||||
if channel.channel_type == 'training' or displayed_slides:
|
||||
category_data.append({
|
||||
'category': False, 'id': False,
|
||||
'name': _('Uncategorized'), 'slug_name': _('Uncategorized'),
|
||||
'total_slides': len(uncategorized_slides),
|
||||
'slides': displayed_slides,
|
||||
})
|
||||
for category in all_categories:
|
||||
category_slides = all_slides.filtered(lambda slide: slide.category_id == category)
|
||||
displayed_slides = category_slides
|
||||
if channel.channel_type == 'documentation':
|
||||
displayed_slides = category_slides[:self.SLIDES_PER_CATEGORY]
|
||||
if not displayed_slides:
|
||||
continue
|
||||
category_data.append({
|
||||
'id': category.id,
|
||||
'name': category.name,
|
||||
'slug_name': slug(category),
|
||||
'total_slides': len(category_slides),
|
||||
'slides': displayed_slides,
|
||||
})
|
||||
elif uncategorized:
|
||||
if channel.channel_type == 'documentation':
|
||||
slides_sudo = request.env['slide.slide'].sudo().search(domain, limit=self.SLIDES_PER_PAGE, offset=pager['offset'], order=order)
|
||||
else:
|
||||
slides_sudo = request.env['slide.slide'].sudo().search(domain, order=order)
|
||||
category_data = [{
|
||||
'category': False,
|
||||
'id': False,
|
||||
'name': _('Uncategorized'),
|
||||
'slug_name': _('Uncategorized'),
|
||||
'total_slides': len(slides_sudo),
|
||||
'slides': slides_sudo,
|
||||
}]
|
||||
else:
|
||||
if channel.channel_type == 'documentation':
|
||||
slides_sudo = request.env['slide.slide'].sudo().search(domain, limit=self.SLIDES_PER_PAGE, offset=pager['offset'], order=order)
|
||||
else:
|
||||
slides_sudo = request.env['slide.slide'].sudo().search(domain, order=order)
|
||||
category_data = [{
|
||||
'category': category,
|
||||
'id': category.id,
|
||||
'name': category.name,
|
||||
'slug_name': slug(category),
|
||||
'total_slides': len(slides_sudo),
|
||||
'slides': slides_sudo,
|
||||
}]
|
||||
values['slide_promoted'] = request.env['slide.slide'].sudo().search(domain, limit=1, order=order)
|
||||
values['category_data'] = category_data
|
||||
values['category_data'] = channel._get_categorized_slides(
|
||||
domain, order,
|
||||
force_void=True,
|
||||
limit=self.SLIDES_PER_CATEGORY if channel.channel_type == 'documentation' else False,
|
||||
offset=pager['offset'])
|
||||
values['channel_progress'] = self._get_channel_progress(channel, include_quiz=True)
|
||||
|
||||
values = self._prepare_additional_channel_values(values, **kw)
|
||||
|
||||
@@ -418,6 +418,41 @@ class Channel(models.Model):
|
||||
domain = super(Channel, self)._rating_domain()
|
||||
return expression.AND([domain, [('website_published', '=', True)]])
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Data / Misc
|
||||
# ---------------------------------------------------------
|
||||
|
||||
def _get_categorized_slides(self, base_domain, order, force_void=True, limit=False, offset=False):
|
||||
""" Return an ordered structure of slides by categories within a given
|
||||
base_domain that must fulfill slides. """
|
||||
self.ensure_one()
|
||||
all_categories = self.env['slide.category'].search([('channel_id', '=', self.id)])
|
||||
all_slides = self.env['slide.slide'].sudo().search(base_domain, order=order)
|
||||
category_data = []
|
||||
|
||||
# First add uncategorized slides
|
||||
uncategorized_slides = all_slides.filtered(lambda slide: not slide.category_id)
|
||||
if uncategorized_slides or force_void:
|
||||
category_data.append({
|
||||
'category': False, 'id': False,
|
||||
'name': _('Uncategorized'), 'slug_name': _('Uncategorized'),
|
||||
'total_slides': len(uncategorized_slides),
|
||||
'slides': uncategorized_slides[(offset or 0):(limit or len(uncategorized_slides))],
|
||||
})
|
||||
# Then all categories by natural order
|
||||
for category in all_categories:
|
||||
category_slides = all_slides.filtered(lambda slide: slide.category_id == category)
|
||||
if not category_slides and not force_void:
|
||||
continue
|
||||
category_data.append({
|
||||
'category': category, 'id': category.id,
|
||||
'name': category.name, 'slug_name': slug(category),
|
||||
'total_slides': len(category_slides),
|
||||
'slides': category_slides[(offset or 0):(limit or len(category_slides))],
|
||||
})
|
||||
|
||||
return category_data
|
||||
|
||||
|
||||
class Category(models.Model):
|
||||
""" Channel contain various categories to manage its slides """
|
||||
|
||||
@@ -267,6 +267,7 @@
|
||||
<ul class="o_wslides_js_slides_list_container border-bottom list-unstyled">
|
||||
<t t-set="j" t-value="0"/>
|
||||
<t t-foreach="category_data" t-as="category">
|
||||
<t t-set="category_record" t-value="category['category'] if category['id'] else None"/>
|
||||
<t t-set="category_id" t-value="category['id'] if category['id'] else None"/>
|
||||
<li t-if="category['total_slides'] or channel.can_publish" class="o_wslides_slide_list_category"
|
||||
t-att-data-category-id="category_id">
|
||||
@@ -274,8 +275,8 @@
|
||||
t-att-class="'text-muted bg-white border-bottom font-weight-bold pt-0 pb-0 pl-2 pr-2 d-flex justify-content-between align-items-center %s' % ('o_wslides_js_category' if category_id else '')">
|
||||
<div class="pt-2 pb-2 d-flex align-items-center">
|
||||
<i t-if="channel.can_publish and category_id" class="fa fa-arrows mr-3"></i>
|
||||
<t t-if="category_id">
|
||||
<span t-esc="category['name']"/>
|
||||
<t t-if="category_record">
|
||||
<span t-field="category_record.name"/>
|
||||
</t>
|
||||
<t t-else="">
|
||||
<span>Uncategorized</span>
|
||||
|
||||
Reference in New Issue
Block a user