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