Skip to content

Commit

Permalink
Merge pull request #87 from wmo-raf/dev
Browse files Browse the repository at this point in the history
Updates and Bug Fixes
  • Loading branch information
erick-otenyo authored Mar 27, 2024
2 parents 50562f6 + dc30183 commit e645674
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.3 on 2024-03-27 11:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('base', '0026_remove_importantpages_cap_feed_and_more'),
]

operations = [
migrations.AddField(
model_name='integrationsettings',
name='google_site_verification_key',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Google Site Verification Key'),
),
]
6 changes: 6 additions & 0 deletions base/models/site_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class IntegrationSettings(BaseSiteSetting):
'Track all pages internally. This will enable the internal analytics dashboard, '
'alongside Google Analytics, if also enabled'), )

google_site_verification_key = models.CharField(max_length=255, blank=True, null=True,
verbose_name=_("Google Site Verification Key"), )

edit_handler = TabbedInterface([
ObjectList([
FieldPanel('youtube_api')
Expand All @@ -175,6 +178,9 @@ class IntegrationSettings(BaseSiteSetting):
FieldPanel('ga_track_button_clicks'),
FieldPanel('track_internally'),
], heading=_("Google Analytics")),
ObjectList([
FieldPanel('google_site_verification_key'),
], heading=_("Google Search")),
])

class Meta:
Expand Down
67 changes: 43 additions & 24 deletions base/templatetags/get_share_url.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
import urllib.parse

from django import template
from django.conf import settings

register = template.Library()

SOCIAL_MEDIA_SHARE_CONFIG = getattr(settings, 'SOCIAL_MEDIA_SHARE_CONFIG', {})
ONLINE_SHARE_CONFIG = getattr(settings, 'ONLINE_SHARE_CONFIG', {})


@register.inclusion_tag("social_media_share_buttons_include.html")
def share_buttons(url, text=None):
share_urls = []
for config in ONLINE_SHARE_CONFIG:
enabled = config.get('enabled', False)
name = config.get('name', None)
base_url = config.get('base_url', None)
link_param = config.get('link_param', None)
text_in_url = config.get('text_in_url', False)
text_param = config.get('text_param', None)
encode = config.get('encode', False)
fa_icon = config.get('fa_icon', False)

link_query = None
text_query = None

@register.simple_tag
def get_social_media_share(platform, url, text=None):
share_url = None
item_url = url
item_text = text

try:
config = SOCIAL_MEDIA_SHARE_CONFIG[platform]
if not enabled or not name or not base_url or not link_param:
continue

base_config = config.get('base_url', None)
url_config = config.get('link_param', None)
text_config = config.get('text_param', None)
if encode:
item_url = urllib.parse.quote(item_url)
if item_text:
item_text = urllib.parse.quote(item_text)

if base_config and url_config:
text_param = None
url_param = None
if item_text:
if text_in_url:
item_url = f"{item_text}%20{item_url}"
elif text_param:
text_query = f"{text_param}={item_text}"

if url_config and url:
url_param = '{}={}'.format(url_config, url)
if link_param and item_url:
link_query = f"{link_param}={item_url}"

if text_config and text:
text_param = '{}={}'.format(text_config, text)
share_url = f"{base_url}?{link_query}"

if url_param:
share_url = '{}?{}'.format(base_config, url_param)
if text_param:
share_url = '{}&{}'.format(share_url, text_param)
if text_query:
share_url = f"{share_url}&{text_query}"

return share_url
except KeyError:
return None
share_urls.append({
'name': name,
'url': share_url,
'fa_icon': fa_icon
})

return share_url
return {"share_urls": share_urls}
47 changes: 43 additions & 4 deletions nmhs_cms/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,49 @@
ACCOUNT_USERNAME_BLACKLIST = ["admin", "god", "superadmin", "staff"]
ACCOUNT_USERNAME_MIN_LENGTH = 3

SOCIAL_MEDIA_SHARE_CONFIG = {
'facebook': {"base_url": "https://www.facebook.com/sharer/sharer.php", "link_param": "u"},
'twitter': {"base_url": "http://twitter.com/share", "text_param": "text", "link_param": "url"}
}
ONLINE_SHARE_CONFIG = [
{
"name": "Facebook",
"base_url": "https://www.facebook.com/sharer/sharer.php",
"link_param": "u",
"text_param": "quote",
"fa_icon": "facebook-f",
"enabled": True
},
{
"name": "X",
"base_url": "https://twitter.com/intent/post",
"link_param": "url",
"text_param": "text",
"fa_icon": "x-twitter",
"enabled": True,
},
{
"name": "LinkedIn",
"base_url": "https://www.linkedin.com/sharing/share-offsite",
"link_param": "url",
"fa_icon": "linkedin-in",
"enabled": True,
},
{
"name": "WhatsApp",
"base_url": "https://api.whatsapp.com/send",
"link_param": "text",
"encode": True,
"text_in_url": True,
"fa_icon": "whatsapp",
"enabled": True,
},
{
"name": "Telegram",
"base_url": "https://t.me/share/url",
"link_param": "url",
"text_param": "text",
"fa_icon": "telegram",
"enabled": True,
},

]

CORS_ALLOW_ALL_ORIGINS = True

Expand Down
3 changes: 2 additions & 1 deletion nmhs_cms/static/css/cms_style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1048,10 +1048,11 @@ img#work-map {

.share-buttons {
display: flex;
overflow: hidden;
flex-wrap: wrap;
}

.share-buttons > a {
margin-bottom: 10px;
margin-right: 20px;
display: flex;
justify-content: center;
Expand Down
4 changes: 4 additions & 0 deletions nmhs_cms/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
{% if settings.base.IntegrationSettings.google_site_verification_key %}
<meta name="google-site-verification"
content="{{ settings.base.IntegrationSettings.google_site_verification_key }}"/>
{% endif %}
{% block page_meta %}
{% if page.get_meta_title %}
{% meta_tags %}
Expand Down
4 changes: 2 additions & 2 deletions nmhs_cms/templates/document_detail_include.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load static wagtailcore_tags wagtailimages_tags lazyimages_tags nmhs_cms_tags i18n %}
{% load static wagtailcore_tags wagtailimages_tags lazyimages_tags nmhs_cms_tags i18n get_share_url %}

<div class="columns is-multiline is-mobile document-item-detail-wrapper">
<div class="column is-3-desktop is-offset-1-desktop is-10-mobile document-detail-left box">
Expand Down Expand Up @@ -149,7 +149,7 @@ <h4 class="document-detail-additional-header">
</span>
</a>
{% endif %}
{% include 'social_media_share_buttons_include.html' with url=page.url text=page.title %}
{% share_buttons url=page.get_full_url text=page.title %}
</div>
</div>
</div>
5 changes: 1 addition & 4 deletions nmhs_cms/templates/featured_item_include.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load wagtailcore_tags wagtailimages_tags lazyimages_tags get_share_url nmhs_cms_tags i18n %}
{% load wagtailcore_tags wagtailimages_tags lazyimages_tags nmhs_cms_tags i18n %}

<div class="columns is-multiline featured-item-block">
<div class="column is-5-widescreen is-full-mobile featured-item-image">
Expand Down Expand Up @@ -27,9 +27,6 @@
<span class="featured-item-header-tag">
{% translate "Featured" %}
</span>
<div class="share-buttons">
{% include 'social_media_share_buttons_include.html' with url=item_url text=item_title %}
</div>
</div>
<div class="featured-item-body">
<h3 class="featured-item-title">
Expand Down
41 changes: 16 additions & 25 deletions nmhs_cms/templates/social_media_share_buttons_include.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
{% load get_share_url %}
{% for share_url in share_urls %}

<a class="button is-small is-rounded"
href="{% get_social_media_share 'facebook' page.get_site.root_url|add:url text %}"
target="_blank" rel="noopener"
data-ga-event-category="Social Media Share"
data-ga-event-label="Facebook"
data-ga-value="{{ text }}"
>
<span class="icon">
<i class="fab fa-facebook-f" aria-hidden="true"></i>
</span>
<span class="share-button-title">Facebook</span>
</a>
<a class="button is-small is-rounded"
href="{% get_social_media_share 'twitter' page.get_site.root_url|add:url text %}"
target="_blank" rel="noopener"
data-ga-event-category="Social Media Share"
data-ga-event-label="Twitter"
data-ga-value="{{ text }}"
>
<span class="icon">
<i class="fab fa-twitter" aria-hidden="true"></i>
</span>
<span class="share-button-title">Twitter</span>
</a>
<a class="button is-small is-rounded"
href="{{ share_url.url }}"
target="_blank" rel="noopener"
data-ga-event-category="Social Media Share"
data-ga-event-label="{{ share_url.name }}"
data-ga-value="{{ share_url.url }}"
>
<span class="icon">
<i class="fab fa-{{ share_url.fa_icon }}" aria-hidden="true"></i>
</span>
<span class="share-button-title">{{ share_url.name }}</span>
</a>


{% endfor %}
Loading

0 comments on commit e645674

Please sign in to comment.