1
1
# coding=UTF8
2
2
3
3
from api import app
4
- from flask import request ,abort
4
+ from flask import request , abort , render_template
5
5
from model .usermodel import UserModel
6
6
import md5
7
7
import datetime
@@ -26,7 +26,7 @@ def decorator(*args, **kwargs): #1
26
26
m = md5 .new ()
27
27
m .update (suma )
28
28
hashsum = m .hexdigest ()
29
-
29
+
30
30
if (hashsum != request .headers ['hash' ]):
31
31
abort (401 )
32
32
else :
@@ -38,44 +38,118 @@ def decorator(*args, **kwargs): #1
38
38
return func (* args , ** kwargs )
39
39
return decorator
40
40
41
+ def authAdmin (func ):
42
+ @wraps (func )
43
+ def decorator (* args , ** kwargs ): #1
44
+ if (request .headers ['hash' ] is not None and request .headers ['timestamp' ] is not None and request .headers ['username' ] is not None ):
45
+ # Control timestamp
46
+ usertimestamp = datetime .datetime .fromtimestamp (int (request .headers ['timestamp' ])/ 1000 )
47
+ maxtime = usertimestamp + datetime .timedelta (minutes = app .config ["loginMaxTime" ])
48
+ diftime = datetime .datetime .now () - maxtime
49
+ if (diftime <= datetime .timedelta (seconds = 0 )):
50
+
51
+ # Control credentials
52
+ usermodel = UserModel ()
53
+ user = usermodel .getUserByUsername (request .headers ['username' ])
54
+ if user is not None and user ['admin' ]:
55
+ password = usermodel .getPasswordByUsername (request .headers ['username' ])
56
+
57
+ if (password != '' ):
58
+ suma = request .headers ['username' ] + password + request .headers ['timestamp' ]
59
+ m = md5 .new ()
60
+ m .update (suma )
61
+ hashsum = m .hexdigest ()
62
+
63
+ if (hashsum != request .headers ['hash' ]):
64
+ abort (401 )
65
+ else :
66
+ abort (401 )
67
+ else :
68
+ abort (401 )
69
+ else :
70
+ abort (401 )
71
+ else :
72
+ abort (401 )
73
+ return func (* args , ** kwargs )
74
+ return decorator
75
+
41
76
def sendEmail (toAddresses ,subject ,body ):
42
77
# Import smtplib for the actual sending function
43
78
import smtplib
44
-
79
+
45
80
from email .MIMEMultipart import MIMEMultipart
46
81
from email .MIMEText import MIMEText
47
82
from email .header import Header
48
-
83
+
49
84
server = smtplib .SMTP (host = app .config ["smtpServer" ], port = app .config ["smtpPort" ], timeout = 10 )
50
85
51
- if app .config ["smtpTLS" ]:
86
+ if app .config ["smtpTLS" ]:
52
87
server .starttls ()
53
-
88
+
54
89
server .ehlo ()
55
90
56
- if app .config ["smtpAuth" ]:
91
+ if app .config ["smtpAuth" ]:
57
92
server .login (app .config ["smtpUser" ], app .config ["smtpPassword" ])
58
93
59
94
fromAddr = app .config ["smtpFromAddr" ]
60
-
95
+
61
96
msg = MIMEMultipart ('alternative' )
62
97
msg ['From' ] = '"%s"<%s>' % (app .config ["smtpFromAddrName" ], app .config ["smtpFromAddr" ])
63
98
msg ['To' ] = "," .join (toAddresses )
64
99
msg ['Subject' ] = Header (subject ,'utf-8' )
65
-
100
+
66
101
msg .attach (MIMEText (body .encode ("utf-8" ), 'html' ,'utf-8' ))
67
-
102
+
68
103
text = msg .as_string ()
69
104
70
105
try :
71
106
server .sendmail (fromAddr , toAddresses , text )
72
107
finally :
73
108
server .quit ()
74
109
75
- def getConfirmationEmailBody (user ,code ,lang = "es" ):
76
- link = "<a href='" + app .config ["baseURL" ] + "/" + lang + "/user/" + user + "/" + code + "' target='_blank'>" + app .trans ['EMAIL_MSG_LINK' ][lang ]+ "</a>"
77
- m = "<h2>" + app .trans ['EMAIL_TITLE' ][lang ]+ "</h2>"
78
- m += "<h2>" + app .trans ['EMAIL_MSG_CONFIRM' ][lang ]+ "</h2>"
79
- m += "<p>" + app .trans ['EMAIL_MSG_PRELINK' ][lang ] + link + app .trans ['EMAIL_MSG_POSTLINK' ][lang ] + "</p>"
110
+ def sendAccountConfirmationEmail (username , userrealname , code , email , lang = "es" ):
111
+ link = app .config ["baseURL" ] + "/" + lang + "/user/" + username + "/" + code
112
+ body = render_template ('email_accountconfirmation_%s.html' % lang , confirmationurl = link , userrealname = userrealname )
113
+ sendEmail ([email ], app .trans ['EMAIL_ACCOUNTCONFIRMATION_SUBJECT' ][lang ], body );
114
+
115
+ def sendNewHistoryNotification (user , history ):
116
+ history ['url' ] = app .config ["baseURL" ] + "/" + user ['lang' ] + "/join/history/" + str (history ['id_history' ])
117
+ if user ['admin' ]:
118
+ body = render_template ('email_newhistory_admin_%s.html' % (user ['lang' ]), history = history )
119
+ else :
120
+ body = render_template ('email_newhistory_author_%s.html' % (user ['lang' ]), history = history )
121
+ sendEmail ([user ['email' ]], app .trans ['EMAIL_NEWHISTORY_SUBJECT' ][user ['lang' ]], body );
122
+
123
+ def sendEditedHistoryNotification (user , history ):
124
+ history ['url' ] = app .config ["baseURL" ] + "/" + user ['lang' ] + "/join/history/" + str (history ['id_history' ])
125
+ if user ['admin' ]:
126
+ body = render_template ('email_editedhistory_admin_%s.html' % (user ['lang' ]), history = history )
127
+ else :
128
+ body = render_template ('email_editedhistory_author_%s.html' % (user ['lang' ]), history = history )
129
+ sendEmail ([user ['email' ]], app .trans ['EMAIL_EDITEDHISTORY_SUBJECT' ][user ['lang' ]], body );
130
+
131
+ def sendPublishedHistoryNotification (user , history ):
132
+ history ['url' ] = app .config ["baseURL" ] + "/" + user ['lang' ] + "/join/history/" + str (history ['id_history' ])
133
+ if user ['admin' ]:
134
+ if history ['status' ] == 1 :
135
+ subject = app .trans ['EMAIL_PUBLISHEDHISTORY_SUBJECT' ][user ['lang' ]]
136
+ body = render_template ('email_publishedhistory_admin_%s.html' % (user ['lang' ]), history = history )
137
+ else :
138
+ subject = app .trans ['EMAIL_UNPUBLISHEDHISTORY_SUBJECT' ][user ['lang' ]]
139
+ body = render_template ('email_unpublishedhistory_admin_%s.html' % (user ['lang' ]), history = history )
140
+ else :
141
+ if history ['status' ] == 1 :
142
+ subject = app .trans ['EMAIL_PUBLISHEDHISTORY_SUBJECT' ][user ['lang' ]]
143
+ body = render_template ('email_publishedhistory_author_%s.html' % (user ['lang' ]), history = history )
144
+ else :
145
+ subject = app .trans ['EMAIL_UNPUBLISHEDHISTORY_SUBJECT' ][user ['lang' ]]
146
+ body = render_template ('email_unpublishedhistory_author_%s.html' % (user ['lang' ]), history = history )
147
+ sendEmail ([user ['email' ]], subject , body );
80
148
81
- return m ;
149
+ def sendDeletedHistoryNotification (user , history ):
150
+ history ['url' ] = app .config ["baseURL" ] + "/" + user ['lang' ] + "/join/history/" + str (history ['id_history' ])
151
+ if user ['admin' ]:
152
+ body = render_template ('email_deletedhistory_admin_%s.html' % (user ['lang' ]), history = history )
153
+ else :
154
+ body = render_template ('email_deletedhistory_author_%s.html' % (user ['lang' ]), history = history )
155
+ sendEmail ([user ['email' ]], app .trans ['EMAIL_DELETEDHISTORY_SUBJECT' ][user ['lang' ]], body );
0 commit comments