generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from moevm/bulk_import
User impex added
- Loading branch information
Showing
14 changed files
with
245 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<template> | ||
<div class="container mt-3"> | ||
<ul id="myTab" class="nav nav-tabs" role="tablist"> | ||
<li class="nav-item" role="presentation"> | ||
<button | ||
id="home-tab" | ||
class="nav-link active" | ||
data-bs-toggle="tab" | ||
data-bs-target="#maps" | ||
> | ||
Экспорт | ||
</button> | ||
</li> | ||
<li class="nav-item" role="presentation"> | ||
<button | ||
id="profile-tab" | ||
class="nav-link" | ||
data-bs-toggle="tab" | ||
data-bs-target="#import" | ||
> | ||
Импорт | ||
</button> | ||
</li> | ||
</ul> | ||
<div class="tab-content mt-3"> | ||
<div id="maps" class="tab-pane fade show active"> | ||
<div class="m-auto col-10 col-lg-6 text-center"> | ||
<button class="btn btn-primary fs-3" @click="download"> | ||
Загрузить | ||
<i class="bi bi-file-earmark-arrow-down fw-bold" /> | ||
</button> | ||
</div> | ||
</div> | ||
<div id="import" class="tab-pane fade"> | ||
<div class="m-auto col-10 col-lg-6"> | ||
<FormKit type="form" :actions="false" @submit="upload"> | ||
<FormKit | ||
name="files" | ||
type="file" | ||
accept=".json" | ||
multiple="false" | ||
label="Дамп" | ||
validation="required" | ||
validation-visibility="live" | ||
/> | ||
<FormKit | ||
outer-class="text-end" | ||
input-class="$reset btn btn-success" | ||
type="submit" | ||
label="Загрузить" | ||
/> | ||
</FormKit> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { FormKitGroupValue } from "@formkit/core"; | ||
import { useToaster } from "@/store/toaster"; | ||
import { ToastTypes } from "@/config/toast"; | ||
import { ref } from "vue"; | ||
import { saveAs } from "file-saver"; | ||
import { DumpsApi } from "@/components/routes/dumps/api"; | ||
const toaster = useToaster(); | ||
const files = ref<{ name: string; file: File }[]>([]); | ||
async function download() { | ||
const data = (await DumpsApi.downloadDump()).data; | ||
const blob = new Blob([JSON.stringify(data)], { | ||
type: "text/plain;charset=utf-8", | ||
}); | ||
saveAs(blob, "dump.json"); | ||
} | ||
async function upload(data: FormKitGroupValue) { | ||
try { | ||
await DumpsApi.uploadDump((data.files as { file: File }[])[0].file); | ||
toaster.addToast({ | ||
title: "Информация", | ||
body: "Дамп загружен успешно", | ||
type: ToastTypes.success, | ||
}); | ||
} catch (e) { | ||
toaster.addToast({ | ||
title: "Информация", | ||
body: "Не удалось загрузить Дамп", | ||
type: ToastTypes.danger, | ||
}); | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped lang="scss"></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { api } from "@/api"; | ||
import { Dump } from "@/components/routes/dumps/types"; | ||
|
||
export function downloadDump() { | ||
return api.get<Dump>("/dumps/"); | ||
} | ||
|
||
export function uploadDump(file: File) { | ||
const formData = new FormData(); | ||
formData.append("dump", file); | ||
return api.post("/dumps/", formData); | ||
} | ||
|
||
export const DumpsApi = { downloadDump, uploadDump }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ObjectInfo } from "@/types/objects"; | ||
import { User } from "@/types/users"; | ||
|
||
export interface Dump { | ||
objects: ObjectInfo[]; | ||
users: User[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<template> | ||
<DumpComponent /> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import DumpComponent from "@/components/routes/dumps/DumpComponent.vue"; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from flask import request | ||
from flask_login import login_required | ||
from flask_restx import Namespace, Resource | ||
from werkzeug.local import LocalProxy | ||
import json | ||
|
||
from app.db import get_db | ||
from app.services.objects import bulk_upload_objects | ||
from app.services.user import bulk_upload_users | ||
from app.utils import parse_json | ||
|
||
db = LocalProxy(get_db) | ||
|
||
api = Namespace("dumps", description="Дампы") | ||
|
||
|
||
@api.route('/') | ||
class DumpResource(Resource): | ||
def get(self): | ||
return parse_json({ | ||
'objects': db.objects.find({}), | ||
'users': db.users.find({}) | ||
}) | ||
|
||
@login_required | ||
def post(self): | ||
dump = json.load(request.files['dump']) | ||
bulk_upload_objects(dump['objects']) | ||
bulk_upload_users(dump['users']) | ||
return "Ok" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from werkzeug.local import LocalProxy | ||
|
||
from app.db import get_db | ||
|
||
db = LocalProxy(get_db) | ||
|
||
|
||
def bulk_upload_objects(new_objects): | ||
object_keys = ["_id", "type", "name", "color", "update", "coordinates", "center"] | ||
for obj in new_objects: | ||
for k in object_keys: | ||
if k not in obj: | ||
raise Exception('invalid object property') | ||
del obj['_id'] | ||
|
||
if len(new_objects): | ||
db.objects.insert_many(new_objects) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters