def edit_chapter(author, namespace, chapter): if chapter == "0": tmpl = tmpl_picker('form_chapter') return render_template(tmpl, form=g.form, namespace=namespace) else: i_data = DB(g.location).get_a_chapter(namespace, chapter) itmpl = tmpl_picker('form_chapter_edit') return render_template(itmpl, form=g.form, items=i_data, namespace=namespace, chapter=chapter)
def edit_intro(author, namespace): if namespace == "0": tmpl = tmpl_picker('form_intro') return render_template(tmpl, form=g.form) else: i_data = DB(g.location).get_an_intro(namespace) namespaces = {} for l in i_data["langs"]: if l != "en": namespaces[l] = i_data["namespace_" + l] tmpl = tmpl_picker('form_intro_edit') return render_template(tmpl, form=g.form, items=i_data, namespaces=namespaces)
def save_tags(namespace, author, action): error = 0 if Auth.is_authenticated and (current_user.author == author or current_user.access > 1): f = request.form if f: datadic = {} for key in f.keys(): _id = key.rsplit('_', 1)[1] k = key.rsplit('_', 1)[0] if len(_id) == 24: # ObjectID in Mongo try: datadic[_id].update({k: f.get(key)}) except: datadic[_id] = {k: f.get(key)} datadic[_id].update({"namespace" : [namespace]}) for _id, data in datadic.iteritems(): DB(g.location).update_an_object(_id, data) else: error == 1 else: error = "Something wrong with a form or authentification!" flash(error, category='error') if error == 0: flash("Data updated successfully!", category='info') else: error = "Something wrong with a form or authentification!" flash(error, category='error') itmpl = tmpl_picker('form_tags') new_objects = DB(g.location).get_objects_by_key_sorted_filter_yes("", "I_S_codename", namespace) spaces = DB(g.location).get_spaces_by_author_ns(author, namespace) data = json.loads(dumps(spaces)) chapters = chapters_for_tags(data) return render_template(itmpl, form=g.form, objects=g.objects, conditions=g.conditions, drugs=g.drugs, geo_objects=g.objects_geo, new_objects=new_objects, namespace=namespace, chapters=chapters)
def edit_tags(namespace, author, action): itmpl = tmpl_picker('form_tags') new_objects = DB(g.location).get_objects_by_key_sorted_filter_yes("", "I_S_codename", namespace) spaces = DB(g.location).get_spaces_by_author_ns(author, namespace) data = json.loads(dumps(spaces)) chapters = chapters_for_tags(data) return render_template(itmpl, form=g.form, objects=g.objects, conditions=g.conditions, drugs=g.drugs, geo_objects=g.objects_geo, new_objects=new_objects, namespace=namespace, chapters=chapters)
def editspace(author): namespaces = DB(g.location).get_intros_by_author(author) spaces = DB(g.location).get_spaces_by_author(author) data = json.loads(dumps(spaces)) chapters = defaultdict(list) for d in range(0, len(data)): ns = data[d]["namespace"] c_date = datetime.fromtimestamp( data[d]["date"]['$date'] / 1e3 ).strftime('%d-%m-%Y %H:%M') try: e_date = datetime.fromtimestamp( data[d]["editdate"]['$date'] / 1e3 ).strftime('%d-%m-%Y %H:%M') except: e_date = "" ch = data[d]["title"] + "|" + data[d]["I_S_codename"] + "|" + c_date + "|" + e_date chapters[ns].append(ch) tmpl = tmpl_picker('cms_start') return render_template(tmpl, form=g.form, items=g.items, author=author, namespaces=namespaces, chapters=chapters)
def profile(author): tmpl = tmpl_picker('profile') return render_template(tmpl, form=g.form, items=g.items)
def save_chapter(author, namespace, chapter): if chapter == "0": sform = newChapter(request.values) else: sform = saveChapter(request.values) in_data = {} if chapter == 0: in_data["date"] = datetime.now() in_data["editdate"] = "" else: in_data["editdate"] = datetime.now() in_data["points"] = [] if request.form and Auth.is_authenticated and (current_user.author == author or current_user.access > 1): f_msg = "" if sform.data: f = request.form points = {} in_pnts = {} in_imgs_pool = {} in_sources_pool = {} ep_text = "" ep_source = "" error = 0 in_data["translated"] = 0 for key in f.keys(): for value in f.getlist(key): value = value.replace("\r\n","<br>") value = value.replace("\n","<br>") if key == "chapter" and chapter == "0": in_data["analyst"] = author in_data["date"] = datetime.now() in_data["namespace"] = namespace in_data["I_S_codename"] = value i_data = DB(g.location).get_a_chapter(namespace, value) # before update/insert if i_data and chapter == 0: error = "This chapter:" + in_data["I_S_codename"] + " already exists!" break else: new_chapter = in_data["I_S_codename"] if key == "I_S_namespace": in_data["I_S_namespace"] = value if key == "title": in_data["I_S_name"] = value in_data["title"] = value if key == "translated" and value != "": in_data["translated"] = 1 if (key == "intro" and value != ""): in_data["intro"] = value if (key == "interpreter" and value != ""): in_data["interpreter"] = value if (key == "interpreter_link" and value != ""): in_data["interpreter_link"] = value if (key == "summary" and value != ""): in_data["summary"] = value if key == "ep_text": ep_text = value if key == "ep_source": ep_source = value if (re.match("point", key) and value != ""): p, k = key.split("_") points[k] = value if key == "I_S_codename" and value.isalpha(): in_data["I_S_codename"] = value in_pnts = value_match_assign("pointHeader", "header", key, value, in_pnts) in_pnts = value_match_assign("pointTitle", "title", key, value, in_pnts) in_pnts = value_match_assign("pointLink", "link", key, value, in_pnts) in_pnts = value_match_assign("pointType", "info_type", key, value, in_pnts) in_pnts = value_match_assign("pointPID", "infoID", key, value, in_pnts) in_pnts = value_match_assign("pointPdate", "info_date", key, value, in_pnts) in_pnts = value_match_assign("pointPauths", "info_authors", key, value, in_pnts) in_pnts = value_match_assign("pointPub", "info_place", key, value, in_pnts) in_pnts = value_match_assign("pointGeo", "info_geo", key, value, in_pnts) in_pnts = value_match_assign("pointImage", "info_img", key, value, in_pnts) in_pnts = value_match_assign("pointIsource", "info_imgSource", key, value, in_pnts) in_pnts = value_match_assign("pointIdescr", "info_imgDescr", key, value, in_pnts) in_pnts = value_match_assign("pointDigest", "digest", key, value, in_pnts) in_pnts = value_match_assign("pointNewID", "num", key, value, in_pnts) in_pnts = value_match_assign("pointHidden", "is_hidden", key, value, in_pnts) if (re.match("pointTags", key) and value != ""): p, r = key.split("_") if (value != ""): lst = value.split(",") c_lst = [] for e in lst: c = e.replace(" ", "") c_lst.append(c) try: in_pnts[r].update({"I_S_codenames": c_lst}) except: in_pnts[r] = {"I_S_codenames": c_lst} if (re.match("pointGeo", key) and value != ""): p, r = key.split("_") if (value != ""): lst = value.split(",") c_lst = [] for e in lst: c = e.replace(" ", "") c_lst.append(c) try: in_pnts[r].update({"info_geo": c_lst}) except: in_pnts[r] = {"info_geo": c_lst} in_imgs_pool = nest_value_match_assign("imgDescr", "info_imgDesc", key, value, in_imgs_pool) in_imgs_pool = nest_value_match_assign("imgLink", "info_img", key, value, in_imgs_pool) in_imgs_pool = nest_value_match_assign("imgSrcURL", "info_imgSource", key, value, in_imgs_pool) in_imgs_pool = nest_value_match_assign("imgSrcTitle", "info_imgTitle", key, value, in_imgs_pool) in_sources_pool = nest_value_match_assign("srcTitle", "title", key, value, in_sources_pool) in_sources_pool = nest_value_match_assign("srcLink", "link", key, value, in_sources_pool) in_sources_pool = nest_value_match_assign("srcID", "infoID", key, value, in_sources_pool) in_sources_pool = nest_value_match_assign("srcDate", "info_date", key, value, in_sources_pool) in_sources_pool = nest_value_match_assign("srcAuth", "info_authors", key, value, in_sources_pool) in_sources_pool = nest_value_match_assign("srcPlace", "info_place", key, value, in_sources_pool) in_sources_pool = nest_value_match_assign("srcType", "info_type", key, value, in_sources_pool) all_tags = set() if error == 0: for r in in_pnts.keys(): try: for ct in in_pnts[r]["I_S_codenames"].split(","): cn = ct.replace(" ", "") all_tags.update(cn) except: pass try: # print in_pnts[r]["info_geo"] for t in in_pnts[r]["info_geo"].split(","): n = t.replace(" ", "") all_tags.update(n) # all_tags.update(in_pnts[r]["info_geo"]) except: pass imgs_pool = [] is_i_pool = 1 try: in_imgs_pool[r].keys() except: is_i_pool = 0 if is_i_pool == 1: for l in in_imgs_pool[r].keys(): try: in_imgs_pool[r][l].update({"num": float(l)}) imgs_pool.append(in_imgs_pool[r][l]) except: if len(in_imgs_pool[r][l]) > 0: in_imgs_pool[r][l].update({"num": float(l)}) imgs_pool = in_imgs_pool[r][l] try: in_pnts[r].update({"img_pool": imgs_pool}) except: in_pnts[r] = {"img_pool": imgs_pool} src_pool= [] is_s_pool = 1 try: in_sources_pool[r].keys() except: is_s_pool = 0 if is_s_pool == 1: for l in in_sources_pool[r].keys(): try: in_sources_pool[r][l].update({"num": float(l)}) src_pool.append(in_sources_pool[r][l]) except: if len(in_imgs_pool[r][l]) > 0: in_sources_pool[r][l].update({"num": float(l)}) src_pool = in_sources_pool[r][l] try: in_pnts[r].update({"sources_pool": src_pool}) except: in_pnts[r] = {"sources_pool": src_pool} for n, p in sorted(in_pnts.iteritems(), key=lambda x: float(x[0])): # https://dzone.com/articles/pymongo-and-key-order try: p["num"] = float(p["num"]) except: p.update({"num": float(n)}) in_data["points"].append(p) if ep_text != "": in_data["epigraph"] = {"text": ep_text, "source": ep_source} for tag in all_tags: # print tag o = DB(g.location).get_an_object_by_codename(tag, namespace) # print tag, list(o) if len(list(o)) == 0: DB(g.location).insert_an_object({"I_S_codename": tag, "I_S_type_this": "", "I_S_type": "", "I_S_name": "", "namespace" : [namespace] }) f_msg = f_msg + "<small><b>" + tag + "</b> is a new tag! <br>Mind to <a style='color:blue' href=https://www.scibook.org/editspace/tags:" + author + ":0> edit the tag list</a>!</small><br>" if chapter == "0": intro = DB(g.location).get_an_intro(namespace) j_intro = json.loads(dumps(intro)) for l in j_intro["langs"]: if l == g.location: if l != "en": I_S = "namespace_" + l else: I_S = "namespace" in_data["I_S_namespace"] = j_intro[I_S].capitalize() DB(g.location).insert_a_chapter(in_data) else: DB(g.location).update_a_chapter(author, namespace, chapter, in_data) app.config['LOGS'].debug( 'Number of points for the chapter \"' + chapter + '\" and location \"' + g.location + '\" : ' + str( len(in_data["points"]) ) ) else: flash(error, category='error') # print all_tags else: error = "Something wrong with data update!" if error == 0: flash(Markup("<div align=left>" + f_msg + "Data updated successfully!</div>"), category='info') else: flash(error, category='error') else: error = "Something wrong with a form or authentification!" flash(error, category='error') if chapter != "0": i_data = DB(g.location).get_a_chapter(namespace, chapter) # after update/insert tmpl = tmpl_picker('form_chapter_edit') return render_template(tmpl, form=g.form, items=i_data) else: if error == 0: if g.location == "en": return redirect("/en/editspace/chapter:" + author + ":" + namespace + ":" + new_chapter) else: return redirect("/editspace/chapter:" + author + ":" + namespace + ":" + new_chapter) else: i_data = in_data tmpl = tmpl_picker('form_chapter') return render_template(tmpl, form=g.form, items=i_data, namespace=namespace)
def save_intro(author, namespace): i_data = {} if namespace == "0": sform = newIntro(request.values) else: sform = saveIntro(request.values) i_data = DB(g.location).get_an_intro(namespace) # before update/insert in_data = {} in_data["points"] = [] in_data["refs"] = [] error = 0 new_namespace = "" try: langs = i_data["langs"] except: langs = ["ru", "en"] # print langs # {{ form.hidden_tag() }} must be in template for sform.validate_on_submit() True if fields are ok if request.form and Auth.is_authenticated and (current_user.author == author or current_user.access > 1): if sform.data: f = request.form points = {} codenames = {} acronyms = {} refs = [] in_refs = {} in_refs_pool = {} ep_text = "" ep_source = "" published = 0 for key in f.keys(): for value in f.getlist(key): # print key,":",value if key == "namespace" and namespace == "0": in_data["namespace"] = value i_data = DB(g.location).get_an_intro(in_data["namespace"]) # before update/insert if i_data and namespace == 0: error = "This namespace:" + in_data["namespace"] + " already exists!" break else: new_namespace = in_data["namespace"] if key == "subject": in_data["subject"] = value if (key == "intro" and value != ""): in_data["intro"] = value if (key == "ref_intro" and value != ""): in_data["ref_intro"] = value if (key == "ref_header" and value != ""): in_data["ref_header"] = value if (key == "summary" and value != ""): in_data["summary"] = value if key == "ep_text": ep_text = value if key == "published": published = value if key == "ep_source": ep_source = value if (re.match("points", key) and value != ""): p, k = key.split("_") points[k] = value if (re.match("namespace_", key) and value.encode("utf-8") != ""): p, k = key.split("_") try: dg = int(k) codenames[k] = value except: dg = "xx" if k != dg: in_data[key] = value if (re.match("acronym_", key) and value != ""): p, k = key.encode("utf-8").split("_") langs.append(value) acronyms[k] = value in_refs = value_match_assign("refBlockTtl", "ref_title", key, value, in_refs) in_refs = value_match_assign("refBlockDigest", "ref_digest", key, value, in_refs) in_refs = value_match_assign("refBlockType", "ref_type", key, value, in_refs) in_refs_pool = nest_value_match_assign("linkTitle", "title", key, value, in_refs_pool) in_refs_pool = nest_value_match_assign("linkURL", "link", key, value, in_refs_pool) in_refs_pool = nest_value_match_assign("linkType", "linktype", key, value, in_refs_pool) in_refs_pool = nest_value_match_assign("linkDigest", "digest", key, value, in_refs_pool) in_refs_pool = nest_value_match_assign("linkAuthor", "author", key, value, in_refs_pool) if error == 0: for r in in_refs.keys(): ref_pool = [] for l in in_refs_pool[r].keys(): # print l, in_refs_pool[r][l] try: ref_pool.append(in_refs_pool[r][l]) except: if len(in_refs_pool[r][l]) > 0: ref_pool = in_refs_pool[r][l] try: in_refs[r].update({"ref_pool": ref_pool}) except: in_refs[r] = {"ref_pool": ref_pool} for n, p in sorted(points.iteritems(), key=lambda x: float(x[0])): in_data["points"].append({"num": float(n), "item": p}) for n, rf in sorted(in_refs.iteritems()): try: refs.append(in_refs[n]) except: if (len(in_refs[n])) > 0: refs = in_refs[n] in_data["refs"] = refs if ep_text != "": in_data["epigraph"] = {"text": ep_text, "source": ep_source} in_data["langs"] = langs in_data["published"] = int(published) for k, v in codenames.iteritems(): in_data["namespace_" + acronyms[k]] = codenames[k] if namespace == "0": in_data["date"] = datetime.now() in_data["analyst"] = author DB(g.location).insert_an_intro(in_data) else: DB(g.location).update_an_intro(author, namespace, in_data) else: flash(error, category='error') else: error = "Something wrong with data update!" if error == 0: flash("Data updated successfully!", category='info') else: flash(error, category='error') else: error = "Something wrong with the form or authentification!" flash(error, category='error') if namespace != "0": i_data = DB(g.location).get_an_intro(namespace) # after update/insert tmpl = tmpl_picker('form_intro_edit') namespaces = {} for l in i_data["langs"]: if l != "en": namespaces[l] = i_data["namespace_" + l] return render_template(tmpl, form=g.form, items=i_data, namespaces=namespaces) else: if error == 0: if g.location == "en": return redirect("/en/editspace/intro:" + author + ":" + new_namespace + "") else: return redirect("/editspace/intro:" + author + ":" + new_namespace + "") else: i_data = in_data itmpl = tmpl_picker('form_intro') return render_template(itmpl, form=g.form, items=i_data)