Show More
@@ -22,6 +22,7 b' debug = true' | |||
|
22 | 22 | #smtp_password = |
|
23 | 23 | #smtp_port = |
|
24 | 24 | #smtp_use_tls = false |
|
25 | #smtp_use_ssl = true | |
|
25 | 26 | |
|
26 | 27 | [server:main] |
|
27 | 28 | ##nr of threads to spawn |
@@ -22,6 +22,7 b' debug = true' | |||
|
22 | 22 | #smtp_password = |
|
23 | 23 | #smtp_port = |
|
24 | 24 | #smtp_use_tls = false |
|
25 | #smtp_use_ssl = true | |
|
25 | 26 | |
|
26 | 27 | [server:main] |
|
27 | 28 | ##nr of threads to spawn |
@@ -230,15 +230,31 b' def reset_user_password(user_email):' | |||
|
230 | 230 | |
|
231 | 231 | @task |
|
232 | 232 | def send_email(recipients, subject, body): |
|
233 | """ | |
|
234 | Sends an email with defined parameters from the .ini files. | |
|
235 | ||
|
236 | ||
|
237 | :param recipients: list of recipients, it this is empty the defined email | |
|
238 | address from field 'email_to' is used instead | |
|
239 | :param subject: subject of the mail | |
|
240 | :param body: body of the mail | |
|
241 | """ | |
|
233 | 242 | log = send_email.get_logger() |
|
234 | 243 | email_config = dict(config.items('DEFAULT')) |
|
244 | ||
|
245 | if not recipients: | |
|
246 | recipients = [email_config.get('email_to')] | |
|
247 | ||
|
248 | def str2bool(v): | |
|
249 | return v.lower() in ["yes", "true", "t", "1"] | |
|
250 | ||
|
235 | 251 | mail_from = email_config.get('app_email_from') |
|
236 | 252 | user = email_config.get('smtp_username') |
|
237 | 253 | passwd = email_config.get('smtp_password') |
|
238 | 254 | mail_server = email_config.get('smtp_server') |
|
239 | 255 | mail_port = email_config.get('smtp_port') |
|
240 | tls = email_config.get('smtp_use_tls') | |
|
241 | ssl = False | |
|
256 | tls = str2bool(email_config.get('smtp_use_tls')) | |
|
257 | ssl = str2bool(email_config.get('smtp_use_ssl')) | |
|
242 | 258 | |
|
243 | 259 | try: |
|
244 | 260 | m = SmtpMailer(mail_from, user, passwd, mail_server, |
@@ -22,7 +22,7 b' class SmtpMailer(object):' | |||
|
22 | 22 | |
|
23 | 23 | def __init__(self, mail_from, user, passwd, mail_server, |
|
24 | 24 | mail_port=None, ssl=False, tls=False): |
|
25 | ||
|
25 | ||
|
26 | 26 | self.mail_from = mail_from |
|
27 | 27 | self.mail_server = mail_server |
|
28 | 28 | self.mail_port = mail_port |
@@ -31,7 +31,7 b' class SmtpMailer(object):' | |||
|
31 | 31 | self.ssl = ssl |
|
32 | 32 | self.tls = tls |
|
33 | 33 | self.debug = False |
|
34 | ||
|
34 | ||
|
35 | 35 | def send(self, recipients=[], subject='', body='', attachment_files={}): |
|
36 | 36 | |
|
37 | 37 | if isinstance(recipients, basestring): |
@@ -43,11 +43,11 b' class SmtpMailer(object):' | |||
|
43 | 43 | |
|
44 | 44 | if self.tls: |
|
45 | 45 | smtp_serv.starttls() |
|
46 | ||
|
47 |
if self.debug: |
|
|
46 | ||
|
47 | if self.debug: | |
|
48 | 48 | smtp_serv.set_debuglevel(1) |
|
49 | 49 | |
|
50 | smtp_serv.ehlo("mailer") | |
|
50 | smtp_serv.ehlo("rhodecode mailer") | |
|
51 | 51 | |
|
52 | 52 | #if server requires authorization you must provide login and password |
|
53 | 53 | smtp_serv.login(self.user, self.passwd) |
@@ -82,13 +82,13 b' class SmtpMailer(object):' | |||
|
82 | 82 | maintype, subtype = ctype.split('/', 1) |
|
83 | 83 | if maintype == 'text': |
|
84 | 84 | # Note: we should handle calculating the charset |
|
85 |
file_part = MIMEText(self.get_content(msg_file), |
|
|
85 | file_part = MIMEText(self.get_content(msg_file), | |
|
86 | 86 | _subtype=subtype) |
|
87 | 87 | elif maintype == 'image': |
|
88 |
file_part = MIMEImage(self.get_content(msg_file), |
|
|
88 | file_part = MIMEImage(self.get_content(msg_file), | |
|
89 | 89 | _subtype=subtype) |
|
90 | 90 | elif maintype == 'audio': |
|
91 |
file_part = MIMEAudio(self.get_content(msg_file), |
|
|
91 | file_part = MIMEAudio(self.get_content(msg_file), | |
|
92 | 92 | _subtype=subtype) |
|
93 | 93 | else: |
|
94 | 94 | file_part = MIMEBase(maintype, subtype) |
@@ -96,13 +96,13 b' class SmtpMailer(object):' | |||
|
96 | 96 | # Encode the payload using Base64 |
|
97 | 97 | encoders.encode_base64(msg) |
|
98 | 98 | # Set the filename parameter |
|
99 |
file_part.add_header('Content-Disposition', 'attachment', |
|
|
99 | file_part.add_header('Content-Disposition', 'attachment', | |
|
100 | 100 | filename=f_name) |
|
101 | 101 | file_part.add_header('Content-Type', ctype, name=f_name) |
|
102 | 102 | msg.attach(file_part) |
|
103 | 103 | else: |
|
104 |
raise Exception('Attachment files should be' |
|
|
105 |
'a dict in format {"filename":"filepath"}') |
|
|
104 | raise Exception('Attachment files should be' | |
|
105 | 'a dict in format {"filename":"filepath"}') | |
|
106 | 106 | |
|
107 | 107 | def get_content(self, msg_file): |
|
108 | 108 | ''' |
@@ -68,9 +68,18 b' def is_git(environ):' | |||
|
68 | 68 | return True |
|
69 | 69 | return False |
|
70 | 70 | |
|
71 | def action_logger(user, action, repo, ipaddr, sa=None): | |
|
71 | def action_logger(user, action, repo, ipaddr='', sa=None): | |
|
72 | 72 | """ |
|
73 | 73 | Action logger for various action made by users |
|
74 | ||
|
75 | :param user: user that made this action, can be a string unique username or | |
|
76 | object containing user_id attribute | |
|
77 | :param action: action to log, should be on of predefined unique actions for | |
|
78 | easy translations | |
|
79 | :param repo: repository that action was made on | |
|
80 | :param ipaddr: optional ip address from what the action was made | |
|
81 | :param sa: optional sqlalchemy session | |
|
82 | ||
|
74 | 83 | """ |
|
75 | 84 | |
|
76 | 85 | if not sa: |
@@ -84,12 +93,22 b' def action_logger(user, action, repo, ip' | |||
|
84 | 93 | else: |
|
85 | 94 | raise Exception('You have to provide user object or username') |
|
86 | 95 | |
|
87 | repo_name = repo.lstrip('/') | |
|
96 | ||
|
97 | if repo: | |
|
98 | repo_name = repo.lstrip('/') | |
|
99 | ||
|
100 | repository = RepoModel(sa).get(repo_name, cache=False) | |
|
101 | if not repository: | |
|
102 | raise Exception('You have to provide valid repository') | |
|
103 | else: | |
|
104 | raise Exception('You have to provide repository to action logger') | |
|
105 | ||
|
106 | ||
|
88 | 107 | user_log = UserLog() |
|
89 | 108 | user_log.user_id = user_obj.user_id |
|
90 | 109 | user_log.action = action |
|
91 | 110 | user_log.repository_name = repo_name |
|
92 |
user_log.repository = |
|
|
111 | user_log.repository = repository | |
|
93 | 112 | user_log.action_date = datetime.datetime.now() |
|
94 | 113 | user_log.user_ip = ipaddr |
|
95 | 114 | sa.add(user_log) |
@@ -72,6 +72,7 b' class UserModel(object):' | |||
|
72 | 72 | raise |
|
73 | 73 | |
|
74 | 74 | def create_registration(self, form_data): |
|
75 | from rhodecode.lib.celerylib import tasks, run_task | |
|
75 | 76 | try: |
|
76 | 77 | new_user = User() |
|
77 | 78 | for k, v in form_data.items(): |
@@ -80,6 +81,14 b' class UserModel(object):' | |||
|
80 | 81 | |
|
81 | 82 | self.sa.add(new_user) |
|
82 | 83 | self.sa.commit() |
|
84 | body = ('New user registration\n' | |
|
85 | 'username: %s\n' | |
|
86 | 'email: %s\n') | |
|
87 | body = body % (form_data['username'], form_data['email']) | |
|
88 | ||
|
89 | run_task(tasks.send_email, None, | |
|
90 | _('[RhodeCode] New User registration'), | |
|
91 | body) | |
|
83 | 92 | except: |
|
84 | 93 | log.error(traceback.format_exc()) |
|
85 | 94 | self.sa.rollback() |
General Comments 0
You need to be logged in to leave comments.
Login now