Skip to content

Commit

Permalink
remove twitter endpoint (#51)
Browse files Browse the repository at this point in the history
* how do you like this, Elon Musk?

* blacked

* Unused imports remove

* Unused import

* Add upgrade step

* Update profile version

* Blacked

* FIx

* black

---------

Co-authored-by: Mauro Amico <[email protected]>
  • Loading branch information
folix-01 and mamico authored Jun 28, 2023
1 parent 70ed8e4 commit a459c25
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 260 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
5.0.4 (unreleased)
------------------

- Nothing changed yet.
- Remove twitter feeds.
[folix-01]


5.0.3 (2023-06-13)
Expand Down
45 changes: 0 additions & 45 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,51 +164,6 @@ La struttura degli attachments è la seguente::

Se l'invio va a buon fine, viene tornata una risposta con `204`.

@twitter-feed
-------------

Endpoint per poter visualizzare una serie di tweet.

Per poterla utilizzare, bisogna creare un'app su Twitter e impostare il token Bearer dentro al registry Plone nella entry *design.plone.policy.twitter_token*.

Per fare la ricerca, utilizza l'endpoint `recent`_ che permette di visualizzare solo i tweet dell'ultima settimana.

.. _recent: https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction

Come parametri accetta i seguenti:

- **authors**: una lista di username tra cui ricercare gli ultimi Tweet.
- **max_results**: un numero tra 10 e 100.

Esempio di chiamata::

> curl -i -X GET http://localhost:8080/Plone/@twitter-feed?authors=foo&authors=bar -H 'Accept: application/json' -H 'Content-Type: application/json'

La risposta è una lista di tweet con le informazioni necessarie per essere renderizzati::

[
{
"author": {
"id": "12345678",
"name": "John Doe",
"profile_image_url": "https://pbs.twimg.com/profile_images/xxx/xxx_normal.jpg",
"username": "jdoe"
},
"id": "xxxxx",
"like_count": 1,
"reply_count": 0,
"retweet_count": 0,
"text": "stringa html"
},
...
]

Il campo `text` contiene già eventuali link ad hashtag, menzioni e link esterni dentro ad un tag <a/>.

**Per evitare troppe chiamate al servizio (c'è un limite di 500000 tweet al mese), c'è della cache: per ogni query
i risultati rimangono in cache per mezz'ora.**


Amministrazione trasparente
===========================

Expand Down
3 changes: 1 addition & 2 deletions src/design/plone/policy/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
"""Module where all interfaces, events and exceptions live."""

from zope import schema
from zope.interface import Interface
from zope.publisher.interfaces.browser import IDefaultBrowserLayer

Expand All @@ -11,4 +10,4 @@ class IDesignPlonePolicyLayer(IDefaultBrowserLayer):


class IDesignPlonePolicySettings(Interface):
twitter_token = schema.TextLine(title="Twitter Bearer token")
"""IDesignPlonePolicySettings interface"""
2 changes: 1 addition & 1 deletion src/design/plone/policy/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<version>3100</version>
<version>3101</version>
<dependencies>
<dependency>profile-plone.restapi:default</dependency>
<dependency>profile-design.plone.contenttypes:default</dependency>
Expand Down
9 changes: 0 additions & 9 deletions src/design/plone/policy/profiles/to_1400/registry.xml

This file was deleted.

1 change: 0 additions & 1 deletion src/design/plone/policy/restapi/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

<include package=".bandi_search_filters" />
<include package=".search_filters" />
<include package=".twitter_feed" />

</configure>
Empty file.
21 changes: 0 additions & 21 deletions src/design/plone/policy/restapi/twitter_feed/configure.zcml

This file was deleted.

179 changes: 0 additions & 179 deletions src/design/plone/policy/restapi/twitter_feed/get.py

This file was deleted.

33 changes: 32 additions & 1 deletion src/design/plone/policy/upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from Acquisition import aq_base
from collective.volto.blocksfield.field import BlocksField
from copy import deepcopy
from design.plone.policy.interfaces import IDesignPlonePolicySettings
from design.plone.policy.setuphandlers import disable_searchable_types
from design.plone.policy.setuphandlers import set_default_subsite_colors
from design.plone.policy.utils import create_default_blocks
from design.plone.policy.interfaces import IDesignPlonePolicySettings
from plone import api
from plone.app.upgrade.utils import installOrReinstallProduct
from plone.dexterity.utils import iterSchemata
Expand All @@ -16,6 +16,7 @@
from zope.component import getUtility
from zope.schema import getFields


import json
import logging

Expand Down Expand Up @@ -364,3 +365,33 @@ def to_3100(context):
logger.info("### Items that were not modified ###")
for i, path in enumerate(not_modified):
logger.info(f"[{i+1}/{len(not_modified)}] - {path}")


def to_3101(context):
def remove_twitter(blocks_orig):
blocks = deepcopy(blocks_orig)
for key, block in blocks.items():
if block.get("@type", "") == "twitter_posts":
del blocks[key]

return blocks

for brain in api.portal.get_tool("portal_catalog")():
item = aq_base(brain.getObject())
for schema in iterSchemata(item):
for name, field in getFields(schema).items():
if name == "blocks":
item.blocks = remove_twitter(item.blocks)

elif isinstance(field, BlocksField):
value = deepcopy(field.get(item))
if not value:
continue
try:
blocks = value.get("blocks", {})
except AttributeError:
logger.warning(
"[BLOCK] - {} (not converted)".format(brain.getURL())
)
if blocks:
item.blocks = remove_twitter(blocks)
10 changes: 10 additions & 0 deletions src/design/plone/policy/upgrades.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,14 @@
handler=".upgrades.to_3100"
/>
</genericsetup:upgradeSteps>
<genericsetup:upgradeSteps
profile="design.plone.policy:default"
source="3100"
destination="3101"
>
<genericsetup:upgradeStep
title="Remove twitter blocks"
handler=".upgrades.to_3101"
/>
</genericsetup:upgradeSteps>
</configure>

0 comments on commit a459c25

Please sign in to comment.