Skip to content

Commit

Permalink
feat(ltFileDetails): save all
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Aug 13, 2024
1 parent 1d09674 commit e8bcb30
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
path: ./dist
- uses: peaceiris/actions-gh-pages@v3
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
if: github.event_name == 'push' && github.ref == 'refs/heads/save-all'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
Expand Down
5 changes: 2 additions & 3 deletions app/api/ltDataAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ export function getUserInfo() {
}).json();
}

export function editLocation(title: CommonsFile, coordinates: LatLng) {
export function editLocation(title: CommonsFile, coordinates: LatLng[]) {
const {pageid} = title;
const {type, lat, lng} = coordinates;
return useFetch<EditApiResponse>(
'/edit',
{
Expand All @@ -32,7 +31,7 @@ export function editLocation(title: CommonsFile, coordinates: LatLng) {
'Content-Type': 'application/json',
'X-XSRF-TOKEN': xsrfToken() || ''
},
body: JSON.stringify({type, lat, lng, pageid})
body: JSON.stringify(coordinates.map(({type, lat, lng}) => ({type, lat, lng, pageid})))
},
{
afterFetch(ctx) {
Expand Down
40 changes: 31 additions & 9 deletions app/components/ltFileDetails.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<template>
<div class="card">
<button
class="btn btn-success form-control"
type="button"
:disabled="!(file.coordinates?.isChanged || file.objectLocation?.isChanged)"
@click="editLocation()"
>
<svg class="octicon">
<use xlink:href="#git-commit"></use>
</svg>
{{ t('Save') }}
</button>
<div class="card mt-2">
<div class="card-body p-2">
<div>
<label @click="collapseCameraLocation = !collapseCameraLocation">
Expand Down Expand Up @@ -117,17 +128,28 @@ const msgErrorStatusText = computed(() =>
watch(props.file.coordinates, (c1, c2) => console.log(c1, c2));
function editLocation(coordinates: LatLng) {
function editLocation(coordinates: LatLng[]) {
error.value = undefined;
if (coordinates === undefined) {
coordinates = [];
if (props.file?.coordinates?.isChanged) {
coordinates.push(props.file.coordinates);
}
if (props.file?.objectLocation?.isChanged) {
coordinates.push(props.file.objectLocation);
}
}
return ltDataAuth.editLocation(props.file, coordinates).then(
() => {
if (coordinates.type === 'Location') {
// eslint-disable-next-line vue/no-mutating-props
props.file.coordinates = coordinates.commit();
} else if (coordinates.type === 'Object location') {
// eslint-disable-next-line vue/no-mutating-props
props.file.objectLocation = coordinates.commit();
}
coordinates.forEach(c => {
if (c.type === 'Location') {
// eslint-disable-next-line vue/no-mutating-props
props.file.coordinates = c.commit();
} else if (c.type === 'Object location') {
// eslint-disable-next-line vue/no-mutating-props
props.file.objectLocation = c.commit();
}
});
},
e => {
error.value = e;
Expand Down
4 changes: 2 additions & 2 deletions app/components/ltLocationInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"
/>
<button
class="btn btn-secondary"
class="btn btn-outline-secondary"
type="button"
:title="t('Discard')"
@click="modelValue && emit('update:modelValue', modelValue.rollback())"
Expand All @@ -22,7 +22,7 @@
</svg>
</button>
<button
class="btn btn-success"
class="btn btn-outline-success"
type="button"
:title="t('Save')"
:disabled="!modelValue?.isChanged"
Expand Down
51 changes: 34 additions & 17 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
import typing

from flask import Flask, abort, jsonify, request
from flask_mwoauth import MWOAuth
Expand Down Expand Up @@ -49,24 +50,28 @@ def user():
return r


LocationType = typing.Literal["Location", "Object location"]


class EditRequest(typing.TypedDict):
type: LocationType
lat: int
lng: int
pageid: int


@app.route("/edit", methods=["POST"])
def edit():
if not mwoauth.get_current_user():
abort(401)

data = request.get_json()
if (
"pageid" not in data
or "type" not in data
or "lat" not in data
or "lng" not in data
data: list[EditRequest] = request.get_json()
if not data or not all(
"pageid" in d or "type" in d or "lat" in d or "lng" in d for d in data
):
abort(400)

pageid = int(data["pageid"])
type = data["type"]
lat = float(data["lat"])
lng = float(data["lng"])
pageid = int(data[0]["pageid"])
app.logger.info("Received request %s", str(data))

r1: QueryResult = mwoauth_request(
Expand All @@ -92,20 +97,32 @@ def edit():
wikitext = to_unicode(wikitext)
except KeyError:
abort(404)
new_wikitext = add_location_to_wikitext(type, lat, lng, wikitext)

for d in data:
wikitext = add_location_to_wikitext(
type=d["type"],
lat=float(d["lat"]),
lng=float(d["lng"]),
wikitext=wikitext,
)
edit_mediainfo(
type=d["type"],
lat=float(d["lat"]),
lng=float(d["lng"]),
page=page,
token=token,
)

r2 = mwoauth_request(
format="json",
action="edit",
pageid=str(pageid),
summary="{{%s}}" % type,
text=new_wikitext,
summary=", ".join("{{%s}}" % d["type"] for d in data),
text=wikitext,
token=token,
)

r3 = edit_mediainfo(type, lat, lng, page, token)

return jsonify(result=r2, sdc=r3)
return jsonify(result=r2)


def get_mediainfo(page: Page) -> Mediainfo:
Expand All @@ -119,7 +136,7 @@ def get_mediainfo(page: Page) -> Mediainfo:
return None


def edit_mediainfo(type, lat: float, lng: float, page: Page, token: str):
def edit_mediainfo(type: LocationType, lat: float, lng: float, page: Page, token: str):
mediainfo = get_mediainfo(page)
if not mediainfo:
return None
Expand Down

0 comments on commit e8bcb30

Please sign in to comment.