Skip to content

Commit

Permalink
Merge pull request #67 from ctreffe/develop
Browse files Browse the repository at this point in the history
Release v0.8.10
  • Loading branch information
jobrachem authored Jun 9, 2021
2 parents 7789f6b + 0e8649c commit 77e1361
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Mortimer v0.8.10 (Released 2021-06-09)

### Added

- Added beta support for social media previews

## Mortimer v0.8.8 (Released 2021-06-03)

### Added v0.8.8
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"static/css/*",
"static/img/*",
"static/js/*",
"static/json/*",
"templates/*",
"templates/elements/*",
"templates/errors/*",
Expand Down
2 changes: 1 addition & 1 deletion src/mortimer/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# 2) we can import it in setup.py for the same reason
# 3) we can import it into your module module

__version__ = "0.8.8"
__version__ = "0.8.10"
Empty file added src/mortimer/static/__init__.py
Empty file.
Binary file added src/mortimer/static/alfred3_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/mortimer/static/alfred3_study.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
14 changes: 14 additions & 0 deletions src/mortimer/static/json/social_media_user_agents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
"facebookexternalhit/1.1",
"facebookexternalhit",
"facebook",
"facebot",
"Twitterbot",
"WhatsApp/2.19.81 A",
"WhatsApp",
"TelegramBot",
"Slack",
"Skype",
"SkypeUriPreview",
"Instagram"
]
Binary file added src/mortimer/static/unigoe_logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/mortimer/static/unigoe_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/mortimer/static/unigoe_study.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/mortimer/templates/exp_preview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

{# <meta property="og:url" content="" /> #}
<meta property="og:type" content="article" />
<meta property="og:title" content="{{ title }}" />
<meta property="og:description" content="{{ desc }}" />
<meta property="og:image" content="{{ url_for('static', filename=logo, _external=True) }}" />
<meta property="og:image" content="{{ url_for('static', filename=logo_small, _external=True) }}" />

</head>

</html>
44 changes: 43 additions & 1 deletion src/mortimer/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-

import copy
import json, os, re, subprocess
import importlib.resources as res
import json
from datetime import datetime
from uuid import uuid4
from typing import Iterator
Expand All @@ -11,9 +14,9 @@
from flask_mail import Message
from jinja2 import Template
import pymongo
import copy

from mortimer import mail
from .static import json as jdat

def get_plugin_data_queries(exp) -> Iterator[dict]:
db = get_user_collection()
Expand Down Expand Up @@ -444,3 +447,42 @@ class _DictObj(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__



def get_social_media_user_agents():
d = res.read_text(jdat, "social_media_user_agents.json")
expected_bots = json.loads(d)
return expected_bots

def is_social_media_preview(user_agent) -> bool:
expected_bots = get_social_media_user_agents()

for bot in expected_bots:
if bot in user_agent:
return True

return False


def render_social_media_preview(config):
# fallbacks
fb = {
"title": "Online-Studie",
"desc": "Jetzt mitmachen und Teil der aktuellen Forschung werden."
}

style = config.get("layout", "style")
title = config.get("layout", "preview_title", fallback=fb["title"])
desc = config.get("layout", "preview_description", fallback=fb["desc"])
logo = config.get("layout", "preview_image")
logo_small = config.get("layout", "preview_image_small")

logos = {"base": "alfred3_study.png", "goe": "unigoe_study.png"}
logos_small = {"base": "alfred3_small.png", "goe": "unigoe_small.png"}

if not logo:
logo = logos.get(style)
logo_small = logos_small.get(style)

return render_template("exp_preview.html", logo=logo, style=style, title=title, desc=desc)
17 changes: 11 additions & 6 deletions src/mortimer/web_experiments/alfredo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from flask_login import current_user

from mortimer.models import WebExperiment, User
from mortimer.utils import create_fernet
from mortimer.utils import create_fernet, is_social_media_preview, render_social_media_preview
from alfred3 import alfredlog
import alfred3.config

Expand Down Expand Up @@ -161,6 +161,15 @@ def start(expid):
# pylint: disable=no-member
experiment = WebExperiment.objects.get_or_404(id=ObjectId(expid))

# create session id
sid = str(uuid4())

config = experiment.parse_exp_config(sid)
secrets = experiment.parse_exp_secrets()

if is_social_media_preview(request.headers.get("User-Agent")):
return render_social_media_preview(config)

if not experiment.public and experiment.password != request.form.get("password", None):
exp_url = url_for("alfredo.start", expid=str(experiment.id))
return (
Expand All @@ -172,20 +181,16 @@ def start(expid):
if not experiment.active:
return render_template("exp_inactive.html")

# create session id
sid = str(uuid4())
session["sid"] = sid
session["page_tokens"] = {}

config = experiment.parse_exp_config(sid)
secrets = experiment.parse_exp_secrets()

# initialize log
log = alfredlog.QueuedLoggingInterface("alfred3", f"exp.{str(experiment.id)}")
log.session_id = sid
log.setLevel(config.get("log", "level").upper())
experiment.prepare_logger()

log.debug("Access from: " + request.headers.get("User-Agent"))

# IMPORT SCRIPT CREATE SESSION
try:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
import mortimer.utils as utils


class TestSocialMediaPreview:

def test_get_expected_bots(self):
bots = utils.get_social_media_user_agents()

assert "facebot" in bots

def test_is_social_media_preview(self):
ua = "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
check = utils.is_social_media_preview(ua)
assert check

0 comments on commit 77e1361

Please sign in to comment.