##// END OF EJS Templates
fixes #59, notifications for user registrations + some changes to mailer
marcink -
r689:ecc566f8 beta
parent child Browse files
Show More
@@ -22,6 +22,7 b' debug = true'
22 #smtp_password =
22 #smtp_password =
23 #smtp_port =
23 #smtp_port =
24 #smtp_use_tls = false
24 #smtp_use_tls = false
25 #smtp_use_ssl = true
25
26
26 [server:main]
27 [server:main]
27 ##nr of threads to spawn
28 ##nr of threads to spawn
@@ -22,6 +22,7 b' debug = true'
22 #smtp_password =
22 #smtp_password =
23 #smtp_port =
23 #smtp_port =
24 #smtp_use_tls = false
24 #smtp_use_tls = false
25 #smtp_use_ssl = true
25
26
26 [server:main]
27 [server:main]
27 ##nr of threads to spawn
28 ##nr of threads to spawn
@@ -230,15 +230,31 b' def reset_user_password(user_email):'
230
230
231 @task
231 @task
232 def send_email(recipients, subject, body):
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 log = send_email.get_logger()
242 log = send_email.get_logger()
234 email_config = dict(config.items('DEFAULT'))
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 mail_from = email_config.get('app_email_from')
251 mail_from = email_config.get('app_email_from')
236 user = email_config.get('smtp_username')
252 user = email_config.get('smtp_username')
237 passwd = email_config.get('smtp_password')
253 passwd = email_config.get('smtp_password')
238 mail_server = email_config.get('smtp_server')
254 mail_server = email_config.get('smtp_server')
239 mail_port = email_config.get('smtp_port')
255 mail_port = email_config.get('smtp_port')
240 tls = email_config.get('smtp_use_tls')
256 tls = str2bool(email_config.get('smtp_use_tls'))
241 ssl = False
257 ssl = str2bool(email_config.get('smtp_use_ssl'))
242
258
243 try:
259 try:
244 m = SmtpMailer(mail_from, user, passwd, mail_server,
260 m = SmtpMailer(mail_from, user, passwd, mail_server,
@@ -22,7 +22,7 b' class SmtpMailer(object):'
22
22
23 def __init__(self, mail_from, user, passwd, mail_server,
23 def __init__(self, mail_from, user, passwd, mail_server,
24 mail_port=None, ssl=False, tls=False):
24 mail_port=None, ssl=False, tls=False):
25
25
26 self.mail_from = mail_from
26 self.mail_from = mail_from
27 self.mail_server = mail_server
27 self.mail_server = mail_server
28 self.mail_port = mail_port
28 self.mail_port = mail_port
@@ -31,7 +31,7 b' class SmtpMailer(object):'
31 self.ssl = ssl
31 self.ssl = ssl
32 self.tls = tls
32 self.tls = tls
33 self.debug = False
33 self.debug = False
34
34
35 def send(self, recipients=[], subject='', body='', attachment_files={}):
35 def send(self, recipients=[], subject='', body='', attachment_files={}):
36
36
37 if isinstance(recipients, basestring):
37 if isinstance(recipients, basestring):
@@ -43,11 +43,11 b' class SmtpMailer(object):'
43
43
44 if self.tls:
44 if self.tls:
45 smtp_serv.starttls()
45 smtp_serv.starttls()
46
46
47 if self.debug:
47 if self.debug:
48 smtp_serv.set_debuglevel(1)
48 smtp_serv.set_debuglevel(1)
49
49
50 smtp_serv.ehlo("mailer")
50 smtp_serv.ehlo("rhodecode mailer")
51
51
52 #if server requires authorization you must provide login and password
52 #if server requires authorization you must provide login and password
53 smtp_serv.login(self.user, self.passwd)
53 smtp_serv.login(self.user, self.passwd)
@@ -82,13 +82,13 b' class SmtpMailer(object):'
82 maintype, subtype = ctype.split('/', 1)
82 maintype, subtype = ctype.split('/', 1)
83 if maintype == 'text':
83 if maintype == 'text':
84 # Note: we should handle calculating the charset
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 _subtype=subtype)
86 _subtype=subtype)
87 elif maintype == 'image':
87 elif maintype == 'image':
88 file_part = MIMEImage(self.get_content(msg_file),
88 file_part = MIMEImage(self.get_content(msg_file),
89 _subtype=subtype)
89 _subtype=subtype)
90 elif maintype == 'audio':
90 elif maintype == 'audio':
91 file_part = MIMEAudio(self.get_content(msg_file),
91 file_part = MIMEAudio(self.get_content(msg_file),
92 _subtype=subtype)
92 _subtype=subtype)
93 else:
93 else:
94 file_part = MIMEBase(maintype, subtype)
94 file_part = MIMEBase(maintype, subtype)
@@ -96,13 +96,13 b' class SmtpMailer(object):'
96 # Encode the payload using Base64
96 # Encode the payload using Base64
97 encoders.encode_base64(msg)
97 encoders.encode_base64(msg)
98 # Set the filename parameter
98 # Set the filename parameter
99 file_part.add_header('Content-Disposition', 'attachment',
99 file_part.add_header('Content-Disposition', 'attachment',
100 filename=f_name)
100 filename=f_name)
101 file_part.add_header('Content-Type', ctype, name=f_name)
101 file_part.add_header('Content-Type', ctype, name=f_name)
102 msg.attach(file_part)
102 msg.attach(file_part)
103 else:
103 else:
104 raise Exception('Attachment files should be'
104 raise Exception('Attachment files should be'
105 'a dict in format {"filename":"filepath"}')
105 'a dict in format {"filename":"filepath"}')
106
106
107 def get_content(self, msg_file):
107 def get_content(self, msg_file):
108 '''
108 '''
@@ -68,9 +68,18 b' def is_git(environ):'
68 return True
68 return True
69 return False
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 Action logger for various action made by users
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 if not sa:
85 if not sa:
@@ -84,12 +93,22 b' def action_logger(user, action, repo, ip'
84 else:
93 else:
85 raise Exception('You have to provide user object or username')
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 user_log = UserLog()
107 user_log = UserLog()
89 user_log.user_id = user_obj.user_id
108 user_log.user_id = user_obj.user_id
90 user_log.action = action
109 user_log.action = action
91 user_log.repository_name = repo_name
110 user_log.repository_name = repo_name
92 user_log.repository = RepoModel(sa).get(repo_name, cache=False)
111 user_log.repository = repository
93 user_log.action_date = datetime.datetime.now()
112 user_log.action_date = datetime.datetime.now()
94 user_log.user_ip = ipaddr
113 user_log.user_ip = ipaddr
95 sa.add(user_log)
114 sa.add(user_log)
@@ -72,6 +72,7 b' class UserModel(object):'
72 raise
72 raise
73
73
74 def create_registration(self, form_data):
74 def create_registration(self, form_data):
75 from rhodecode.lib.celerylib import tasks, run_task
75 try:
76 try:
76 new_user = User()
77 new_user = User()
77 for k, v in form_data.items():
78 for k, v in form_data.items():
@@ -80,6 +81,14 b' class UserModel(object):'
80
81
81 self.sa.add(new_user)
82 self.sa.add(new_user)
82 self.sa.commit()
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 except:
92 except:
84 log.error(traceback.format_exc())
93 log.error(traceback.format_exc())
85 self.sa.rollback()
94 self.sa.rollback()
General Comments 0
You need to be logged in to leave comments. Login now