Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Fix curation

Daoud Clarke911b243239d8
diff --git a/front-end/config.js b/front-end/config.js
index 1c7c23c..e7abff4 100644
--- a/front-end/config.js
+++ b/front-end/config.js
@@ -8,7 +8,7 @@
export default {
componentPrefix: 'mwmbl',
- publicApiURL: 'https://api.mwmbl.org/',
+ publicApiURL: '/api/v1/',
// publicApiURL: 'http://localhost:5000/',
searchQueryParam: 'q',
footerLinks: [
diff --git a/front-end/src/components/app.js b/front-end/src/components/app.js
index d53cb3b..91ef881 100644
--- a/front-end/src/components/app.js
+++ b/front-end/src/components/app.js
@@ -7,8 +7,9 @@ const template = () => /*html*/`
<ul>
<li is="${save}"></li>
</ul>
+ <div><a href="/accounts/login/">Login</a> <a href="/accounts/signup/">Sign up</a> </div>
<div class="branding">
- <img class="brand-icon" src="/images/logo.svg" width="40" height="40" alt="mwmbl logo">
+ <img class="brand-icon" src="/static/images/logo.svg" width="40" height="40" alt="mwmbl logo">
<span class="brand-title">MWMBL</span>
</div>
<mwmbl-search-bar></mwmbl-search-bar>
diff --git a/front-end/src/components/organisms/results.js b/front-end/src/components/organisms/results.js
index 37fefe5..9cd9993 100644
--- a/front-end/src/components/organisms/results.js
+++ b/front-end/src/components/organisms/results.js
@@ -96,6 +96,7 @@ export default define('results', class extends HTMLElement {
detail: {
type: 'delete',
data: {
+ timestamp: Date.now(),
url: document.location.href,
results: newResults,
curation: {
@@ -121,6 +122,7 @@ export default define('results', class extends HTMLElement {
detail: {
type: 'validate',
data: {
+ timestamp: Date.now(),
url: document.location.href,
results: newResults,
curation: e.detail.data
@@ -156,6 +158,7 @@ export default define('results', class extends HTMLElement {
detail: {
type: 'add',
data: {
+ timestamp: Date.now(),
url: document.location.href,
results: newResults,
curation: {
@@ -183,8 +186,10 @@ export default define('results', class extends HTMLElement {
detail: {
type: 'begin',
data: {
+ timestamp: Date.now(),
url: document.location.href,
- results: results
+ results: results,
+ curation: {}
}
}
});
@@ -219,6 +224,7 @@ export default define('results', class extends HTMLElement {
detail: {
type: 'move',
data: {
+ timestamp: Date.now(),
url: document.location.href,
results: newResults,
curation: {
diff --git a/front-end/src/components/organisms/save.js b/front-end/src/components/organisms/save.js
index dc2f883..38f2dbe 100644
--- a/front-end/src/components/organisms/save.js
+++ b/front-end/src/components/organisms/save.js
@@ -4,7 +4,7 @@ import config from "../../../config.js";
const CURATION_KEY_PREFIX = "curation-";
-const CURATION_URL = config.publicApiURL + "user/curation/";
+const CURATION_URL = config.publicApiURL + "curation/";
const template = () => /*html*/`
@@ -65,12 +65,12 @@ export default define('save', class extends HTMLLIElement {
return;
}
this.sending = true;
- const auth = document.cookie
+ const csrftoken = document.cookie
.split('; ')
- .find((row) => row.startsWith('jwt='))
+ .find((row) => row.startsWith('csrftoken='))
?.split('=')[1];
- if (!auth) {
+ if (!csrftoken) {
console.log("No auth");
return;
}
@@ -81,20 +81,14 @@ export default define('save', class extends HTMLLIElement {
console.log("Value", value);
const url = CURATION_URL + value['type'];
- let data = value['data'];
- if (value.type !== 'begin') {
- if (this.currentCurationId === null) {
- throw ReferenceError("No current curation found");
- }
- data['curation_id'] = this.currentCurationId;
- }
- data['auth'] = auth;
-
+ const data = value['data'];
console.log("Data", data);
const response = await fetch(url, {
method: 'POST',
cache: 'no-cache',
- headers: {'Content-Type': 'application/json'},
+ headers: {'Content-Type': 'application/json', 'X-CSRFToken': csrftoken},
+ credentials: "same-origin",
+ mode: "same-origin",
body: JSON.stringify(data),
});
@@ -109,10 +103,6 @@ export default define('save', class extends HTMLLIElement {
const responseData = await response.json();
console.log("Response data", responseData);
- if (responseData["curation_id"]) {
- this.currentCurationId = responseData["curation_id"];
- }
-
// There may be more to send, wait a second and see
setTimeout(this.__sendToApi.bind(this), 1000);
}
diff --git a/front-end/src/index.html b/front-end/src/index.html
index 843bfc8..65bacda 100644
--- a/front-end/src/index.html
+++ b/front-end/src/index.html
@@ -44,12 +44,12 @@
</head>
<body>
- <mwmbl-login></mwmbl-login>
- <mwmbl-register></mwmbl-register>
+<!-- <mwmbl-login></mwmbl-login>-->
+<!-- <mwmbl-register></mwmbl-register>-->
<mwmbl-app></mwmbl-app>
<noscript>
<main class="noscript">
- <img class="brand-icon" src="/images/logo.svg" width="40" height="40" alt="mwmbl logo">
+ <img class="brand-icon" src="/static/images/logo.svg" width="40" height="40" alt="mwmbl logo">
<h1>
Welcome to mwmbl, the free, open-source and non-profit search engine.
</h1>
diff --git a/mwmbl/api.py b/mwmbl/api.py
index bb2fe75..5fe1129 100644
--- a/mwmbl/api.py
+++ b/mwmbl/api.py
@@ -3,6 +3,7 @@ from pathlib import Path
from django.conf import settings
from ninja import NinjaAPI
+from ninja.security import django_auth
import mwmbl.crawler.app as crawler
from mwmbl.indexer.batch_cache import BatchCache
@@ -35,7 +36,7 @@ def create_api(version):
api.add_router("/crawler/", crawler_router)
curation_router = curate.create_router(index_path)
- api.add_router("/curation/", curation_router)
+ api.add_router("/curation/", curation_router, auth=django_auth)
return api
diff --git a/mwmbl/platform/curate.py b/mwmbl/platform/curate.py
index 92b1ebd..b9aedaa 100644
--- a/mwmbl/platform/curate.py
+++ b/mwmbl/platform/curate.py
@@ -1,17 +1,15 @@
-import json
-from urllib.parse import urljoin, parse_qs
+from typing import Any
+from urllib.parse import parse_qs
-import requests
from ninja import Router
-from ninja.security import django_auth
from mwmbl.indexer.update_urls import get_datetime_from_timestamp
from mwmbl.models import UserCuration
-from mwmbl.platform.data import CurateBegin, CurateMove, CurateDelete, CurateAdd, CurateValidate, Curation
+from mwmbl.platform.data import CurateBegin, CurateMove, CurateDelete, CurateAdd, CurateValidate, \
+ make_curation_type
from mwmbl.tinysearchengine.indexer import TinyIndex, Document
from mwmbl.tokenizer import tokenize
-
RESULT_URL = "https://mwmbl.org/?q="
MAX_CURATED_SCORE = 1_111_111.0
@@ -19,30 +17,30 @@ MAX_CURATED_SCORE = 1_111_111.0
def create_router(index_path: str) -> Router:
router = Router(tags=["user"])
- @router.post("/begin", auth=django_auth)
- def user_begin_curate(request, curate_begin: CurateBegin):
+ @router.post("/begin")
+ def user_begin_curate(request, curate_begin: make_curation_type(CurateBegin)):
return _curate(request, "curate_begin", curate_begin)
- @router.post("/move", auth=django_auth)
- def user_move_result(request, curate_move: Curation[CurateMove]):
+ @router.post("/move")
+ def user_move_result(request, curate_move: make_curation_type(CurateMove)):
return _curate(request, "curate_move", curate_move)
- @router.post("/delete", auth=django_auth)
- def user_delete_result(request, curate_delete: Curation[CurateDelete]):
+ @router.post("/delete")
+ def user_delete_result(request, curate_delete: make_curation_type(CurateDelete)):
return _curate(request, "curate_delete", curate_delete)
- @router.post("/add", auth=django_auth)
- def user_add_result(request, curate_add: Curation[CurateAdd]):
+ @router.post("/add")
+ def user_add_result(request, curate_add: make_curation_type(CurateAdd)):
return _curate(request, "curate_add", curate_add)
- @router.post("/validate", auth=django_auth)
- def user_add_result(request, curate_validate: Curation[CurateValidate]):
+ @router.post("/validate")
+ def user_add_result(request, curate_validate: make_curation_type(CurateValidate)):
return _curate(request, "curate_validate", curate_validate)
- def _curate(request, curation_type: str, curation: Curation):
+ def _curate(request, curation_type: str, curation: Any):
user_curation = UserCuration(
user=request.user,
- timestamp=get_datetime_from_timestamp(curation.timestamp),
+ timestamp=get_datetime_from_timestamp(curation.timestamp / 1000.0),
url=curation.url,
results=curation.dict()["results"],
curation_type=curation_type,
diff --git a/mwmbl/platform/data.py b/mwmbl/platform/data.py
index 1f0b0c1..93b8f41 100644
--- a/mwmbl/platform/data.py
+++ b/mwmbl/platform/data.py
@@ -37,9 +37,10 @@ class CurateValidate(Schema):
T = TypeVar('T', CurateBegin, CurateAdd, CurateDelete, CurateMove, CurateValidate)
-class Curation(Schema, Generic[T]):
- timestamp: int
- url: str
- results: list[Result]
- curation: T
-
+def make_curation_type(t):
+ class Curation(Schema):
+ timestamp: int
+ url: str
+ results: list[Result]
+ curation: t
+ return Curation