From ed8363f163291cf96689f259c0fc86bfc337595b Mon Sep 17 00:00:00 2001 From: Daniel Grams Date: Thu, 27 May 2021 14:14:38 +0200 Subject: [PATCH] Add meta tags for social media #181 --- project/services/event.py | 37 +++++++++++++++++++ project/templates/admin/admin.html | 4 +- project/templates/admin/admin_units.html | 4 +- project/templates/admin/settings.html | 4 +- .../templates/admin/update_admin_unit.html | 4 +- project/templates/admin/update_user.html | 4 +- project/templates/admin/users.html | 4 +- project/templates/developer/read.html | 4 +- project/templates/event/actions.html | 4 +- project/templates/event/create.html | 4 +- project/templates/event/read.html | 4 +- project/templates/event/reference.html | 4 +- .../templates/event/reference_request.html | 4 +- project/templates/event/update.html | 4 +- project/templates/event_date/list.html | 4 +- project/templates/event_date/read.html | 6 +-- project/templates/event_date/search.html | 4 +- project/templates/event_place/list.html | 4 +- project/templates/event_place/read.html | 4 +- .../templates/event_suggestion/reject.html | 4 +- .../templates/event_suggestion/review.html | 4 +- .../event_suggestion/review_status.html | 4 +- project/templates/example.html | 4 +- project/templates/home.html | 4 +- project/templates/invitation/read.html | 4 +- project/templates/layout.html | 30 ++++++++++++--- project/templates/legal.html | 4 +- project/templates/manage/admin_units.html | 4 +- project/templates/manage/events.html | 4 +- project/templates/manage/members.html | 4 +- project/templates/manage/organizers.html | 4 +- project/templates/manage/places.html | 4 +- .../manage/reference_requests_incoming.html | 4 +- .../manage/reference_requests_outgoing.html | 4 +- .../templates/manage/references_incoming.html | 4 +- .../templates/manage/references_outgoing.html | 4 +- project/templates/manage/reviews.html | 4 +- project/templates/manage/widgets.html | 4 +- project/templates/oauth2_client/create.html | 4 +- project/templates/oauth2_client/list.html | 4 +- project/templates/oauth2_client/read.html | 4 +- project/templates/oauth2_client/update.html | 4 +- project/templates/oauth2_token/list.html | 4 +- project/templates/organization/read.html | 4 +- project/templates/planing/list.html | 4 +- project/templates/profile.html | 4 +- project/templates/reference/read.html | 4 +- project/templates/reference/update.html | 4 +- .../templates/reference_request/review.html | 4 +- .../reference_request/review_status.html | 4 +- project/templates/widget/event_date/list.html | 4 +- project/templates/widget/event_date/read.html | 4 +- .../widget/event_suggestion/create.html | 4 +- project/templates/widget/infoscreen/read.html | 4 +- project/views/event.py | 2 + project/views/event_date.py | 6 ++- tests/services/test_event.py | 22 +++++++++++ 57 files changed, 195 insertions(+), 112 deletions(-) diff --git a/project/services/event.py b/project/services/event.py index cb4c7a7..6b97205 100644 --- a/project/services/event.py +++ b/project/services/event.py @@ -1,6 +1,8 @@ from datetime import datetime from dateutil.relativedelta import relativedelta +from flask import url_for +from flask_babelex import format_date, format_time from sqlalchemy import and_, func, or_ from sqlalchemy.orm import contains_eager, defaultload, joinedload, lazyload from sqlalchemy.sql import extract @@ -10,6 +12,7 @@ from project.dateutils import date_add_time, dates_from_recurrence_rule, get_tod from project.models import ( AdminUnit, Event, + EventAttendanceMode, EventCategory, EventDate, EventOrganizer, @@ -20,6 +23,7 @@ from project.models import ( Location, ) from project.utils import get_pending_changes +from project.views.utils import truncate def get_event_category(category_name): @@ -313,3 +317,36 @@ def get_significant_event_changes(event) -> dict: "organizer_id", ] return get_pending_changes(event, include_collections=False, include_keys=keys) + + +def get_meta_data(event: Event, event_date: EventDate = None) -> dict: + meta = dict() + meta["title"] = event.name + + if ( + event.attendance_mode + and event.attendance_mode != EventAttendanceMode.online + and event.event_place + ): + meta["title"] = f"{meta['title']} @ {event.event_place.name}" + + if event.event_place.location and event.event_place.location.city: + meta["title"] = f"{meta['title']}, {event.event_place.location.city}" + + if event_date: + date_str = format_date(event_date.start, "full") + time_str = format_time(event_date.start, "short") + meta["description"] = f"{date_str} {time_str}" + + if event.description: + desc_short = truncate(event.description, 300) + + if "description" in meta: + meta["description"] = f"{meta['description']}: {desc_short}" + else: + meta["description"] = desc_short + + if event.photo_id: + meta["image"] = url_for("image", id=event.photo_id, _external=True) + + return meta diff --git a/project/templates/admin/admin.html b/project/templates/admin/admin.html index 0c3b183..8e096a5 100644 --- a/project/templates/admin/admin.html +++ b/project/templates/admin/admin.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} -{% block title %} +{%- block title -%} {{ _('Admin') }} -{% endblock %} +{%- endblock -%} {% block content %}