def enable_attachments(slug, obj, path, movefile=False): """ Used to process new object files upload it'll rename the filename to new path and also add content_object according obj and if slug is None, then it'll do nothing """ fileserving = AttachmentsFileServing() for f in get_attachments(slug or obj): f.enabled = True f.content_object = obj if slug and movefile: r_filename = fileserving.get_filename(f.filepath, convert=False) dir = os.path.dirname(f.filepath) _file = os.path.basename(f.filepath) new_filename = os.path.join(dir, str(path), _file) f.filepath = new_filename r_new_filename = fileserving.get_filename(new_filename, convert=False) #check if the new directory is existed new_dir = os.path.dirname(r_new_filename) if not os.path.exists(new_dir): os.makedirs(new_dir) try: os.rename(r_filename, r_new_filename) except Exception, e: log.exception(e) log.info("from %s to %s", r_filename, r_new_filename) f.save()
def load_table(table, filename, con, delimiter=',', format=None, encoding='utf-8', delete=True): import csv from uliweb.utils.date import to_date, to_datetime table = reflect_table(con, table.name) if delete: do_(table.delete()) if not os.path.exists(filename): log.info("The table [%s] data is not existed." % table.name) return f = fin = open(filename, 'rb') try: first_line = f.readline() fields = first_line[1:].strip().split() n = 1 if format: fin = csv.reader(f, delimiter=delimiter) for line in fin: try: n += 1 if not format: line = eval(line.strip()) record = dict(zip(fields, line)) params = {} for c in table.c: if c.name in record: if not format: params[c.name] = record[c.name] else: if record[c.name] == 'NULL': params[c.name] = None else: if isinstance(c.type, String): params[c.name] = unicode( record[c.name], encoding) elif isinstance(c.type, Date): params[c.name] = to_date( to_datetime(record[c.name])) elif isinstance(c.type, DateTime): params[c.name] = to_datetime( record[c.name]) else: params[c.name] = record[c.name] ins = table.insert().values(**params) do_(ins) except: log.error('Error: Line %d' % n) raise finally: f.close()
def add_rule(map, url, endpoint=None, **kwargs): from werkzeug.routing import Rule kwargs['endpoint'] = endpoint try: map.add(Rule(url, **kwargs)) except ValueError as e: log.info("Wrong url is %s, endpoint=%s" % (url, endpoint)) raise
def process(): from uliweb.utils.common import Serial from uliweb.mail import Mail redis = functions.get_redis() while 1: data = redis.brpop("sendmail", 5) if data: message = Serial.load(data[1]) log.info(message) Mail().send_mail(**message) else: log.info("no data")
def load_table(table, filename, con, delimiter=',', format=None, encoding='utf-8', delete=True): import csv from uliweb.utils.date import to_date, to_datetime table = reflect_table(con, table.name) if delete: do_(table.delete()) if not os.path.exists(filename): log.info("The table [%s] data is not existed." % table.name) return f = fin = open(filename, 'rb') try: first_line = f.readline() fields = first_line[1:].strip().split() n = 1 if format: fin = csv.reader(f, delimiter=delimiter) for line in fin: try: n += 1 if not format: line = eval(line.strip()) record = dict(zip(fields, line)) params = {} for c in table.c: if c.name in record: if not format: params[c.name] = record[c.name] else: if record[c.name] == 'NULL': params[c.name] = None else: if isinstance(c.type, String): params[c.name] = unicode(record[c.name], encoding) elif isinstance(c.type, Date): params[c.name] = to_date(to_datetime(record[c.name])) elif isinstance(c.type, DateTime): params[c.name] = to_datetime(record[c.name]) else: params[c.name] = record[c.name] ins = table.insert().values(**params) do_(ins) except: log.error('Error: Line %d' % n) raise finally: f.close()
def log(self, message): log.info("[{0}] {1}".format(self.socket.sessid, message))
from uliweb.manage import make_simple_application from uliweb import functions from uliweb.utils.common import log def process(): from uliweb.utils.common import Serial from uliweb.mail import Mail redis = functions.get_redis() while 1: data = redis.brpop("sendmail", 5) if data: message = Serial.load(data[1]) log.info(message) Mail().send_mail(**message) else: log.info("no data") if __name__ == "__main__": app = make_simple_application(project_dir="..") log.info("staring...") process()
def find(plugin, *args, **kwargs): from uliweb.core.SimpleFrame import get_app_dir from uliweb import application as app, settings from uliweb.utils.common import is_pyfile_exist, import_attr key = (plugin, repr(args) + repr(sorted(kwargs.items()))) if key in __use_cached__: return __use_cached__[key] if plugin in __saved_template_plugins_modules__: mod = __saved_template_plugins_modules__[plugin] else: #add settings support, only support simple situation #so for complex cases you should still write module #format just like: # #[TEMPLATE_USE] #name = { # 'toplinks':[ # 'myapp/jquery.myapp.{version}.min.js', # ], # 'depends':[xxxx], # 'config':{'version':'UI_CONFIG/test'}, # 'default':{'version':'1.2.0'}, #} # # add toplinks and bottomlinks could be config by a function path, # just like: # #[TEMPLATE_USE] #name = { # 'toplinks':'#{appname}.load_js', # 'config':{'version':'UI_CONFIG/test'}, #} mod = None c = settings.get_var('TEMPLATE_USE/' + plugin) if c: config = c.pop('config', {}) default = c.pop('default', {}) #evaluate config value config = dict([(k, settings.get_var(v, default.get(k, ''))) for k, v in config.items()]) #merge passed arguments config.update(kwargs) for t in ['toplinks', 'bottomlinks']: if t in c: links = c[t] if isinstance(links, (tuple, list)): c[t] = [x.format(**config) for x in c[t]] elif isinstance(links, string_types): m = import_attr(links) c[t] = [x for x in m(**config)] mod = c else: for p in reversed(app.apps): if not is_pyfile_exist( os.path.join(get_app_dir(p), 'template_plugins'), plugin): continue module = '.'.join([p, 'template_plugins', plugin]) try: mod = __import__(module, fromlist=['*']) break except ImportError as e: log.info('Module path is {}'.format( os.path.join(get_app_dir(p), 'template_plugins', plugin))) log.exception(e) mod = None if mod: __saved_template_plugins_modules__[plugin] = mod else: log.error( "Can't find the [%s] template plugin, please check if you've installed special app already" % plugin) raise UseModuleNotFound( "Can't find the %s template plugin, check if you've installed special app already" % plugin) #mod maybe an dict if isinstance(mod, dict): v = mod else: v = None call = getattr(mod, 'call', None) call.__name__ = call.__module__ if call: para = inspect.getargspec(call)[0] #test if the funtion is defined as old style if ['app', 'var', 'env'] == para[:3]: warnings.simplefilter('default') warnings.warn( "Tmplate plugs call function(%s) should be defined" " as call(*args, **kwargs) not need (app, var, env) any more" % call.__module__, DeprecationWarning) v = call(app, {}, {}, *args, **kwargs) else: v = call(*args, **kwargs) toplinks = [] bottomlinks = [] if v: if 'depends' in v: for _t in v['depends']: if isinstance(_t, str): t, b = find(_t) else: d, kw = _t t, b = find(d, **kw) toplinks.extend(t) bottomlinks.extend(b) if 'toplinks' in v: links = v['toplinks'] if not isinstance(links, (tuple, list)): links = [links] toplinks.extend(links) if 'bottomlinks' in v: links = v['bottomlinks'] if not isinstance(links, (tuple, list)): links = [links] bottomlinks.extend(links) if 'depends_after' in v: for _t in v['depends_after']: if isinstance(_t, str): t, b = use(env, _t) else: d, kw = _t t, b = use(env, d, **kw) toplinks.extend(t) bottomlinks.extend(b) __use_cached__[key] = toplinks, bottomlinks return toplinks, bottomlinks
def find(plugin, *args, **kwargs): from uliweb.core.SimpleFrame import get_app_dir from uliweb import application as app, settings from uliweb.utils.common import is_pyfile_exist, import_attr key = (plugin, repr(args) + repr(sorted(kwargs.items()))) if key in __use_cached__: return __use_cached__[key] if plugin in __saved_template_plugins_modules__: mod = __saved_template_plugins_modules__[plugin] else: #add settings support, only support simple situation #so for complex cases you should still write module #format just like: # #[TEMPLATE_USE] #name = { # 'toplinks':[ # 'myapp/jquery.myapp.{version}.min.js', # ], # 'depends':[xxxx], # 'config':{'version':'UI_CONFIG/test'}, # 'default':{'version':'1.2.0'}, #} # # add toplinks and bottomlinks could be config by a function path, # just like: # #[TEMPLATE_USE] #name = { # 'toplinks':'#{appname}.load_js', # 'config':{'version':'UI_CONFIG/test'}, #} mod = None c = settings.get_var('TEMPLATE_USE/'+plugin) if c: config = c.pop('config', {}) default = c.pop('default', {}) #evaluate config value config = dict([(k, settings.get_var(v, default.get(k, ''))) for k, v in config.items()]) #merge passed arguments config.update(kwargs) for t in ['toplinks', 'bottomlinks']: if t in c: links = c[t] if isinstance(links, (tuple, list)): c[t] = [x.format(**config) for x in c[t]] elif isinstance(links, (str, unicode)): m = import_attr(links) c[t] = [x for x in m(**config)] mod = c else: for p in reversed(app.apps): if not is_pyfile_exist(os.path.join(get_app_dir(p), 'template_plugins'), plugin): continue module = '.'.join([p, 'template_plugins', plugin]) try: mod = __import__(module, fromlist=['*']) break except ImportError as e: log.info('Module path is {}'.format(os.path.join(get_app_dir(p), 'template_plugins', plugin))) log.exception(e) mod = None if mod: __saved_template_plugins_modules__[plugin] = mod else: log.error("Can't find the [%s] template plugin, please check if you've installed special app already" % plugin) raise UseModuleNotFound("Can't find the %s template plugin, check if you've installed special app already" % plugin) #mod maybe an dict if isinstance(mod, dict): v = mod else: v = None call = getattr(mod, 'call', None) call.__name__ = call.__module__ if call: para = inspect.getargspec(call)[0] #test if the funtion is defined as old style if ['app', 'var', 'env'] == para[:3]: warnings.simplefilter('default') warnings.warn("Tmplate plugs call function(%s) should be defined" " as call(*args, **kwargs) not need (app, var, env) any more" % call.__module__, DeprecationWarning) v = call(app, {}, {}, *args, **kwargs) else: v = call(*args, **kwargs) toplinks = [] bottomlinks = [] if v: if 'depends' in v: for _t in v['depends']: if isinstance(_t, str): t, b = find(_t) else: d, kw = _t t, b = find(d, **kw) toplinks.extend(t) bottomlinks.extend(b) if 'toplinks' in v: links = v['toplinks'] if not isinstance(links, (tuple, list)): links = [links] toplinks.extend(links) if 'bottomlinks' in v: links = v['bottomlinks'] if not isinstance(links, (tuple, list)): links = [links] bottomlinks.extend(links) if 'depends_after' in v: for _t in v['depends_after']: if isinstance(_t, str): t, b = use(env, _t) else: d, kw = _t t, b = use(env, d, **kw) toplinks.extend(t) bottomlinks.extend(b) __use_cached__[key] = toplinks, bottomlinks return toplinks, bottomlinks