1
|
|
|
# coding: utf-8 |
2
|
|
|
import arrow |
3
|
|
|
import datetime |
4
|
|
|
|
5
|
|
|
from django.conf import settings |
6
|
|
|
from django.core.mail import send_mail, mail_admins |
7
|
|
|
from django.utils import html |
8
|
|
|
|
9
|
|
|
import importlib |
10
|
|
|
import time |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
""" |
14
|
|
|
Simple utility functions |
15
|
|
|
""" |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def class_for_name(module_name, class_name): |
19
|
|
|
""" |
20
|
|
|
Import a class dynamically |
21
|
|
|
:param module_name: the name of a module |
22
|
|
|
:param class_name: the name of a class |
23
|
|
|
:type module_name: string |
24
|
|
|
:type class_name: string |
25
|
|
|
:return: Return the value of the named attribute of object. |
26
|
|
|
:rtype: object |
27
|
|
|
""" |
28
|
|
|
# load the module, will raise ImportError if module cannot be loaded |
29
|
|
|
m = importlib.import_module(module_name) |
30
|
|
|
# get the class, will raise AttributeError if class cannot be found |
31
|
|
|
c = getattr(m, class_name) |
32
|
|
|
return c |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def get_service(service, model_form='models', form_name=''): |
36
|
|
|
""" |
37
|
|
|
get the service name then load the model |
38
|
|
|
:param service: the service name |
39
|
|
|
:param model_form: could be 'models' or 'forms' |
40
|
|
|
:param form_name: the name of the form is model_form is 'forms' |
41
|
|
|
:type service: string |
42
|
|
|
:type model_form: string |
43
|
|
|
:type form_name: string |
44
|
|
|
:return: the object of the spotted Class. |
45
|
|
|
:rtype: object |
46
|
|
|
|
47
|
|
|
:Example: |
48
|
|
|
|
49
|
|
|
class_name could be : |
50
|
|
|
th_rss.models |
51
|
|
|
th_rss.forms |
52
|
|
|
service_name could be : |
53
|
|
|
ServiceRss |
54
|
|
|
then could call : |
55
|
|
|
Rss+ProviderForm |
56
|
|
|
Evernote+ConsumerForm |
57
|
|
|
""" |
58
|
|
|
service_name = str(service).split('Service')[1] |
59
|
|
|
|
60
|
|
|
class_name = 'th_' + service_name.lower() + '.' + model_form |
61
|
|
|
|
62
|
|
|
if model_form == 'forms': |
63
|
|
|
return class_for_name(class_name, service_name + form_name) |
64
|
|
|
else: |
65
|
|
|
return class_for_name(class_name, service_name) |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def to_datetime(data): |
69
|
|
|
""" |
70
|
|
|
convert Datetime 9-tuple to the date and time format |
71
|
|
|
feedparser provides this 9-tuple |
72
|
|
|
:param data: data to be checked |
73
|
|
|
:type data: dict |
74
|
|
|
""" |
75
|
|
|
my_date_time = None |
76
|
|
|
|
77
|
|
|
if 'published_parsed' in data: |
78
|
|
|
my_date_time = datetime.datetime.utcfromtimestamp(time.mktime(data.get('published_parsed'))) |
79
|
|
|
elif 'created_parsed' in data: |
80
|
|
|
my_date_time = datetime.datetime.utcfromtimestamp(time.mktime(data.get('created_parsed'))) |
81
|
|
|
elif 'updated_parsed' in data: |
82
|
|
|
my_date_time = datetime.datetime.utcfromtimestamp(time.mktime(data.get('updated_parsed'))) |
83
|
|
|
elif 'my_date' in data: |
84
|
|
|
my_date_time = arrow.get(data['my_date']) |
85
|
|
|
|
86
|
|
|
return my_date_time |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def warn_user_and_admin(consumer_provider, service): |
90
|
|
|
""" |
91
|
|
|
|
92
|
|
|
:param consumer_provider: the consumer or provider |
93
|
|
|
:param service: the service |
94
|
|
|
:return: |
95
|
|
|
""" |
96
|
|
|
|
97
|
|
|
from_mail = settings.DEFAULT_FROM_EMAIL |
98
|
|
|
|
99
|
|
|
if consumer_provider == 'provider': |
100
|
|
|
service_name = service.provider.name.name.split('Service')[1] |
101
|
|
|
else: |
102
|
|
|
service_name = service.consumer.name.name.split('Service')[1] |
103
|
|
|
|
104
|
|
|
title = 'Trigger "{}" disabled'.format(service.description) |
105
|
|
|
|
106
|
|
|
body = 'The trigger "{}" has been disabled due to an issue with "{}". ' \ |
107
|
|
|
'Try to renew it to refresh the token to try to fix the issue'. \ |
108
|
|
|
format(service.description, service_name) |
109
|
|
|
# for enduser |
110
|
|
|
send_mail(title, body, from_mail, [service.user.email], fail_silently=False) |
111
|
|
|
# for admins |
112
|
|
|
body = 'The trigger "{}" has been disabled due to an issue with "{}". ' \ |
113
|
|
|
'User {}\'s trigger'.format(service.description, service_name, service.user.email) |
114
|
|
|
mail_admins(title, body, fail_silently=False) |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
def download_image(url): |
118
|
|
|
""" |
119
|
|
|
|
120
|
|
|
:param url: url of the image to download |
121
|
|
|
:return: local_filename the name of the file in the cache |
122
|
|
|
""" |
123
|
|
|
import requests |
124
|
|
|
import os |
125
|
|
|
cache_dir = os.path.dirname(__file__) + '/cache/' |
126
|
|
|
local_filename = os.path.basename(url) |
127
|
|
|
local_filename = cache_dir + local_filename |
128
|
|
|
r = requests.get(url, stream=True) |
129
|
|
|
with open(local_filename, 'wb') as f: |
130
|
|
|
for chunk in r.iter_content(chunk_size=1024): |
131
|
|
|
if chunk: |
132
|
|
|
f.write(chunk) |
133
|
|
|
return local_filename |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
def get_tags(model, trigger_id): |
137
|
|
|
""" |
138
|
|
|
get the tags if any |
139
|
|
|
:param model: the model object to request |
140
|
|
|
:param trigger_id: the id of the related trigger |
141
|
|
|
:return: tags string |
142
|
|
|
""" |
143
|
|
|
# get the data of this trigger |
144
|
|
|
trigger = model.objects.get(trigger_id=trigger_id) |
145
|
|
|
|
146
|
|
|
tags = '' |
147
|
|
|
if trigger.tag: |
148
|
|
|
# is there several tag ? |
149
|
|
|
tags = ["#" + tag.strip() for tag in trigger.tag.split(',')] |
150
|
|
|
tags = str(','.join(tags)) if isinstance(tags, list) else tags |
151
|
|
|
tags = ' ' + tags |
152
|
|
|
return tags |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
def limit_content(content, limit): |
156
|
|
|
""" |
157
|
|
|
|
158
|
|
|
:param content: the content |
159
|
|
|
:param limit: limit of the content |
160
|
|
|
:return: content resized or not |
161
|
|
|
""" |
162
|
|
|
content = html.strip_tags(content) |
163
|
|
|
|
164
|
|
|
return content[:limit] if len(content) > limit else content |
165
|
|
|
|