def get_field_history(user, site, fieldname=None): """get the history of one field (for the history slider) (or for all - global slider)""" if fieldname: entries = Entry.select().where(Entry.user==user, Entry.site==site, Entry.fieldname==fieldname).order_by(Entry.created.desc()) else: entries = Entry.select().where(Entry.user==user, Entry.site==site).order_by(Entry.created.desc()) data = [{entry.fieldname: entry.value} for entry in entries] return json.dumps(data)
def show_template(user, site, template=None, edit=False): """render a template with the latest content for an entry""" entries = Entry.select().where(Entry.user==user, Entry.site==site).order_by(Entry.created.desc()) for e in entries: print e.fieldname, e.value.replace("\n", "")[:60] seen_entries = [] simple_entries = [] collections = {} items = {} for e in entries: identifier = "{}_{}".format(e.parent, e.fieldname) if not identifier in seen_entries: if e.type == "collection" and e.fieldname not in collections: collections[e.fieldname] = [] elif e.type == "item": collections.get(e.parent, []).append(e.fieldname) items[e.fieldname] = {"__parent": e.parent, "__name": e.fieldname} elif e.parent: items.get(e.parent, {})[e.fieldname] = e.value else: simple_entries.append(e) seen_entries.append(identifier) data = {entry.fieldname: entry.value for entry in simple_entries} collections = {k: [items.get(item, {}) for item in kitems] for k, kitems in collections.items()} data.update(collections) templates_path = app.jinja_loader.searchpath[0] if not template: template = 'index.html' current_template_path = os.path.join(templates_path, user, site, template) if not os.path.exists(current_template_path): return 'Response(status_code=404)', 404 template_string = open(current_template_path).read() return render_template_string(template_string, __user=user, __site=site, __template=template, cnoms_edit=edit, **data)
def show_user(user): """show all information for a user""" leads = Entry.select().where(Entry.user==user).group_by(Entry.site) sites = [] for site in leads: sites.append({ "name": site.site, "user": site.user, }) return render_template('show_user.html', sites=sites, user=user)
def show_site_admin(user, site): """admin view for a site""" # get all templates templates_path = app.jinja_loader.searchpath[0] templates = glob.glob(os.path.join(templates_path, user, site, '*.html')) templates = [os.path.basename(t) for t in templates] # get all fieldnames fields = [] entries = Entry.select().where(Entry.user==user, Entry.site==site).order_by(Entry.created.desc()) for entry in entries: d = {'type': entry.type, 'fieldname': entry.fieldname, 'value': entry.value} if not d in fields: fields.append(d) return json.dumps({"templates": templates, "fields": fields})
def change_entry(user, site): """receive entry changes via ajax call""" Entry.create(user=user, site=site, **request.form) return ''
def import_website(user=None, path_to_site=None): """import a website * create templates * add content to database * copy static files """ print 'import_website' if not (user and path_to_site): path_to_site = request.args['path_to_site'] user = request.args['user'] sitename = os.path.basename(os.path.normpath(path_to_site)) static_path = os.path.join(app.config['STATIC_MEDIA'], user, sitename) if not os.path.exists(static_path): os.makedirs(static_path) # copy static files # path = os.path.dirname(__file__) # new_static_path = os.path.join(path, '..', 'static', user, sitename) # if os.path.exists(new_static_path): # shutil.rmtree(new_static_path) # if os.path.exists(os.path.join(path_to_site, 'static')): # shutil.copytree(os.path.join(path_to_site, 'static'), new_static_path) # else: # os.makedirs(os.path.join(new_static_path, 'static')) # if os.path.exists(os.path.join(path_to_site, '__icon.png')): # shutil.copyfile(os.path.join(path_to_site, '__icon.png'), os.path.join(new_static_path, '__icon.png')) # create templates and add parsed stuff to db new_templates_path = os.path.join(app.config['TEMPLATE_PATH'], user, sitename) if not os.path.exists(new_templates_path): os.makedirs(new_templates_path) parser = Parser(user, sitename) for filename in os.listdir(path_to_site): if any([filename.endswith(ext) for ext in app.config['HTML_EXT']]): template = parser.parse_html(os.path.join(path_to_site, filename)) save_path = os.path.join(new_templates_path, os.path.basename(filename)) with open(save_path, 'w') as f: f.write(str(template)) for entry in parser.fields: Entry.get_or_create(user=user, site=sitename, **entry) for resource in set(parser.resources): if any([resource.endswith(ext) for ext in app.config["STYLESHEET_EXT"]]): parser.parse_css(path_to_site, resource) for resource in set(parser.resources): source = os.path.join(path_to_site, resource) destination = os.path.join(static_path, resource) if not os.path.exists(source): print "WARNING", source, "does not exist." else: if not os.path.exists(os.path.dirname(destination)): os.makedirs(os.path.dirname(destination)) shutil.copyfile(source, destination) print "Copying", resource #shutil.copyfile(os.path.join(path_to_site, '__icon.png'), os.path.join(new_static_path, '__icon.png')) return ''