def upldpic(): """ Form submit and monitoring for uploading a point pic. """ # flask.request.method always returns "GET", so check for file input. picfile = flask.request.files.get("picfilein") if not picfile: logging.info("upldpic ready for upload") return util.respond("Ready", mimetype="text/plain") try: appuser, _ = util.authenticate() ptid = dbacc.reqarg("ptid", "dbid", required=True) pt = dbacc.cfbk("Point", "dsId", ptid, required=True) logging.info(appuser["email"] + " upldpic Point " + str(ptid)) if not appuser["dsId"] in util.csv_to_list(pt["editors"]): raise ValueError("Not authorized to edit this point") img = Image.open(picfile) img = ImageOps.exif_transpose(img) # correct vertical orientation sizemaxdims = 400, 400 # max allowed width/height for thumbnail resize img.thumbnail(sizemaxdims) # modify, preserving aspect ratio bbuf = io.BytesIO() # file-like object for save img.save(bbuf, format="PNG") pt["pic"] = base64.b64encode(bbuf.getvalue()) pt = dbacc.write_entity(pt, pt["modified"]) except ValueError as e: logging.info("upldpic Point " + str(ptid) + ": " + str(e)) return util.serve_value_error(e) return util.respond("Done: " + pt["modified"], mimetype="text/plain")
def updpt(): """ Standard app POST call to update a Point. """ try: appuser, _ = util.authenticate() fields = [ "dsId", "dsType", "modified", "editors", "srctl", "source", "date", "text", "refs", "qtype", "communities", "regions", "categories", "tags", "srclang", "translations", "stats" ] ptdat = util.set_fields_from_reqargs(fields, {}) dbpt = verify_edit_authorization(appuser, ptdat) if dbpt: dbst = dbpt.get("srctl") if dbst and (dbst != ptdat.get("srctl")): raise ValueError("Source Timeline may not be changed.") util.fill_missing_fields(fields, dbpt, ptdat) else: # making a new instance for fld in ["srctl", "date", "text"]: if not ptdat.get(fld): # required point field value raise ValueError("Point " + fld + " value is required.") # date format validity checking is done client side remove_html_from_point_fields(ptdat) ptdat["lmuid"] = appuser["dsId"] pt = dbacc.write_entity(ptdat, ptdat.get("modified", "")) except ValueError as e: return util.serve_value_error(e) return util.respJSON(pt)
def updtl(): """ Standard app POST call to update a Timeline. """ try: appuser, _ = util.authenticate() tlfs = [ "dsId", "dsType", "modified", "editors", "name", "slug", "title", "subtitle", "featured", "lang", "comment", "about", "kwds", "ctype", "cids", "rempts", "svs" ] tldat = util.set_fields_from_reqargs(tlfs, {}) # logging.info("updtl received: " + json.dumps(tldat)) tldb = verify_edit_authorization(appuser, tldat) if tldb: util.fill_missing_fields(tlfs, tldb, tldat) util.set_fields_from_reqargs(tlfs, tldat) # for fields set to "" tldat["cname"] = canonize(tldat.get("name", "")) verify_unique_timeline_field(tldat, "cname", tldb) verify_unique_timeline_field(tldat, "slug", tldb) if tldat.get("featured") == "Promoted": if not tldb or (tldb.get("featured") != "Promoted"): raise ValueError("Promoted feature not authorized") update_prebuilt(tldat, tldb) tldat["lmuid"] = appuser["dsId"] tl = dbacc.write_entity(tldat, tldat.get("modified", "")) except ValueError as e: return util.serve_value_error(e) return util.respJSON(tl)
def findcomps(): """ Return completions from other people for the given timeline. """ try: appuser, _ = util.authenticate() tlid = dbacc.reqarg("tlid", "dbid", required=True) where = ("WHERE tlid = " + tlid + " AND userid != " + appuser["dsId"] + " ORDER BY modified DESC LIMIT 50") tlcs = dbacc.query_entity("TLComp", where) except ValueError as e: return util.serve_value_error(e) return util.respJSON(tlcs)
def notecomp(): """ Note Timeline completion in TLComp instance. """ try: appuser, token = util.authenticate() tlc = { "dsType": "TLComp", "userid": appuser["dsId"], "username": appuser["name"] } tlc = util.set_fields_from_reqargs( ["tlid", "tlname", "tltitle", "tlsubtitle"], tlc) proginst = pop_proginst_from_started(appuser, tlc["tlid"]) tlc["data"] = json.dumps(proginst) tlc = dbacc.write_entity(tlc) push_or_update_completion(appuser, tlc, proginst) appuser = dbacc.write_entity(appuser, appuser["modified"]) dbacc.entcache.cache_put(appuser) # ensure cache has latest except ValueError as e: return util.serve_value_error(e) return util.respJSON([appuser, token], audience="private")