Beispiel #1
0
 def set_lang_in_cookie(self, lang):
     response = cherrypy.response
     if lang  == maki.i18n.ANY_LANG:
         response.cookie[maki.i18n.CKEY] = maki.i18n.ANY_LANG
     else:
         if lang in maki.i18n.AVAILABLE_LANGS:
             response.cookie[maki.i18n.CKEY] = lang
         else:
             log('Trying to set invalid lang %s' % lang,
                 'ERROR')
             return
     response.cookie[maki.i18n.CKEY]['path'] = '/'
     response.cookie[maki.i18n.CKEY]['expires'] = 3600
Beispiel #2
0
def update_model(model, fields, dmapper=(lambda x: x)):
    """Update the model `model` with the content of the dictionary `fields`.
    Use `dmapper` to fetch the real value from another mapper object.
    """
    if model:
        try:
            for k, v in fields.items():
                setattr(model, k, dmapper(v))
        except Exception:
            log('Error at k=%s and v=%s' % (k, v))
            raise 
        return True
    else:
        return False
Beispiel #3
0
def precautious_commit(dbs, errorm='Unable to commit the changes.'):
    """Commit the db.Session handling  any unexpected error.

    This is meant to be used on the form handling, makes use
    of the helper functions `put_mesages` to notify to add
    the errors.

    Return the error messages or None, en case of no error.
    """
    try:
        dbs.commit()
    except Exception as error:
        log(errorm, tb=True)
        emsg = '%s: %s' % (errorm, error.args[0])
        log(emsg)
        return emsg
    else:
        return None
Beispiel #4
0
 def _modify_post(self, action, *args):
     actionrslt, reqfields, update_method = self._identify_action(action)
     if self._have_valid_fields(reqfields):
         try:
             post_id_slug= update_method(*args, **cherrypy.request.json)
         except Exception as exep:
             log('Unable to modify post', tb=True)
             cherrypy.response.status = 500
             return {actionrslt: False,
                     'message': str(exep)}
         else:
             return {actionrslt: True,
                     'message': post_id_slug}
     else:
         log('Trying to modify post, with invalid fields \n\t%s' % \
             cherrypy.request.json)
         cherrypy.response.status = 500
         return {actionrslt: False,
                 'message': 'Invalid fields'}
Beispiel #5
0
def send(from_, to, subject, content, smtpconf=None):
    """
    Send and email, return True/False on Succes/Failure,
    depending on the configuration, any other
    possible error from the SMTP server will raise an Error.
    """
    smtpconf = get_smtp_config(smtpconf)
    log('Sending email', 'EMAIL')
    log("From: %s\n"\
        "To: %s\n"\
        "Subject: %s\n"\
        "Content: %s\n\n" % (from_, to, subject, content), 'EMAIL')
    msg = Message()
    msg['From'] = from_
    msg['To'] = to
    msg['Subject'] = subject
    msg['Content-Type'] = 'text/html'
    msg.set_payload(content)
    msg.set_charset('utf-8')
    # DEBUG
    #log(msg.as_string())
    if smtpconf is None:
        log('Missing required parameters [host, user, passwd]'
            ' please, make sure that you have the right configuration'
            ' in the main config file. Unable to send email.', 'ERROR')
    if in_development():
        return True
    else:
        if smtpconf is None:
            return False
        else: # Finally send!
            server = smtplib.SMTP_SSL(smtpconf['host'])
            server.login(smtpconf['user'], smtpconf['passwd'])
            server.send_message(msg)
            server.quit()
            return True