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 20, 2017
1 parent 9e64117 commit f0ac4b2
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 241 deletions.
68 changes: 35 additions & 33 deletions django_th/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,53 +155,55 @@ def update_result(trigger_id, msg, status):
:param status: status of the handling of the current trigger
:return:
"""
service = TriggerService.objects.get(id=trigger_id)
# if status is True, reset *_failed counter
if status:
service = TriggerService.objects.get(id=trigger_id)

TriggerService.objects.filter(id=trigger_id).update(result=msg, date_result=now(), provider_failed=0,
consumer_failed=0, counter_ok=service.counter_ok + 1)
UserService.objects.filter(user=service.user, name=service.consumer.name).update(
counter_ok=service.counter_ok + 1)
provider_failed = 0
consumer_failed = 0
counter_ok = service.counter_ok + 1
counter_ko = service.counter_ko
# otherwise, add 1 to the consumer_failed
else:
service = TriggerService.objects.get(id=trigger_id)
failed = service.consumer_failed + 1

UserService.objects.filter(user=service.user, name=service.consumer.name).update(
counter_ko=service.counter_ko + 1)
provider_failed = service.provider_failed
consumer_failed = service.consumer_failed + 1
counter_ok = service.counter_ko
counter_ko = service.counter_ko + 1

if failed > settings.DJANGO_TH.get('failed_tries', 5):
TriggerService.objects.filter(id=trigger_id).update(result=msg, date_result=now(), status=False,
counter_ko=service.counter_ko + 1)
else:
TriggerService.objects.filter(id=trigger_id).update(result=msg, date_result=now(), consumer_failed=failed,
counter_ko=service.counter_ko + 1)
status = False if consumer_failed > settings.DJANGO_TH.get('failed_tries', 5) else True

warn_user_and_admin('consumer', service)

TriggerService.objects.filter(id=trigger_id).update(
result=msg,
date_result=now(),
provider_failed=provider_failed,
consumer_failed=consumer_failed,
counter_ok=counter_ok,
counter_ko=counter_ko,
status=status)

UserService.objects.filter(user=service.user, name=service.consumer.name).update(
counter_ok=counter_ok,
counter_ko=counter_ko)


def th_create_user_profile(sender, instance, created, **kwargs):
# create the default service that does not
# need any third party auth
user = instance
if user.last_login is None:
services = ('ServiceRss', 'ServicePelican', )
for service in services:
if any(service in s for s in settings.TH_SERVICES):
try:
sa = ServicesActivated.objects.get(name=service)
UserService.objects.get_or_create(user=user, name=sa)
except ObjectDoesNotExist:
logger.debug("A new user %s has been connected but %s "
"could not be added to his services because "
"the service is present in TH_SERVICES but not"
" activated from the Admin Panel" %
(user, service))


post_save.connect(th_create_user_profile, sender=User,
dispatch_uid="create_user_profile")
for service in settings.SERVICES_NEUTRAL:
try:
sa = ServicesActivated.objects.get(name=service)
UserService.objects.get_or_create(user=user, name=sa)
except ObjectDoesNotExist:
logger.debug("A new user %s has been connected but %s "
"could not be added to his services because "
"the service is present in th_settings.py file but not"
" activated from the Admin Panel" % (user, service))


post_save.connect(th_create_user_profile, sender=User, dispatch_uid="create_user_profile")


@receiver(digest_event)
Expand Down
46 changes: 18 additions & 28 deletions django_th/publishing_limit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# coding: utf-8
from django.conf import settings
from django.core.cache import caches
from django_th.my_services import MyService


class PublishingLimit(object):

"""
this class permits to reduce the quantity of data to be pulibshed
get the limit from settings.DJANGO_TH['publishing_limit']
Expand All @@ -21,31 +19,23 @@ def get_data(service, cache_data, trigger_id):
:return: Return the data from the cache
:rtype: object
"""

# rebuild the string
# th_<service>.my_<service>.Service<Service>
if service.startswith('th_'):
service_long = MyService.full_name(service)
# ... and check it
if service_long in settings.TH_SERVICES:

cache = caches['django_th']

limit = settings.DJANGO_TH.get('publishing_limit', 0)

# publishing of all the data
if limit == 0:
return cache_data
# or just a set of them
if cache_data is not None and len(cache_data) > limit:
for data in cache_data[limit:]:
service_str = ''.join((service, '_', str(trigger_id)))
# put that data in a version 2 of the cache
cache.set(service_str, data, version=2)
# delete data from cache version=1
# https://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
cache.delete_pattern(service_str)
# put in cache unpublished data
cache_data = cache_data[:limit]

cache = caches['django_th']

limit = settings.DJANGO_TH.get('publishing_limit', 0)

# publishing of all the data
if limit == 0:
return cache_data
# or just a set of them
if cache_data is not None and len(cache_data) > limit:
for data in cache_data[limit:]:
service_str = ''.join((service, '_', str(trigger_id)))
# put that data in a version 2 of the cache
cache.set(service_str, data, version=2)
# delete data from cache version=1
# https://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
cache.delete_pattern(service_str)
# put in cache unpublished data
cache_data = cache_data[:limit]
return cache_data
33 changes: 9 additions & 24 deletions django_th/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ def _get_content(data, which_content):
"""
content = ''
if data.get(which_content):
if type(data.get(which_content)) is list or\
type(data.get(which_content)) is tuple or\
type(data.get(which_content)) is dict:
if type(data.get(which_content)) is list or type(data.get(which_content)) is tuple or type(data.get(
which_content)) is dict:
if 'value' in data.get(which_content)[0]:
content = data.get(which_content)[0].value
else:
Expand All @@ -90,9 +89,7 @@ def set_title(data):
:type data: dict
:rtype: string
"""
title = (data.get('title') if data.get('title') else data.get('link'))

return title
return data.get('title') if data.get('title') else data.get('link')

def set_content(self, data):
"""
Expand Down Expand Up @@ -131,8 +128,7 @@ def process_data(self, **kwargs):
:type kwargs: dict
"""
cache = caches['django_th']
cache_data = cache.get(kwargs.get('cache_stack') + '_' +
kwargs.get('trigger_id'))
cache_data = cache.get(kwargs.get('cache_stack') + '_' + kwargs.get('trigger_id'))
return PublishingLimit.get_data(kwargs.get('cache_stack'), cache_data, int(kwargs.get('trigger_id')))

def save_data(self, trigger_id, **data):
Expand Down Expand Up @@ -165,9 +161,7 @@ def auth(self, request):
:type request: dict
:rtype: dict
"""
request_token = self.get_request_token(request)

return request_token
return self.get_request_token(request)

def callback_url(self, request):
"""
Expand Down Expand Up @@ -211,13 +205,8 @@ def callback_oauth1(self, request, **kwargs):
:type kwargs: dict
:rtype: string
"""
if kwargs.get('access_token') == '' \
or kwargs.get('access_token') is None:
access_token = self.get_access_token(
request.session['oauth_token'],
request.session['oauth_token_secret'],
request.GET.get('oauth_verifier', '')
)
if kwargs.get('access_token') == '' or kwargs.get('access_token') is None:
access_token = self.get_access_token(request.session['oauth_token'], request.GET.get('oauth_verifier', ''))
else:
access_token = kwargs.get('access_token')

Expand Down Expand Up @@ -262,8 +251,7 @@ def get_request_token(self, request):
authorization_url, state = oauth.authorization_url(self.AUTH_URL)
return authorization_url

def get_access_token(self, oauth_token, oauth_token_secret,
oauth_verifier):
def get_access_token(self, oauth_token, oauth_token_secret, oauth_verifier):
"""
get the access token
the url to go back after the external service call
Expand Down Expand Up @@ -303,12 +291,9 @@ def send_digest_event(self, trigger_id, title, link=''):
:return:
"""
if settings.DJANGO_TH.get('digest_event'):

t = TriggerService.objects.get(id=trigger_id)

if t.provider.duration != 'n':

kwargs = {'user': t.user, 'title': title,
'link': link, 'duration': t.provider.duration}
kwargs = {'user': t.user, 'title': title, 'link': link, 'duration': t.provider.duration}

signals.digest_event.send(sender=t.provider.name, **kwargs)
13 changes: 13 additions & 0 deletions django_th/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.conf import settings
from django.core.mail import send_mail, mail_admins
from django.utils import html

import importlib
import time
Expand Down Expand Up @@ -142,3 +143,15 @@ def get_tags(model, trigger_id):
tags = str(','.join(tags)) if isinstance(tags, list) else tags
tags = ' ' + tags
return tags


def limit_content(content, limit):
"""
:param content: the content
:param limit: limit of the content
:return: content resized or not
"""
content = html.strip_tags(content)

return content[:limit] if len(content) > limit else content

0 comments on commit f0ac4b2

Please sign in to comment.