Last active
March 24, 2021 09:46
-
-
Save Hidenmy/4fcc969feaabc0561143721d63ab90b1 to your computer and use it in GitHub Desktop.
odoo testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo import models, fields | |
from math import sqrt | |
def get_distance(place1, place2): | |
distance = sqrt((place1.latitude - place2.latitude)**2 + (place1.longitude - place2.longitude)**2) * 111 | |
return distance | |
class Location(models.Model): | |
_name = 'tourism_platform.location' | |
_description = 'Collection of locations on map' | |
name = fields.Char(required=True) | |
photo = fields.Image(max_width=1000, max_height=1000) | |
address = fields.Char() | |
phones = fields.Char() | |
website = fields.Char() | |
short_description = fields.Text(string="Short Description") | |
description = fields.Html(string="Description") | |
latitude = fields.Float(required=True, default=0, digits=(3, 15)) | |
longitude = fields.Float(required=True, default=0, digits=(3, 15)) | |
google_map = fields.Char(default='Google Map') | |
reviews_qty = fields.Integer(string='qty of views', readonly=True, compute='_get_reviews_qty') | |
rating = fields.Float(string='rating of location', readonly=True, compute='_get_rating') | |
categories = fields.Many2many('tourism_platform.category', required=True) | |
images = fields.One2many('tourism_platform.image', 'location') | |
reviews = fields.One2many('tourism_platform.review', compute='_get_reviews') | |
places_nearby = fields.One2many('tourism_platform.location', compute='_get_places_nearby') | |
def _get_reviews(self): | |
for rec in self: | |
reviews = self.env['tourism_platform.review'].search([('model', '=', 'tourism_platform.location'), | |
('obj_id', '=', rec.id)]) | |
rec.reviews = reviews | |
def _get_reviews_qty(self): | |
for rec in self: | |
rec.reviews_qty = len(rec.reviews) | |
def _get_rating(self): | |
rating = 0 | |
rating_sum = 0 | |
for rec in self: | |
if rec.reviews: | |
for review in rec.reviews: | |
rating_sum += int(review.rating) | |
rating = rating_sum / len(rec.reviews) | |
rec.rating = rating | |
def open_reviews(self): | |
res = { | |
'name': 'Review', | |
'view_type': 'tree', | |
'view_mode': 'list,form', | |
'view_id': False, | |
'res_model': 'tourism_platform.review', | |
'context': "{'search_default_model_name': 'Location', 'search_default_obj_id': '%d'}" % self.id, | |
'type': 'ir.actions.act_window', | |
'target': 'main', | |
} | |
return res | |
def _get_places_nearby(self): | |
for rec in self: | |
places_nearby = [] | |
locations = self.env['tourism_platform.location'].search([('id', '!=', rec.id)]) | |
for location in locations: | |
if get_distance(rec, location) <= 30: | |
places_nearby.append(location.id) | |
rec.places_nearby = self.env['tourism_platform.location'].search([('id', 'in', places_nearby)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo.tests.common import TransactionCase | |
class TestModelLocation(TransactionCase): | |
def setUp(self, *args, **kwargs): | |
super(TestModelLocation, self).setUp(*args, **kwargs) | |
self.category = self.env['tourism_platform.category'].create({'name': 'test_category'}) | |
self.rec = self.env['tourism_platform.location'].create({'name': 'test_location', | |
'categories': [(4, self.category.id)]}) | |
self.review = self.env['tourism_platform.review'].create({'comment': 'test_comment', | |
'rating': '5', | |
'model': 'tourism_platform.location', | |
'obj_id': self.rec.id}) | |
def test_creation(self): | |
self.assertEqual(self.rec.reviews.id, self.review.id) | |
self.assertEqual(self.rec.rating.id, self.review.id) | |
self.assertEqual(self.rec.rating, 5.0) | |
self.assertEqual(self.rec.reviews_qty, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment