def if_match(self, item): header = self.headers.get("If-Match", item.etag) header = rfc822.unquote(header) if header == item.etag: return True quoted = '"' + item.etag + '"' if header == quoted: return True extraquoted = rfc822.quote(quoted) if header == extraquoted: return True return False
def test_quote_unquote(self): eq = self.assertEqual eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name') eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')
def _checklogin(request, *args, **kwargs): if test_func(request.user): return view_func(request, *args, **kwargs) elif not request.user.is_authenticated(): return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, quote(request.get_full_path()))) else: resp = render_to_response('403.html', context_instance=RequestContext(request)) resp.status_code = 403 return resp
def send_email(recipients, subject, body='', html_body='', headers=None, author=None): """ Sends an email with defined parameters from the .ini files. :param recipients: list of recipients, if this is None, the defined email address from field 'email_to' and all admins is used instead :param subject: subject of the mail :param body: body of the mail :param html_body: html version of body :param headers: dictionary of prepopulated e-mail headers :param author: User object of the author of this mail, if known and relevant """ log = get_logger(send_email) assert isinstance(recipients, list), recipients if headers is None: headers = {} else: # do not modify the original headers object passed by the caller headers = headers.copy() email_config = config email_prefix = email_config.get('email_prefix', '') if email_prefix: subject = "%s %s" % (email_prefix, subject) if not recipients: # if recipients are not defined we send to email_config + all admins recipients = [u.email for u in User.query() .filter(User.admin == True).all()] if email_config.get('email_to') is not None: recipients += [email_config.get('email_to')] # If there are still no recipients, there are no admins and no address # configured in email_to, so return. if not recipients: log.error("No recipients specified and no fallback available.") return False log.warning("No recipients specified for '%s' - sending to admins %s", subject, ' '.join(recipients)) # SMTP sender envelope_from = email_config.get('app_email_from', 'Kallithea') # 'From' header if author is not None: # set From header based on author but with a generic e-mail address # In case app_email_from is in "Some Name <e-mail>" format, we first # extract the e-mail address. envelope_addr = author_email(envelope_from) headers['From'] = '"%s" <%s>' % ( rfc822.quote('%s (no-reply)' % author.full_name_or_username), envelope_addr) user = email_config.get('smtp_username') passwd = email_config.get('smtp_password') mail_server = email_config.get('smtp_server') mail_port = email_config.get('smtp_port') tls = str2bool(email_config.get('smtp_use_tls')) ssl = str2bool(email_config.get('smtp_use_ssl')) debug = str2bool(email_config.get('debug')) smtp_auth = email_config.get('smtp_auth') logmsg = ("Mail details:\n" "recipients: %s\n" "headers: %s\n" "subject: %s\n" "body:\n%s\n" "html:\n%s\n" % (' '.join(recipients), headers, subject, body, html_body)) if mail_server: log.debug("Sending e-mail. " + logmsg) else: log.error("SMTP mail server not configured - cannot send e-mail.") log.warning(logmsg) return False try: m = SmtpMailer(envelope_from, user, passwd, mail_server, smtp_auth, mail_port, ssl, tls, debug=debug) m.send(recipients, subject, body, html_body, headers=headers) except: log.error('Mail sending failed') log.error(traceback.format_exc()) return False return True