diff options
| author | Thibault Delavallée <tde@odoo.com> | 2020-04-16 09:46:47 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2020-04-16 10:26:32 +0000 |
| commit | b72b29f9900847e2cf5711b22243a1a462dd7650 (patch) | |
| tree | dd5763fd82a0e0691e46f136fd5c9e9d42a77564 /addons/website_slides_survey | |
| parent | 24033e4aaf65a52fedaa13fc0846eb745238f09d (diff) | |
[IMP] website_slides_survey: merge slides controller files
Two files holding slides-related controllers exist. Let us merge them in
a single one to simplify organization.
Task ID 1981399
Diffstat (limited to 'addons/website_slides_survey')
| -rw-r--r-- | addons/website_slides_survey/controllers/__init__.py | 1 | ||||
| -rw-r--r-- | addons/website_slides_survey/controllers/main.py | 67 | ||||
| -rw-r--r-- | addons/website_slides_survey/controllers/slides.py | 70 |
3 files changed, 63 insertions, 75 deletions
diff --git a/addons/website_slides_survey/controllers/__init__.py b/addons/website_slides_survey/controllers/__init__.py index 3e0e20e525f..800368ade12 100644 --- a/addons/website_slides_survey/controllers/__init__.py +++ b/addons/website_slides_survey/controllers/__init__.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from . import main from . import slides from . import survey diff --git a/addons/website_slides_survey/controllers/main.py b/addons/website_slides_survey/controllers/main.py deleted file mode 100644 index a0b4cf9b53c..00000000000 --- a/addons/website_slides_survey/controllers/main.py +++ /dev/null @@ -1,67 +0,0 @@ -# -*- coding: utf-8 -*- -# Part of Odoo. See LICENSE file for full copyright and licensing details. - -from odoo import http, _ -from odoo.http import request -from odoo.exceptions import AccessError - -from odoo.addons.website_slides.controllers.main import WebsiteSlides - - -class WebsiteSlidesSurvey(WebsiteSlides): - - @http.route(['/slides_survey/certification/search_read'], type='json', auth='user', methods=['POST'], website=True) - def slides_certification_search_read(self, fields): - can_create = request.env['survey.survey'].check_access_rights('create', raise_exception=False) - return { - 'read_results': request.env['survey.survey'].search_read([('certification', '=', True)], fields), - 'can_create': can_create, - } - - # ------------------------------------------------------------ - # Overrides - # ------------------------------------------------------------ - - @http.route(['/slides/add_slide'], type='json', auth='user', methods=['POST'], website=True) - def create_slide(self, *args, **post): - create_new_survey = post['slide_type'] == "certification" and post.get('survey') and not post['survey']['id'] - linked_survey_id = int(post.get('survey', {}).get('id') or 0) - - if create_new_survey: - # If user cannot create a new survey, no need to create the slide either. - if not request.env['survey.survey'].check_access_rights('create', raise_exception=False): - return {'error': _('You are not allowed to create a survey.')} - - # Create survey first as certification slide needs a survey_id (constraint) - post['survey_id'] = request.env['survey.survey'].create({ - 'title': post['survey']['title'], - 'background_image': post['image_1920'], - 'questions_layout': 'page_per_question', - 'is_attempts_limited': True, - 'attempts_limit': 1, - 'is_time_limited': False, - 'scoring_type': 'scoring_without_answers', - 'certification': True, - 'scoring_success_min': 70.0, - 'certification_mail_template_id': request.env.ref('survey.mail_template_certification').id, - }).id - elif linked_survey_id: - try: - request.env['survey.survey'].browse([linked_survey_id]).read(['title']) - except AccessError: - return {'error': _('You are not allowed to link a certification.')} - - post['survey_id'] = post['survey']['id'] - - # Then create the slide - result = super(WebsiteSlidesSurvey, self).create_slide(*args, **post) - - if create_new_survey: - # Set the redirect_url used in toaster - action_id = request.env.ref('survey.action_survey_form').id - result.update({ - 'redirect_url': '/web#id=%s&action=%s&model=survey.survey&view_type=form' % (post['survey_id'], action_id), - 'redirect_to_certification': True - }) - - return result diff --git a/addons/website_slides_survey/controllers/slides.py b/addons/website_slides_survey/controllers/slides.py index bd7f34349be..a810f878ba8 100644 --- a/addons/website_slides_survey/controllers/slides.py +++ b/addons/website_slides_survey/controllers/slides.py @@ -7,14 +7,14 @@ import werkzeug.exceptions from odoo import _ from odoo import http +from odoo.exceptions import AccessError from odoo.http import request from odoo.osv import expression from odoo.addons.website_slides.controllers.main import WebsiteSlides -from collections import defaultdict -class WebsiteSlides(WebsiteSlides): +class WebsiteSlidesSurvey(WebsiteSlides): @http.route(['/slides_survey/slide/get_certification_url'], type='http', auth='user', website=True) def slide_get_certification_url(self, slide_id, **kw): @@ -29,22 +29,78 @@ class WebsiteSlides(WebsiteSlides): raise werkzeug.exceptions.NotFound() return werkzeug.utils.redirect(certification_url) + @http.route(['/slides_survey/certification/search_read'], type='json', auth='user', methods=['POST'], website=True) + def slides_certification_search_read(self, fields): + can_create = request.env['survey.survey'].check_access_rights('create', raise_exception=False) + return { + 'read_results': request.env['survey.survey'].search_read([('certification', '=', True)], fields), + 'can_create': can_create, + } + + # ------------------------------------------------------------ + # Overrides + # ------------------------------------------------------------ + + @http.route(['/slides/add_slide'], type='json', auth='user', methods=['POST'], website=True) + def create_slide(self, *args, **post): + create_new_survey = post['slide_type'] == "certification" and post.get('survey') and not post['survey']['id'] + linked_survey_id = int(post.get('survey', {}).get('id') or 0) + + if create_new_survey: + # If user cannot create a new survey, no need to create the slide either. + if not request.env['survey.survey'].check_access_rights('create', raise_exception=False): + return {'error': _('You are not allowed to create a survey.')} + + # Create survey first as certification slide needs a survey_id (constraint) + post['survey_id'] = request.env['survey.survey'].create({ + 'title': post['survey']['title'], + 'background_image': post['image_1920'], + 'questions_layout': 'page_per_question', + 'is_attempts_limited': True, + 'attempts_limit': 1, + 'is_time_limited': False, + 'scoring_type': 'scoring_without_answers', + 'certification': True, + 'scoring_success_min': 70.0, + 'certification_mail_template_id': request.env.ref('survey.mail_template_certification').id, + }).id + elif linked_survey_id: + try: + request.env['survey.survey'].browse([linked_survey_id]).read(['title']) + except AccessError: + return {'error': _('You are not allowed to link a certification.')} + + post['survey_id'] = post['survey']['id'] + + # Then create the slide + result = super(WebsiteSlidesSurvey, self).create_slide(*args, **post) + + if create_new_survey: + # Set the redirect_url used in toaster + action_id = request.env.ref('survey.action_survey_form').id + result.update({ + 'redirect_url': '/web#id=%s&action=%s&model=survey.survey&view_type=form' % (post['survey_id'], action_id), + 'redirect_to_certification': True + }) + + return result + # Utils # --------------------------------------------------- def _set_completed_slide(self, slide): if slide.slide_type == 'certification': raise werkzeug.exceptions.Forbidden(_("Certification slides are completed when the survey is succeeded.")) - return super(WebsiteSlides, self)._set_completed_slide(slide) + return super(WebsiteSlidesSurvey, self)._set_completed_slide(slide) def _get_valid_slide_post_values(self): - result = super(WebsiteSlides, self)._get_valid_slide_post_values() + result = super(WebsiteSlidesSurvey, self)._get_valid_slide_post_values() result.append('survey_id') return result # Profile # --------------------------------------------------- def _prepare_user_slides_profile(self, user): - values = super(WebsiteSlides, self)._prepare_user_slides_profile(user) + values = super(WebsiteSlidesSurvey, self)._prepare_user_slides_profile(user) values.update({ 'certificates': self._get_users_certificates(user)[user.id] }) @@ -53,7 +109,7 @@ class WebsiteSlides(WebsiteSlides): # All Users Page # --------------------------------------------------- def _prepare_all_users_values(self, users): - result = super(WebsiteSlides, self)._prepare_all_users_values(users) + result = super(WebsiteSlidesSurvey, self)._prepare_all_users_values(users) certificates_per_user = self._get_users_certificates(users) for index, user in enumerate(users): result[index].update({ @@ -81,7 +137,7 @@ class WebsiteSlides(WebsiteSlides): def _prepare_ranks_badges_values(self, **kwargs): """ Extract certification badges, to render them in ranks/badges page in another section. Order them by number of granted users desc and show only badges linked to opened certifications.""" - values = super(WebsiteSlides, self)._prepare_ranks_badges_values(**kwargs) + values = super(WebsiteSlidesSurvey, self)._prepare_ranks_badges_values(**kwargs) # 1. Getting all certification badges, sorted by granted user desc domain = expression.AND([[('survey_id', '!=', False)], self._prepare_badges_domain(**kwargs)]) |
