From 2f67e1cea6aca2345ce9d7ea27a62646188f2844 Mon Sep 17 00:00:00 2001 From: winet Date: Fri, 8 Mar 2024 16:39:04 +1000 Subject: [PATCH] changes --- app/blueprints/api/collections/routes.py | 22 +++++++++- .../collections/templates/collections/card.j2 | 36 ++++++++-------- .../collections/collections_section.j2 | 2 + .../templates/collections/create_form.j2 | 33 +++++++++++++++ .../collections/templates/collections/grid.j2 | 4 +- .../templates/collections/select.j2 | 7 ++++ app/blueprints/api/tasks/routes.py | 4 -- app/blueprints/frontend/routes.py | 4 +- app/blueprints/frontend/templates/parse.j2 | 41 +++++++------------ app/models/collection.py | 7 +++- paper/parser.py | 12 +++--- 11 files changed, 111 insertions(+), 61 deletions(-) create mode 100644 app/blueprints/api/collections/templates/collections/collections_section.j2 create mode 100644 app/blueprints/api/collections/templates/collections/create_form.j2 create mode 100644 app/blueprints/api/collections/templates/collections/select.j2 diff --git a/app/blueprints/api/collections/routes.py b/app/blueprints/api/collections/routes.py index 39c83a9..a64b20b 100644 --- a/app/blueprints/api/collections/routes.py +++ b/app/blueprints/api/collections/routes.py @@ -5,6 +5,26 @@ from app.extensions import db collections = Blueprint("collections", __name__, url_prefix="/collections", template_folder="templates") +@collections.route('/', methods=["POST"]) +def create_collection(): + data = request.form + name = data.get('collection_name') + + if not name: + return render_template('collections/create_form.j2', collections_error='Введите название базы!') + + if Collection.exists(name): + return render_template('collections/create_form.j2', collections_error='База уже существует!') + + swap = 'afterbegin' if len(Collection.query.all()) > 0 else 'innerHTML' + + collection = Collection(name=name) + + db.session.add(collection) + db.session.commit() + + return f"", 200, {'Hx-Reswap': swap, 'HX-Retarget': '#collection-select'} + @collections.route("/", methods=["DELETE"]) def delete_collection(id: int): @@ -18,4 +38,4 @@ def delete_collection(id: int): if length <= 0: return "Здесь ничего нет", 200 - return "", 204 \ No newline at end of file + return "", 200 diff --git a/app/blueprints/api/collections/templates/collections/card.j2 b/app/blueprints/api/collections/templates/collections/card.j2 index 9fcd973..aa7ed66 100644 --- a/app/blueprints/api/collections/templates/collections/card.j2 +++ b/app/blueprints/api/collections/templates/collections/card.j2 @@ -1,18 +1,20 @@ -
-
{{ collection.name }}
-
    -
  • Кол-во пользрвателей: {{ collection.users|length }}
  • -
- +
+
+
{{ collection.name }}
+
    +
  • Кол-во пользрвателей: {{ collection.users|length }}
  • +
+ +
\ No newline at end of file diff --git a/app/blueprints/api/collections/templates/collections/collections_section.j2 b/app/blueprints/api/collections/templates/collections/collections_section.j2 new file mode 100644 index 0000000..f6e3494 --- /dev/null +++ b/app/blueprints/api/collections/templates/collections/collections_section.j2 @@ -0,0 +1,2 @@ +{% include "collections/create_form.j2" %} +{% include "collections/select.j2" %} \ No newline at end of file diff --git a/app/blueprints/api/collections/templates/collections/create_form.j2 b/app/blueprints/api/collections/templates/collections/create_form.j2 new file mode 100644 index 0000000..e45d9e7 --- /dev/null +++ b/app/blueprints/api/collections/templates/collections/create_form.j2 @@ -0,0 +1,33 @@ +
+ + + + {% if collections_error is defined %} +
+ {{ collections_error }} +
+ {% endif %} +
\ No newline at end of file diff --git a/app/blueprints/api/collections/templates/collections/grid.j2 b/app/blueprints/api/collections/templates/collections/grid.j2 index b15f23c..26ea25d 100644 --- a/app/blueprints/api/collections/templates/collections/grid.j2 +++ b/app/blueprints/api/collections/templates/collections/grid.j2 @@ -1,8 +1,6 @@
{% for collection in collections %} -
- {% include "collections/card.j2" %} -
+ {% include "collections/card.j2" %} {% else %} Здесь ничего нет. {% endfor %} diff --git a/app/blueprints/api/collections/templates/collections/select.j2 b/app/blueprints/api/collections/templates/collections/select.j2 new file mode 100644 index 0000000..7fc3d5f --- /dev/null +++ b/app/blueprints/api/collections/templates/collections/select.j2 @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/app/blueprints/api/tasks/routes.py b/app/blueprints/api/tasks/routes.py index e163b96..30880d8 100644 --- a/app/blueprints/api/tasks/routes.py +++ b/app/blueprints/api/tasks/routes.py @@ -16,10 +16,6 @@ def get_tasks(): return render_template("tasks_cards.j2", tasks=tasks, sessions=sessions) -@tasks.route("/", methods=["GET"]) -def get_task(task_id: int): - ... - @tasks.route("/session/", methods=["POST"]) def create_task(session_id: int): info = request.form diff --git a/app/blueprints/frontend/routes.py b/app/blueprints/frontend/routes.py index f70ef78..56885d7 100644 --- a/app/blueprints/frontend/routes.py +++ b/app/blueprints/frontend/routes.py @@ -1,6 +1,5 @@ from flask import Blueprint, render_template from app.blueprints.api.sessions.routes import get_sessions -from app.blueprints.api.users.routes import get_users from app.blueprints.api.tasks.routes import get_tasks from app.models.collection import Collection @@ -21,7 +20,8 @@ def tasks(): @frontend.route("/parse/") def parse(id: int): - return render_template("parse.j2", session_id=id, users_template=get_users(id)) + collections = Collection.query.all() + return render_template("parse.j2", session_id=id, collections=collections) @frontend.route("/collections") def collections(): diff --git a/app/blueprints/frontend/templates/parse.j2 b/app/blueprints/frontend/templates/parse.j2 index 3692f51..4aa9ced 100644 --- a/app/blueprints/frontend/templates/parse.j2 +++ b/app/blueprints/frontend/templates/parse.j2 @@ -5,12 +5,19 @@ {% endblock title %} {% block main %} -
+
-

Действия

+
+

Действия

+
+
+ Loading... +
+
+

-
-
- - -
- -
- - + {% include "collections/collections_section.j2" %}
- +
diff --git a/app/models/collection.py b/app/models/collection.py index 077fa99..5d0de43 100644 --- a/app/models/collection.py +++ b/app/models/collection.py @@ -9,4 +9,9 @@ class Collection(db.Model): name: Mapped[str] = mapped_column(unique=True, nullable=False) tasks: Mapped[List['Task']] = relationship("Task", back_populates="collection") - users: Mapped[List['User']] = relationship("User", cascade="all, delete-orphan", back_populates="collection") \ No newline at end of file + users: Mapped[List['User']] = relationship("User", cascade="all, delete-orphan", back_populates="collection") + + def exists(name: str): + collections = Collection.query.filter_by(name=name).all() + + return len(collections) > 0 \ No newline at end of file diff --git a/paper/parser.py b/paper/parser.py index e4261b0..5f3589f 100644 --- a/paper/parser.py +++ b/paper/parser.py @@ -1,11 +1,9 @@ import asyncio -import os from loguru import logger from paper.client import PaperClient from paper.errors import IgnoreException, NeedPasswordException, UserPrivacyException -from paper.models import Message class PaperParser: @@ -17,7 +15,7 @@ class PaperParser: self.users_to_delete = [] - async def invite_users(self, users, group, task = None): + async def invite_users(self, users, group, task): await self.client.invite_self(group) group_participants = await self.client.get_participants(group) @@ -33,7 +31,7 @@ class PaperParser: await self.client.invite_user(user, group) - except (UserPrivacyException, IgnoreException) as e: + except (UserPrivacyException, IgnoreException): self.users_to_delete.append(user) logger.warning("Exception occurred. Skipping user...") @@ -47,6 +45,8 @@ class PaperParser: # if dialog.is_user: # messages = tuple(filter(lambda chat_message: message.text == chat_message.text, await self.client.get_messages(dialog))) # print(messages) + if not task: + return for user in users: try: @@ -55,7 +55,7 @@ class PaperParser: await self.client.send_message(user, message, file) - except (UserPrivacyException, IgnoreException) as e: + except (UserPrivacyException, IgnoreException): self.users_to_delete.append(user) logger.warning("Exception occurred. Skipping user...") @@ -84,4 +84,4 @@ class PaperParser: return self async def __aexit__(self, *args, **kwargs): - await self.client.disconnect() \ No newline at end of file + await self.client.disconnect()