Added csv export of collection users
This commit is contained in:
parent
1ce84db59c
commit
a605795578
|
@ -1,4 +1,10 @@
|
||||||
|
import csv
|
||||||
|
from datetime import datetime
|
||||||
|
import io
|
||||||
|
import os
|
||||||
|
|
||||||
from flask import Blueprint, render_template, request
|
from flask import Blueprint, render_template, request
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
from app.models.collection import Collection
|
from app.models.collection import Collection
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
|
@ -39,3 +45,22 @@ def delete_collection(id: int):
|
||||||
return "<small class=\"form-text\">Здесь ничего нет</small>", 200
|
return "<small class=\"form-text\">Здесь ничего нет</small>", 200
|
||||||
|
|
||||||
return "", 200
|
return "", 200
|
||||||
|
|
||||||
|
@collections.route("/<int:id>/csv", methods=["GET"])
|
||||||
|
def export_users_to_csv(id: int):
|
||||||
|
collection: Collection = Collection.query.get_or_404(id)
|
||||||
|
|
||||||
|
if len(collection.users) <= 0:
|
||||||
|
return "", 204
|
||||||
|
|
||||||
|
filename = f"{collection.name} - {datetime.now()}.csv"
|
||||||
|
|
||||||
|
fieldnames = collection.users[0].to_dict().keys()
|
||||||
|
si = io.StringIO()
|
||||||
|
|
||||||
|
writer = csv.DictWriter(si, fieldnames)
|
||||||
|
|
||||||
|
writer.writeheader()
|
||||||
|
writer.writerows(map(lambda user: user.to_dict(), collection.users))
|
||||||
|
|
||||||
|
return si.getvalue(), 200, {'Content-Disposition': f'attachment; filename={filename}', 'Content-type': 'text/csv'}
|
|
@ -4,7 +4,9 @@
|
||||||
<ul class="list-group list-group-flush">
|
<ul class="list-group list-group-flush">
|
||||||
<li class="list-group-item">Кол-во пользрвателей: {{ collection.users|length }}</li>
|
<li class="list-group-item">Кол-во пользрвателей: {{ collection.users|length }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
<form action="/api/collections/{{ collection.id }}" method="DELETE" class="card-footer px-2 pt-2">
|
<div class="card-footer px-2 pt-2">
|
||||||
|
<div class="d-flex gap-2" role="group">
|
||||||
|
<form action="/api/collections/{{ collection.id }}" method="DELETE">
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
hx-delete="/api/collections/{{ collection.id }}"
|
hx-delete="/api/collections/{{ collection.id }}"
|
||||||
|
@ -16,5 +18,13 @@
|
||||||
Удалить
|
Удалить
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
<a
|
||||||
|
href="/api/collections/{{ collection.id }}/csv"
|
||||||
|
class="btn btn-outline-success"
|
||||||
|
>
|
||||||
|
Экспорт
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
|
@ -20,3 +20,12 @@ class User(db.Model):
|
||||||
|
|
||||||
def exist(username: str, collection):
|
def exist(username: str, collection):
|
||||||
return not User.query.filter_by(username=username, collection=collection).first() is None
|
return not User.query.filter_by(username=username, collection=collection).first() is None
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'first_name': self.first_name,
|
||||||
|
'last_name': self.last_name,
|
||||||
|
'phone': self.phone,
|
||||||
|
'username': self.username,
|
||||||
|
}
|
Loading…
Reference in New Issue