Skip to content

Commit

Permalink
T0437 crowdfunding_compassion : Migration to 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ecino committed May 2, 2024
1 parent eec5333 commit 6bd1c86
Show file tree
Hide file tree
Showing 102 changed files with 11,958 additions and 8,561 deletions.
4 changes: 2 additions & 2 deletions crowdfunding_compassion/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import models
from . import controllers
from . import forms
from .hooks import pre_init_hook
from . import exceptions
from . import wizards
11 changes: 4 additions & 7 deletions crowdfunding_compassion/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@
"website": "https://github.com/CompassionCH/compassion-website",
"depends": [
"advanced_translation",
"cms_form_compassion", # compassion-modules
"theme_crowdfunding", # compassion-switzerland
"partner_communication_switzerland", # compassion-switzerland
"website_compassion", # compassion-switzerland
"crm_compassion", # compassion-modules
"event", # odoo base modules
"wordpress_configuration", # compassion-modules
"mass_mailing_switzerland",
"my_compassion",
"website_sale_donation",
],
"data": [
"security/ir.model.access.csv",
"security/access_rules.xml",
"data/crowdfunding_website.xml",
"data/crowdfunding_event_type.xml",
"data/email_templates.xml",
"data/utm_medium.xml",
"data/products.xml",
"data/action_rules.xml",
"data/queue_job.xml",
"data/form_data.xml",
"views/account_invoice_line_view.xml",
"views/crowdfunding_participant_view.xml",
"views/crowdfunding_project_view.xml",
Expand All @@ -62,12 +61,11 @@
"views/assets.xml",
"views/settings_view.xml",
"templates/crowdfunding_components.xml",
"templates/crowdfunding_form_template.xml",
"templates/homepage.xml",
"templates/my_account_crowdfunding.xml",
"templates/my_account_components.xml",
"templates/my_account_crowdfunding_page.xml",
"templates/project_creation_page.xml",
"templates/project_creation_forms.xml",
"templates/project_donation_page.xml",
"templates/presentation_page.xml",
"templates/projects_list_page.xml",
Expand All @@ -77,5 +75,4 @@
"demo": ["demo/demo.xml"],
"installable": True,
"auto_install": False,
"pre_init_hook": "pre_init_hook",
}
140 changes: 70 additions & 70 deletions crowdfunding_compassion/controllers/donation_controller.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
import werkzeug
from odoo.http import Controller, request, route

from odoo.http import request, route

from odoo.addons.cms_form.controllers.main import FormControllerMixin
from odoo.addons.cms_form_compassion.controllers.payment_controller import (
PaymentFormController,
)


class DonationController(PaymentFormController, FormControllerMixin):
class DonationController(Controller):
@route(
["/project/<model('crowdfunding.project'):project>/donation"],
[
"/project/<model('crowdfunding.project'):project>/donation",
"/project/<model('crowdfunding.project'):project>/"
"<model('crowdfunding.participant'):participant>/donation",
],
auth="public",
methods=["GET", "POST"],
website=True,
sitemap=False,
)
def project_donation_page(self, page=1, project=None, **kwargs):
def project_donation_page(self, project, page=1, participant=None, **kwargs):
"""To preselect a participant, pass its id as participant query parameter"""
if not project.website_published:
if not project.website_published and not request.env.user.has_group(
"website.group_website_designer"
):
return request.redirect("/projects")
participant = int(kwargs.pop("participant", 0))
skip_type_selection = (
not project.number_sponsorships_goal and not project.number_csp_goal
)
if int(page) == 1 and len(project.participant_ids) == 1:
page = 2
participant = project.participant_ids.id
if int(page) == 2 and not project.number_sponsorships_goal:
# Skip directly to donation page
participant = request.env["crowdfunding.participant"].browse(participant)
return self.project_donation_form_page(3, project, participant, **kwargs)
participant = project.participant_ids
elif int(page) == 1:
participant = project.participant_ids[:1]
if int(page) == 2:
if not participant:
page = 1
participant = project.participant_ids[:1]
if isinstance(participant, str):
participant = request.env["crowdfunding.participant"].browse(
int(participant)
)
if skip_type_selection:
# Skip directly to donation page
page = 3

# Used for redirection after page 2
action_url = {
"sponsorship": participant.sponsorship_url,
"csp": participant.survival_sponsorship_url,
"product": f"{project.website_url}/{participant.id}/donation?page=3",
}
return request.render(
"crowdfunding_compassion.project_donation_page",
{
"project": project.sudo(),
"selected_participant": participant,
"page": page,
"skip_type_selection": not project.number_sponsorships_goal,
"skip_type_selection": skip_type_selection,
"participant_name": participant.nickname
or participant.partner_id.preferred_name,
"action_url": action_url,
},
)

@route(
[
"/project/<model('crowdfunding.project'):project>"
"/donation/form/<model('crowdfunding.participant'):participant>"
],
"/project/<model('crowdfunding.project'):project>"
"/<model('crowdfunding.participant'):participant>"
"/donation/submit",
type="http",
auth="public",
methods=["POST"],
website=True,
sitemap=False,
)
def project_donation_form_page(
self, page=3, project=None, participant=None, **kwargs
):
if not project.website_published:
def post_donation_form(self, project, participant, amount, **post):
"""
Use the cart of the website to process the donation.
Force the price of the order line to make sure it reflects the selected
amount for donation.
:param project: the project record
:param participant: the participant record
:param amount: the donation amount
:param post: the post request
:return: the rendered page
"""
if not project.is_published:
return request.redirect("/projects")
kwargs["form_model_key"] = "cms.form.crowdfunding.donation"

donation_form = self.get_form(
"crowdfunding.participant", participant.id, **kwargs
)
donation_form.form_process()

# If the form is valid, redirect to payment
if donation_form.form_success:
return werkzeug.utils.redirect(donation_form.form_next_url(), code=303)

context = {
"project": project.sudo(),
"participant": participant.sudo(),
"form": donation_form,
"main_object": participant.sudo(),
"page": page,
}

return request.render(
"crowdfunding_compassion.project_donation_page",
context,
sale_order = request.website.sale_get_order(force_create=True).sudo()
if sale_order.state != "draft":
request.session["sale_order_id"] = None
sale_order = request.website.sale_get_order(force_create=True).sudo()
product = project.product_id
quantity = float(amount) / product.standard_price
sale_order.add_donation(
product.id,
product.standard_price,
qty=quantity,
participant_id=participant.id,
opt_out=post.get("opt_out"),
is_anonymous=post.get("is_anonymous"),
)

@route(
"/crowdfunding/payment/validate/<int:invoice_id>",
auth="public",
website=True,
sitemap=False,
)
def crowdfunding_donation_validate(self, invoice_id=None, **kwargs):
"""Method called after a payment attempt"""
try:
invoice = request.env["account.invoice"].sudo().browse(int(invoice_id))
invoice.exists().ensure_one()
transaction = invoice.get_portal_last_transaction()
except ValueError:
transaction = request.env["payment.transaction"]

if transaction.state != "done":
return request.render("crowdfunding_compassion.donation_failure")

invoice.with_delay().generate_crowdfunding_receipt()
return request.render("crowdfunding_compassion.donation_successful")
return request.redirect("/shop/checkout?express=1")
65 changes: 9 additions & 56 deletions crowdfunding_compassion/controllers/homepage_controller.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,11 @@
import base64
import logging

from odoo import _
from odoo.http import Controller, request, route
from odoo.tools.misc import file_open

from odoo.addons.website_compassion.tools.image_compression import compress_big_images

SPONSOR_HEADER = compress_big_images(
base64.b64encode(
file_open(
"crowdfunding_compassion/static/src/img/sponsor_children_banner.jpg", "rb"
).read()
),
max_width=400,
)

SPONSOR_ICON = base64.b64encode(
file_open("crowdfunding_compassion/static/src/img/icn_children.png", "rb").read()
)

_logger = logging.getLogger(__name__)


def fund_impact_val_formatting(value):
return format(value, ",d").replace(",", " ") if value > 9999 else value


def sponsorship_card_content():
value = sum(
request.env["crowdfunding.project"]
.sudo()
.search([])
.mapped("number_sponsorships_reached")
)
return {
"type": "sponsorship",
"value": fund_impact_val_formatting(value),
"name": _("Sponsor children"),
"text": _("sponsored children") if value > 1 else _("sponsored child"),
"description": _(
"""
For 42 francs a month, you're opening the way out of poverty for a child. Sponsorship
ensures that the child is known, loved and protected. In particular, it gives the child
access to schooling, tutoring, regular balanced meals, medical care and training in the
spiritual field, hygiene, etc. Every week, the child participates in the activities of
one of the project center of the 8,000 local churches that are partners of
Compassion. They allow him or her to discover and develop his or her talents."""
),
"icon_image": SPONSOR_ICON,
"header_image": SPONSOR_HEADER,
}


class HomepageController(Controller):
@route("/homepage", auth="public", website=True, sitemap=False)
def homepage(self, **kwargs):
Expand All @@ -61,8 +14,7 @@ def homepage(self, **kwargs):
self._compute_homepage_context(),
)

@staticmethod
def _compute_homepage_context():
def _compute_homepage_context(self):
# Retrieve all projects open (deadline in the future)
active_projects = (
request.env["crowdfunding.project"]
Expand All @@ -80,16 +32,17 @@ def _compute_homepage_context():
{
"name": fund.crowdfunding_impact_text_active,
"description": fund.crowdfunding_description,
"icon_image": fund.image_medium or SPONSOR_ICON,
"icon_image": fund.image_512 or active_projects.get_sponsor_icon(),
# the header is a small image so we can compress it to save space
"header_image": compress_big_images(fund.image_large, max_width=400)
"header_image": fund.image_400
if fund.image_large
else SPONSOR_HEADER,
else active_projects.get_sponsor_card_header_image(),
}
)

# populate the fund information depending on the impact type and the number of impact
impact = {"sponsorship": sponsorship_card_content()}
# populate the fund information depending on the impact type and the number
# of impact
impact = {"sponsorship": active_projects.sponsorship_card_content()}
for fund in (
request.env["product.product"]
.sudo()
Expand All @@ -105,10 +58,10 @@ def _compute_homepage_context():
fund_text = fund.crowdfunding_impact_text_passive_singular
impact[fund.name] = {
"type": "fund",
"value": fund_impact_val_formatting(impact_val),
"value": active_projects.fund_impact_val_formatting(impact_val),
"text": fund_text,
"description": fund.crowdfunding_description,
"icon_image": fund.image_medium or SPONSOR_ICON,
"icon_image": fund.image_512 or active_projects.get_sponsor_icon(),
}

subheading = _("What we achieved so far")
Expand Down
Loading

0 comments on commit 6bd1c86

Please sign in to comment.