Files
odoo_source/addons/website_slides/tests/test_karma.py
T
Aurélien Warnon 7b5427f6d6 [REF] website_slides: refactor external content management in slide.slide
PURPOSE

Globally refactor the way we handle external content (YouTube videos,
Google Drive documents, ...) in the course slides.

This includes both technical changes (new computed fields, rename, ...) as well
as visual changes on the form views that allow introducing new content, both in
the backend (slide.slide form view) and in the frontend (course main website
page).

SPECS

TECHNICAL REFACTOR

1. Rework the "url" field

This field was used without any distinction for videos, regular documents and
images, leading to a big confusion both technically and visually.

The urls are now split in separate fields (video_url, document_google_url and
image_google_url), based on the type of slide.
Those fields are implemented as 'related' to the stored 'url' field to avoid
extra storage.

This change globally increases the readability and usability in the code, and
also allows for clear labels on the various views.

1b. Rework the "binary_content" field

The same way as for the 'url' field, the 'binary_content' field has been split
into 2 related fields ('document_binary_content' and 'image_binary_content').

This allows for more readability and also helps in the form view to limit the
file selection to the supported file types.

2. Remove the "document_id" stored field

This field did not have to be stored since it can "easily" be inferred from the
video/document URL with some regex work.

3. Remove the "mime_type" field

This field's value was only determined by an external call to the Google Drive
API. It was inconsistent because not set for manually uploaded document and was
used for some strange checks that seemed to work but did not make much sense.
(e.g: used to make the difference between a Google Drive video and a YouTube
video??).

It has been replaced by a "document_type" field, which is always 'pdf' for
local files (only type we support) and inferred from the mimeType returned by
Google Drive metadata for external documents.

This allows, for instance, to show pretty icons next to every slide based on
their type, "sheet" files (Excel, Google Sheet, ...) share the same icons, the
same principle is applied to "doc" files (Word, OpenOffice, Google Doc, ...)
and finally "slides" files (PowerPoint, Google Slides, ...).

4. Introduce several computed fields

Such as:
- video_source_type
- youtube_id
- google_drive_id

These fields are mostly inferred from the slide type and the related URL.
They are used to make clear checks in the code and ease readability, for
example when constructing the slide "embed code" to insert it in the frontend
full screen viewer.

5. Remove the "presentation" slide_type

The slide type "presentation" was some kind of very confusing type between
image and pdf. It was mostly relying on the fact that the content had a bigger
width than height ('landscape' display).

This lead to a lot of confusion both in the code base and in the various forms
allowing to introduce new content ("what should I choose? document or
presentation?").

In order to simplify everything, we completely removed the slide type
"presentation" and converted all the data that had this type and the various
code checks to the type "document" instead.

Meaning that we end up with the following slide types:
- Infographic - for images, local files + external links
- Web Page - local input only
- Document local files + external links
- Video - external only, YouTube or Google Drive links
- Quiz

FUNCTIONAL

Following the technical changes here above, we can improve the form view of the
slide.slide model as well as the website form that allows adding new content.

1. Slide form view

To make it clear where the source of the document comes from, we introduced a
"source_type" field that is either "local_file", used when uploading a file
from your computer, or "external", used when linking content from Google Drive.

The source_type selection appears when adding content of type "document" or
"infographic" and conditions the display of the file upload button or the
document_url field.

2. Adding content from the website

When adding content from the website, the user is now invited to select from
ALL the available slide type, meaning we added "Infographic" to the selection.

Before this change, if you wanted to link an image, you had to select the type
"presentation" (that has a PDF file as icon...) and then input an image file.
The JS then processed the file type to determine the slide type.

As this was slightly confusing for the end user, we decided to use the same
approach as the form view, meaning you FIRST select the type of slide you want
and THEN input the content as a local file or external link.

Since we now handle all types of documents when using an external Google Drive
link, the various screen and text helpers have been reworked accordingly.

3. Handle more types of slides with the new "slide_type" field

This commit also introduces a new "slide_type" field.
(Don't get confused, the previous "slide_type" field has been renamed to
"slide_category" in an earlier rename commit).

This slide_type is a refined slide_category:
- Videos are split into 'youtube_video' and 'google_drive_video'
- Documents are refined based on the file mime_type if it's external content
  For example, if you link a Excel file, the slide_type will be 'sheet'.
  If you link a Google Doc, the slide_type will be 'doc'
  etc...
  (Local documents are only PDFs since it's the only type we support for now)

This small change allows to include minor nice details into views, such as a
refined icon for each slide_type on the website.

LINKS

Task-2510174
UPG PR odoo/upgrade#2498

Part-of: odoo/odoo#71477
2021-11-30 15:21:48 +00:00

114 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.website_slides.tests import common
from odoo.tests import tagged
from odoo.tests.common import users
from odoo.tools import mute_logger
@tagged('functional')
class TestKarmaGain(common.SlidesCase):
def setUp(self):
super(TestKarmaGain, self).setUp()
self.channel_2 = self.env['slide.channel'].with_user(self.user_officer).create({
'name': 'Test Channel 2',
'channel_type': 'training',
'promote_strategy': 'most_voted',
'enroll': 'public',
'visibility': 'public',
'is_published': True,
'karma_gen_channel_finish': 100,
'karma_gen_slide_vote': 5,
'karma_gen_channel_rank': 10,
})
self.slide_2_0 = self.env['slide.slide'].with_user(self.user_officer).create({
'name': 'How to travel through space and time',
'channel_id': self.channel_2.id,
'slide_category': 'document',
'is_published': True,
'completion_time': 2.0,
})
self.slide_2_1 = self.env['slide.slide'].with_user(self.user_officer).create({
'name': 'How to duplicate yourself',
'channel_id': self.channel_2.id,
'slide_category': 'document',
'is_published': True,
'completion_time': 2.0,
})
@mute_logger('odoo.models')
@users('user_emp', 'user_portal', 'user_officer')
def test_karma_gain(self):
user = self.env.user
user.write({'karma': 0})
computed_karma = 0
# Add the user to the course
(self.channel | self.channel_2)._action_add_members(user.partner_id)
self.assertEqual(user.karma, 0)
# Finish the Course
self.slide.with_user(user).action_set_completed()
self.assertFalse(self.channel.with_user(user).completed)
self.slide_2.with_user(user).action_set_completed()
# answer a quizz question
self.slide_3.with_user(user).action_set_viewed(quiz_attempts_inc=True)
self.slide_3.with_user(user)._action_set_quiz_done()
self.slide_3.with_user(user).action_set_completed()
computed_karma += self.slide_3.quiz_first_attempt_reward
computed_karma += self.channel.karma_gen_channel_finish
self.assertTrue(self.channel.with_user(user).completed)
self.assertEqual(user.karma, computed_karma)
# Begin then finish the second Course
self.slide_2_0.with_user(user).action_set_completed()
self.assertFalse(self.channel_2.with_user(user).completed)
self.assertEqual(user.karma, computed_karma)
self.slide_2_1.with_user(user).action_set_completed()
self.assertTrue(self.channel_2.with_user(user).completed)
computed_karma += self.channel_2.karma_gen_channel_finish
self.assertEqual(user.karma, computed_karma)
# Vote for a slide
slide_user = self.slide.with_user(user)
slide_user.action_like()
computed_karma += self.channel.karma_gen_slide_vote
self.assertEqual(user.karma, computed_karma)
slide_user.action_like() # re-like something already liked should not add karma again
self.assertEqual(user.karma, computed_karma)
slide_user.action_dislike()
computed_karma -= self.channel.karma_gen_slide_vote
self.assertEqual(user.karma, computed_karma)
slide_user.action_dislike()
computed_karma -= self.channel.karma_gen_slide_vote
self.assertEqual(user.karma, computed_karma)
slide_user.action_dislike() # dislike again something already disliked should not remove karma again
self.assertEqual(user.karma, computed_karma)
# Leave the finished course
self.channel._remove_membership(user.partner_id.ids)
computed_karma -= self.channel.karma_gen_channel_finish
computed_karma -= self.slide_3.quiz_first_attempt_reward
self.assertEqual(user.karma, computed_karma)
@mute_logger('odoo.models')
@users('user_emp', 'user_portal', 'user_officer')
def test_karma_gain_multiple_course(self):
user = self.env.user
user.write({'karma': 0})
computed_karma = 0
# Finish two course at the same time (should not ever happen but hey, we never know)
(self.channel | self.channel_2)._action_add_members(user.partner_id)
computed_karma += self.channel.karma_gen_channel_finish + self.channel_2.karma_gen_channel_finish
(self.slide | self.slide_2 | self.slide_3 | self.slide_2_0 | self.slide_2_1).with_user(user).action_set_completed()
self.assertEqual(user.karma, computed_karma)