async def profile(request: Request): if not request.session.get("user"): raise HTTPException(404) user = request.session["user"] context = {"request": request, "user": user} if request.method == "GET": if request.url.path.endswith(":edit"): return render("users/profile_editor.html", context) else: return render("users/profile.html", context) if request.method == "POST": form_values = await request.form() # form_errors = validate_input(model="user", values=dict(form_values)) form_errors = False if form_errors: context["form_values"] = dict(form_values) context["form_errors"] = form_errors # render profile_editor with values and error-messages return render("profile_editor.html", context, status_code=400) values = {"name": form_values.get("username")} # await queries.update_user(user["openid"], values=values) # else redirect to profile-page return RedirectResponse( url=request.url_for("profile"), status_code=303 )
async def searches(request: Request): if not request.session.get("user"): raise HTTPException(404) user = request.session["user"] searches = user.get("searches", []) if request.method == "GET": context = {"request": request, "user": user} # searches = await queries.get_searches(user["openid"]) if searches: context["searches"] = searches return render("users/searches.html", context) if request.method == "POST": search = await request.json() url = search.get("url") if not url: return JSONResponse({"error": "Manglende søgestreng"}) if url in searches: return JSONResponse({"error": "Søgningen var allerede gemt"}) # update db # search = {"user_id": user["openid"], "url": url, "description": search.get("description")} # await queries.insert_search(search) # update session user["searches"] = searches.extend(url) # generate response response = JSONResponse({"msg": "Søgning gemt"}) response.set_cookie("user", user) return response
async def search(req: Request): context = {"request": req} context["user"] = req.session.get("user") resp = api.list_resources(req.query_params) # If json-req if req.query_params.get("fmt", "") == "json": return JSONResponse(resp) # If SAM-req. It only wants json-encoded id-lists if "ids" in req.query_params.getlist("view"): return JSONResponse(resp) # If errors, show error-page if resp.get("errors"): context["errors"] = resp.get("errors") return render("error.html", context) # enables (and resets) traversal if resp.get("size") > 1 and resp.get("result"): ses = req.session params = MultiDict(req.query_params) params.pop("start", None) cur_search = QueryParams(params).__str__() if ses.get("traverse") and cur_search == ses["traverse"].get( "cur_search"): pass else: ses["traverse"] = {} ses["traverse"]["total"] = resp.get("total") ses["traverse"]["size"] = resp.get("size") ses["traverse"]["cur_search"] = cur_search ses["traverse"]["batches"] = {} start = str(resp.get("start", 0)) ses["traverse"]["start"] = int(start) if start not in ses["traverse"]["batches"]: ses["traverse"]["batches"][start] = [ int(d.get("id")) for d in resp.get("result") ] ses["traverse"]["cur_ids"] = ses["traverse"]["batches"].get(start) context.update(resp) return render("search.html", context)
async def bookmarks(request: Request): user = request.session["user"] if not user: raise HTTPException(404) bookmarks = user.get("bookmarks") or [] if request.method == "GET": context = {"request": request, "user": user} if bookmarks: context["bookmarks"] = bookmarks # fetch full records # context["bookmarks"] = await api.get_records(id_list=bookmarks) return render("users/bookmarks.html", context) if request.method == "POST": bookmark = await request.json() resource_id = bookmark.get("resource_id") if not resource_id: return JSONResponse({"error": "Manglende materialeID"}) if resource_id in bookmarks: return JSONResponse({"error": "Materialet var allerede bogmærket"}) # update db # bookmark = {"user_id": user["openid"], "resource_id": resource_id} # await queries.insert_bookmark(bookmark) # update session user["bookmarks"] = bookmarks.extend(resource_id) # generate response response = JSONResponse({"msg": "Bogmærke tilføjet"}) response.set_cookie("user", user) return response
async def clear(req: Request): req.session.clear() return render("clear.html", {"request": req})
async def welcome(req: Request): return render("welcome.html", {"request": req})
async def about(req: Request): return render("about.html", {"request": req})
async def index(req: Request): return render("index.html", {"request": req})