Compare commits

..

2 Commits

4 changed files with 58 additions and 14 deletions

View File

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

View File

@ -4,17 +4,27 @@
<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">
<button <div class="d-flex gap-2" role="group">
type="submit" <form action="/api/collections/{{ collection.id }}" method="DELETE">
hx-delete="/api/collections/{{ collection.id }}" <button
hx-swap="outerHTML" type="submit"
hx-target='[data-collection-id="{{ collection.id }}"]' hx-delete="/api/collections/{{ collection.id }}"
hx-confirm="Вы уверены, что хотите удалить коллекцию?" hx-swap="outerHTML"
class="btn btn-outline-danger" hx-target='[data-collection-id="{{ collection.id }}"]'
> hx-confirm="Вы уверены, что хотите удалить коллекцию?"
Удалить class="btn btn-outline-danger"
</button> >
</form> Удалить
</button>
</form>
<a
href="/api/collections/{{ collection.id }}/csv"
class="btn btn-outline-success"
>
Экспорт
</a>
</div>
</div>
</div> </div>
</div> </div>

View File

@ -59,7 +59,7 @@ def delete_task(task_id: int):
db.session.delete(task) db.session.delete(task)
db.session.commit() db.session.commit()
return '', 204 return '', 200
@tasks.route("/<int:task_id>/stop", methods=["PUT"]) @tasks.route("/<int:task_id>/stop", methods=["PUT"])
def stop_task(task_id: int): def stop_task(task_id: int):

View File

@ -19,4 +19,13 @@ class User(db.Model):
collection: Mapped["Collection"] = relationship("Collection", back_populates="users") collection: Mapped["Collection"] = relationship("Collection", back_populates="users")
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,
}