Debian Codegraph @

addons/marketing_card/controllers/marketing_card.py

raw
1 import base64
2 from urllib.parse import quote
3 from werkzeug.exceptions import BadRequest
4
5 from odoo.http import Controller, content_disposition, request, route
6
7 # from https://github.com/monperrus/crawler-user-agents
8 SOCIAL_NETWORK_USER_AGENTS = (
9 # Facebook
10 'Facebot',
11 'facebookexternalhit',
12 # Twitter
13 'Twitterbot',
14 # LinkedIn
15 'LinkedInBot',
16 # Whatsapp
17 'WhatsApp',
18 # Pinterest
19 'Pinterest',
20 'Pinterestbot',
21 )
22
23
24 def _is_crawler(request):
25 """Returns True if the request is made by a social network crawler."""
26 return any(
27 short_crawler_name in request.httprequest.user_agent.string
28 for short_crawler_name in SOCIAL_NETWORK_USER_AGENTS
29 )
30
31
32 def _get_card_from_url(card_id, card_slug):
33 """Helper to support both legacy card id url and new slug urls"""
34 if card_slug:
35 card_id = request.env['ir.http']._unslug(card_slug)[1]
36 if not card_id:
37 raise request.not_found()
38 card = request.env['card.card'].browse(card_id).exists()
39 if not card:
40 raise BadRequest()
41 return card
42
43
44 class MarketingCardController(Controller):
45
46 @route([
47 '/cards/<string:card_slug>/card.jpg',
48 '/cards/<int:card_id>/card.jpg',
49 ], type='http', auth='public', sitemap=False, website=True)
50 def card_campaign_image(self, card_id=None, card_slug=None):
51 card = _get_card_from_url(card_id, card_slug)
52 if _is_crawler(request) and card.share_status != 'shared':
53 card.sudo().share_status = 'shared'
54 if not card.image:
55 raise request.not_found()
56
57 image_bytes = base64.b64decode(card.image)
58 return request.make_response(image_bytes, [
59 ('Content-Type', ' image/jpeg'),
60 ('Content-Length', len(image_bytes)),
61 ('Content-Disposition', content_disposition('card.jpg')),
62 ])
63
64 @route([
65 '/cards/<string:card_slug>/preview',
66 '/cards/<int:card_id>/preview',
67 ], type='http', auth='public', sitemap=False, website=True)
68 def card_campaign_preview(self, card_id=None, card_slug=None):
69 """Route for users to preview their card and share it on their social platforms."""
70 card = _get_card_from_url(card_id, card_slug)
71 if not card.share_status:
72 card.sudo().share_status = 'visited'
73
74 campaign_sudo = card.sudo().campaign_id
75 return request.render('marketing_card.card_campaign_preview', {
76 'card': card,
77 'campaign': campaign_sudo,
78 'quote': quote,
79 })
80
81 @route([
82 '/cards/<string:card_slug>/redirect',
83 '/cards/<int:card_id>/redirect',
84 ], type='http', auth='public', sitemap=False, website=True)
85 def card_campaign_redirect(self, card_id=None, card_slug=None):
86 """Route to redirect users to the target url, or display the opengraph embed text for web crawlers.
87
88 When a user posts a link on an application supporting opengraph, the application will follow
89 the link to fetch specific meta tags on the web page to get preview information such as a preview card.
90 The "crawler" performing that action usually has a specific user agent.
91
92 As we cannot necessarily control the target url of the campaign we must return a different
93 result when a social network crawler is visiting the URL to get preview information.
94 From the perspective of the crawler, this url is an empty page with opengraph tags.
95 For all other user agents, it's a simple redirection url.
96
97 Keeping an up-to-date list of user agents for each supported target website is imperative
98 for this app to work.
99 """
100 card = _get_card_from_url(card_id, card_slug)
101
102 campaign_sudo = card.sudo().campaign_id
103 # don't count clicks from preview
104 redirect_url = campaign_sudo.target_url or campaign_sudo.get_base_url()
105 if card.active:
106 redirect_url = campaign_sudo.link_tracker_id.short_url or redirect_url
107
108 if _is_crawler(request):
109 return request.render('marketing_card.card_campaign_crawler', {
110 'image_url': card._get_card_url(),
111 'post_text': campaign_sudo.post_suggestion,
112 'post_suggestion': campaign_sudo.post_suggestion,
113 'target_name': card.display_name or '',
114 })
115
116 return request.redirect(redirect_url)