|
| 1 | +# Copyright 2026 Dixmit |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from math import sqrt |
| 5 | + |
| 6 | +from odoo import _, http |
| 7 | +from odoo.http import request |
| 8 | + |
| 9 | +from odoo.addons.portal.controllers.portal import CustomerPortal |
| 10 | + |
| 11 | + |
| 12 | +class ContributorsController(CustomerPortal): |
| 13 | + @http.route( |
| 14 | + [ |
| 15 | + "/vcp", |
| 16 | + "/vcp/<string:vcp>", |
| 17 | + ], |
| 18 | + type="http", |
| 19 | + auth="user", |
| 20 | + website=True, |
| 21 | + ) |
| 22 | + def contributors_vcp(self, vcp=None): |
| 23 | + values = self._prepare_portal_layout_values() |
| 24 | + values.update(self._prepare_home_portal_values([])) |
| 25 | + if vcp is None: |
| 26 | + vcps = request.env["vcp.platform"].search([]) |
| 27 | + return request.render( |
| 28 | + "vcp.vcp_platforms_template", |
| 29 | + {"vcps": vcps, **values}, |
| 30 | + ) |
| 31 | + vcp_id = ( |
| 32 | + request.env["vcp.platform"] |
| 33 | + .sudo() |
| 34 | + .search([("name", "=ilike", vcp)], limit=1) |
| 35 | + .id |
| 36 | + ) |
| 37 | + return request.render( |
| 38 | + "vcp.vcp_platform_template", |
| 39 | + {"vcp": vcp_id, **values}, |
| 40 | + ) |
| 41 | + |
| 42 | + def _get_index(self, data): |
| 43 | + return round( |
| 44 | + sqrt(data["created_requests"]) |
| 45 | + + data["merged_requests"] |
| 46 | + + sqrt(data["comments"]) |
| 47 | + + data["reviews"], |
| 48 | + 2, |
| 49 | + ) |
| 50 | + |
| 51 | + def _get_field(self, kind): |
| 52 | + if kind == "contributors": |
| 53 | + return "partner_id" |
| 54 | + elif kind == "organizations": |
| 55 | + return "organization_id" |
| 56 | + elif kind == "repositories": |
| 57 | + return "repository_id" |
| 58 | + return False |
| 59 | + |
| 60 | + @http.route(["/vcp-fetch"], type="json", auth="user", readonly=True) |
| 61 | + def fetch_vcp_data(self, vcp_id, year, month, kind, period, **values): |
| 62 | + vcp = request.env["vcp.platform"].browse(vcp_id).exists() |
| 63 | + if not vcp: |
| 64 | + return [] |
| 65 | + start, end = vcp._get_dates(year, month, period, **values) |
| 66 | + data = vcp._generate_data(start, end, self._get_field(kind), kind, **values) |
| 67 | + return { |
| 68 | + "columns": self._get_vcp_columns(kind), |
| 69 | + "data": self._improve_vcp_data(data, kind, **values), |
| 70 | + } |
| 71 | + |
| 72 | + def _get_vcp_columns(self, kind): |
| 73 | + if kind == "contributors": |
| 74 | + return [ |
| 75 | + {"field": "name", "title": _("Name"), "kind": "name"}, |
| 76 | + { |
| 77 | + "field": "created_requests", |
| 78 | + "title": _("Created Requests"), |
| 79 | + "kind": "float", |
| 80 | + "decimals": 0, |
| 81 | + }, |
| 82 | + { |
| 83 | + "field": "merged_requests", |
| 84 | + "title": _("Merged Requests"), |
| 85 | + "kind": "float", |
| 86 | + "decimals": 0, |
| 87 | + }, |
| 88 | + { |
| 89 | + "field": "comments", |
| 90 | + "title": _("Comments"), |
| 91 | + "kind": "float", |
| 92 | + "decimals": 0, |
| 93 | + }, |
| 94 | + { |
| 95 | + "field": "reviews", |
| 96 | + "title": _("Reviews"), |
| 97 | + "kind": "float", |
| 98 | + "decimals": 0, |
| 99 | + }, |
| 100 | + ] |
| 101 | + elif kind == "organizations": |
| 102 | + return [ |
| 103 | + {"field": "name", "title": _("Organization Name"), "kind": "name"}, |
| 104 | + { |
| 105 | + "field": "created_requests", |
| 106 | + "title": _("Created Requests"), |
| 107 | + "kind": "float", |
| 108 | + "decimals": 0, |
| 109 | + }, |
| 110 | + { |
| 111 | + "field": "merged_requests", |
| 112 | + "title": _("Merged Requests"), |
| 113 | + "kind": "float", |
| 114 | + "decimals": 0, |
| 115 | + }, |
| 116 | + { |
| 117 | + "field": "comments", |
| 118 | + "title": _("Comments"), |
| 119 | + "kind": "float", |
| 120 | + "decimals": 0, |
| 121 | + }, |
| 122 | + { |
| 123 | + "field": "reviews", |
| 124 | + "title": _("Reviews"), |
| 125 | + "kind": "float", |
| 126 | + "decimals": 0, |
| 127 | + }, |
| 128 | + { |
| 129 | + "field": "developers", |
| 130 | + "title": _("Developers"), |
| 131 | + "kind": "float", |
| 132 | + "decimals": 0, |
| 133 | + }, |
| 134 | + ] |
| 135 | + elif kind == "repositories": |
| 136 | + return [ |
| 137 | + {"field": "name", "title": _("Repository Name"), "kind": "name"}, |
| 138 | + { |
| 139 | + "field": "created_requests", |
| 140 | + "title": _("Created Requests"), |
| 141 | + "kind": "float", |
| 142 | + "decimals": 0, |
| 143 | + }, |
| 144 | + { |
| 145 | + "field": "merged_requests", |
| 146 | + "title": _("Merged Requests"), |
| 147 | + "kind": "float", |
| 148 | + "decimals": 0, |
| 149 | + }, |
| 150 | + { |
| 151 | + "field": "comments", |
| 152 | + "title": _("Comments"), |
| 153 | + "kind": "float", |
| 154 | + "decimals": 0, |
| 155 | + }, |
| 156 | + { |
| 157 | + "field": "reviews", |
| 158 | + "title": _("Reviews"), |
| 159 | + "kind": "float", |
| 160 | + "decimals": 0, |
| 161 | + }, |
| 162 | + { |
| 163 | + "field": "developers", |
| 164 | + "title": _("Developers"), |
| 165 | + "kind": "float", |
| 166 | + "decimals": 0, |
| 167 | + }, |
| 168 | + ] |
| 169 | + return [] |
| 170 | + |
| 171 | + def _improve_vcp_data(self, data, kind, **kwargs): |
| 172 | + for key, values in data.items(): |
| 173 | + if kind == "contributors": |
| 174 | + partner = request.env["res.partner"].browse(key) |
| 175 | + values["name"] = partner._get_contributors_name(kind, **kwargs) |
| 176 | + values["url"] = partner._get_contributor_url() |
| 177 | + elif kind == "organizations": |
| 178 | + organization = request.env["res.partner"].browse(key) |
| 179 | + values["name"] = organization._get_contributors_name(kind, **kwargs) |
| 180 | + values["url"] = organization._get_contributor_url() |
| 181 | + elif kind == "repositories": |
| 182 | + repository = request.env["vcp.repository"].browse(key) |
| 183 | + values["name"] = repository.name |
| 184 | + values["url"] = repository._get_repository_url() |
| 185 | + return data |
0 commit comments