Fix curation

This commit is contained in:
Daoud Clarke 2023-10-25 23:12:41 +01:00
parent 4d823497a6
commit 911b243239
8 changed files with 46 additions and 49 deletions

View file

@ -8,7 +8,7 @@
export default {
componentPrefix: 'mwmbl',
publicApiURL: 'https://api.mwmbl.org/',
publicApiURL: '/api/v1/',
// publicApiURL: 'http://localhost:5000/',
searchQueryParam: 'q',
footerLinks: [

View file

@ -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>

View file

@ -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: {

View file

@ -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);
}

View file

@ -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>

View file

@ -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

View file

@ -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,

View file

@ -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