Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxMaSk committed Nov 19, 2017
1 parent a6a25cd commit c1ef814
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 186 deletions.
18 changes: 18 additions & 0 deletions django_th/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,21 @@ def download_image(url):
if chunk:
f.write(chunk)
return local_filename


def get_tags(model, trigger_id):
"""
get the tags if any
:param trigger_id: the id of the related trigger
:return: tags string
"""
# get the data of this trigger
trigger = model.objects.get(trigger_id=trigger_id)

tags = ''
if len(trigger.tag) > 0:
# is there several tag ?
tags = ["#" + tag.strip() for tag in trigger.tag.split(',')] if ',' in trigger.tag else "#" + trigger.tag
tags = str(','.join(tags)) if isinstance(tags, list) else tags
tags = ' ' + tags
return tags
19 changes: 5 additions & 14 deletions th_evernote/evernote_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from evernote.edam.notestore import NoteStore

from logging import getLogger

from th_evernote.evernote_exception import error

logger = getLogger('django_th.trigger_happy')
cache = caches['django_th']

Expand Down Expand Up @@ -118,25 +121,13 @@ def create_note(note_store, note, trigger_id, data):
logger.debug(sentence)
return True
except EDAMSystemException as e:
if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED:
sentence = "Rate limit reached {code} " \
"Retry your request in {msg} seconds".format(
code=e.errorCode, msg=e.rateLimitDuration)
logger.warn(sentence)
# put again in cache the data that could not be
# published in Evernote yet
cache.set('th_evernote_' + str(trigger_id), data, version=2)
update_result(trigger_id, msg=sentence, status=True)
return True
else:
logger.critical(e)
return False
return error(trigger_id, data, e)
except EDAMUserException as e:
if e.errorCode == EDAMErrorCode.ENML_VALIDATION:
sentence = "Data ignored due to validation" \
" error : err {code} {msg}".format(
code=e.errorCode, msg=e.parameter)
logger.warn(sentence)
logger.warning(sentence)
update_result(trigger_id, msg=sentence, status=True)
return True
except Exception as e:
Expand Down
20 changes: 2 additions & 18 deletions th_evernote/my_evernote.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from evernote.api.client import EvernoteClient
import evernote.edam.type.ttypes as Types
from evernote.edam.error.ttypes import EDAMSystemException, EDAMUserException
from evernote.edam.error.ttypes import EDAMErrorCode

from logging import getLogger

from th_evernote.evernote_exception import error
from th_evernote.evernote_mgr import EvernoteMgr
from th_evernote.models import Evernote
from th_evernote.sanitize import sanitize
Expand Down Expand Up @@ -193,23 +193,7 @@ def _notestore(self, trigger_id, data):
note_store = self.client.get_note_store()
return note_store
except EDAMSystemException as e:
# rate limit reach have to wait 1 hour !
if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED:
sentence = "Rate limit reached {code}\n" \
"Retry your request in {msg} seconds\n" \
"Data set to cache again until" \
" limit reached".format(code=e.errorCode,
msg=e.rateLimitDuration)
logger.warning(sentence)
cache.set('th_evernote_' + str(trigger_id),
data,
version=2)
update_result(trigger_id, msg=sentence, status=True)
return True
else:
logger.critical(e)
update_result(trigger_id, msg=e, status=False)
return False
return error(trigger_id, data, e)
except Exception as e:
logger.critical(e)
update_result(trigger_id, msg=e, status=False)
Expand Down
21 changes: 20 additions & 1 deletion th_mastodon/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# coding: utf-8

from django import forms
from django.core.exceptions import ValidationError
from django.forms import TextInput
from django.utils.translation import ugettext as _
from th_mastodon.models import Mastodon


Expand All @@ -14,6 +16,23 @@ class Meta:
model = Mastodon
fields = ['timeline', 'tooter', 'tag', 'fav']

widgets = {
'tag': TextInput(attrs={'class': 'form-control'}),
'tooter': TextInput(attrs={'class': 'form-control'}),
}

def clean(self):
"""
validate if tag or screen is filled
:return:
"""
cleaned_data = super(MastodonForm, self).clean()
tag = cleaned_data.get("tag")
screen = cleaned_data.get("tooter")
# check if one of the field is filled when a field is empty the clean() function set it as None
if tag is None and screen is None:
raise ValidationError(_("You have to fill ONE of the fields (or tag + tooter or tooter + fav)"))


class MastodonProviderForm(MastodonForm):
pass
Expand Down
57 changes: 16 additions & 41 deletions th_mastodon/my_mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# django_th classes
from django_th.models import update_result, UserService
from django_th.services.services import ServicesMgr
from django_th.tools import download_image
from django_th.tools import download_image, get_tags

from logging import getLogger

Expand Down Expand Up @@ -91,12 +91,10 @@ def _get_toots(toot_api, toot_obj, search):
return search, statuses

if self.token is not None:
kw = {'app_label': 'th_mastodon', 'model_name': 'Mastodon',
'trigger_id': trigger_id}
kw = {'app_label': 'th_mastodon', 'model_name': 'Mastodon', 'trigger_id': trigger_id}
toot_obj = super(ServiceMastodon, self).read_data(**kw)

us = UserService.objects.get(token=self.token,
name='ServiceMastodon')
us = UserService.objects.get(token=self.token, name='ServiceMastodon')
try:
toot_api = MastodonAPI(
client_id=us.client_id,
Expand Down Expand Up @@ -159,31 +157,30 @@ def _get_toots(toot_api, toot_obj, search):

def save_data(self, trigger_id, **data):
"""
get the data from the service
:param trigger_id: id of the trigger
:params data, dict
:rtype: dict
let's save the data
:param trigger_id: trigger ID from which to save data
:param data: the data to check to be used and save
:type trigger_id: int
:type data: dict
:return: the status of the save statement
:rtype: boolean
"""
title, content = super(ServiceMastodon, self).save_data(
trigger_id, **data)
title, content = super(ServiceMastodon, self).save_data(trigger_id, **data)

# check if we have a 'good' title
if self.title_or_content(title):

content = str("{title} {link}").format(title=title,
link=data.get('link'))
content += self.get_tags(trigger_id)
content = str("{title} {link}").format(title=title, link=data.get('link'))
content += get_tags(Mastodon, trigger_id)
# if not then use the content
else:
content += " " + data.get('link')
content += " " + self.get_tags(trigger_id)
content += " " + get_tags(Mastodon, trigger_id)

content = self.set_mastodon_content(content)

us = UserService.objects.get(user=self.user,
token=self.token,
name='ServiceMastodon')
us = UserService.objects.get(user=self.user, token=self.token, name='ServiceMastodon')

try:
toot_api = MastodonAPI(
Expand Down Expand Up @@ -216,28 +213,6 @@ def save_data(self, trigger_id, **data):
update_result(trigger_id, msg=inst, status=status)
return status

def get_tags(self, trigger_id):
"""
get the tags if any
:param trigger_id: the id of the related trigger
:return: tags string
"""

# get the Mastodon data of this trigger
trigger = Mastodon.objects.get(trigger_id=trigger_id)

tags = ''

if trigger.tag is not None:
# is there several tag ?
tags = ["#" + tag.strip() for tag in trigger.tag.split(',')
] if ',' in trigger.tag else "#" + trigger.tag

tags = str(','.join(tags)) if isinstance(tags, list) else tags
tags = ' ' + tags

return tags

def title_or_content(self, title):
"""
If the title always contains 'Tweet from'
Expand Down
24 changes: 5 additions & 19 deletions th_mastodon/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,31 @@ def create_masto(self, tooter='foxmask@mamot.fr', timeline='home',
def test_mastodon(self):
m = self.create_masto()
self.assertTrue(isinstance(m, Mastodon))
self.assertEqual(m.show(), "My Mastodon %s %s" %
(m.timeline, m.trigger))
self.assertEqual(m.show(), "My Mastodon %s %s" % (m.timeline, m.trigger))
self.assertEqual(m.__str__(), "{}".format(m.timeline))

"""
Form
"""
# provider

def test_valid_provider_form(self):
m = self.create_masto()
data = {'tooter': m.tooter,
'timeline': m.timeline,
'tag': m.tag,
'fav': m.fav}
form = MastodonProviderForm(data=data)
form = MastodonProviderForm(data={'tooter': m.tooter, 'timeline': m.timeline, 'tag': m.tag, 'fav': m.fav})
self.assertTrue(form.is_valid())

def test_invalid_provider_form(self):
form = MastodonProviderForm(data={'tooter': '',
'timeline': '',
'tag': '', 'fav': ''})
form = MastodonProviderForm(data={'tooter': '', 'timeline': '', 'tag': '', 'fav': ''})
self.assertFalse(form.is_valid())

# consumer
def test_valid_consumer_form(self):
m = self.create_masto()
data = {'tooter': m.tooter,
'timeline': m.timeline,
'tag': m.tag,
'fav': m.fav}
form = MastodonConsumerForm(data=data)
form = MastodonConsumerForm(data={'tooter': m.tooter, 'timeline': m.timeline, 'tag': m.tag, 'fav': m.fav})
self.assertTrue(form.is_valid())

def test_invalid_consumer_form(self):
# when a field is empty the clean() function set it as None
form = MastodonConsumerForm(data={'tooter': '',
'timeline': '',
'tag': '', 'fav': False})
form = MastodonConsumerForm(data={'tooter': '', 'timeline': '', 'tag': '', 'fav': False})
self.assertFalse(form.is_valid())


Expand Down
2 changes: 0 additions & 2 deletions th_pelican/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
VERSION = (0, 0, 1) # PEP 386
__version__ = ".".join([str(x) for x in VERSION])
2 changes: 0 additions & 2 deletions th_pocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
VERSION = (0, 4, 0) # PEP 386
__version__ = ".".join([str(x) for x in VERSION])
2 changes: 0 additions & 2 deletions th_rss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
VERSION = (0, 4, 0) # PEP 386
__version__ = ".".join([str(x) for x in VERSION])
30 changes: 2 additions & 28 deletions th_slack/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,11 @@
from rest_framework.response import Response
from rest_framework.decorators import api_view

from django_th.models import TriggerService
from django_th.services import default_provider
from django_th.api.consumer import save_data

from th_slack.models import Slack


def consumer(trigger_id, data):
"""
call the consumer and handle the data
:param trigger_id:
:param data:
:return:
"""
status = True
# consumer - the service which uses the data
default_provider.load_services()
service = TriggerService.objects.get(id=trigger_id)

service_consumer = default_provider.get_service(
str(service.consumer.name.name))
kwargs = {'user': service.user}

if len(data) > 0:

getattr(service_consumer, '__init__')(service.consumer.token,
**kwargs)
status = getattr(service_consumer, 'save_data')(service.id, **data)

return status


@api_view(['POST'])
def slack(request):
data = {}
Expand All @@ -42,7 +16,7 @@ def slack(request):
if slack:
data['title'] = 'From Slack #{}'.format(request.data['channel_name'])
data['content'] = request.data['text']
status = consumer(slack.trigger_id, data)
status = save_data(slack.trigger_id, data)
if status:
return Response({"message": "Success"})
else:
Expand Down
8 changes: 3 additions & 5 deletions th_taiga/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.response import Response
from rest_framework.decorators import api_view

from django_th.api.consumer import save_data
from django_th.models import TriggerService
from django_th.services import default_provider

Expand Down Expand Up @@ -72,7 +73,6 @@ def create(self, taiga_obj, data):
"""
:param taiga_obj: taiga object
:param action: craete/change/delete
:param data: data to return
:return: data
"""
Expand All @@ -84,7 +84,6 @@ def change(self, taiga_obj, data):
"""
:param taiga_obj: taiga object
:param action: craete/change/delete
:param data: data to return
:return: data
"""
Expand All @@ -96,7 +95,6 @@ def delete(self, taiga_obj, data):
"""
:param taiga_obj: taiga object
:param action: craete/change/delete
:param data: data to return
:return: data
"""
Expand Down Expand Up @@ -277,7 +275,6 @@ def consumer(trigger_id, data):
str(service.consumer.name.name))
kwargs = {'user': service.user}

data = data_filter(trigger_id, **data)
if len(data) > 0:

getattr(service_consumer, '__init__')(service.consumer.token,
Expand All @@ -297,7 +294,8 @@ def taiga(request, trigger_id, key):
signature = request.META.get('HTTP_X_TAIGA_WEBHOOK_SIGNATURE')
# check that the data are ok with the provided signature
if verify_signature(request._request.body, key, signature):
status = consumer(trigger_id, request.data)
data = data_filter(trigger_id, **request.data)
status = save_data(trigger_id, data)
if status:
return Response({"message": "Success"})
else:
Expand Down
2 changes: 0 additions & 2 deletions th_trello/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
VERSION = (0, 4, 0) # PEP 386
__version__ = ".".join([str(x) for x in VERSION])
2 changes: 0 additions & 2 deletions th_tumblr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
VERSION = (0, 0, 1) # PEP 386
__version__ = ".".join([str(x) for x in VERSION])

0 comments on commit c1ef814

Please sign in to comment.