diff --git a/migrations/versions/9083452d3a80_add_catagory_user_associations.py b/migrations/versions/9083452d3a80_add_catagory_user_associations.py new file mode 100644 index 00000000..07387ceb --- /dev/null +++ b/migrations/versions/9083452d3a80_add_catagory_user_associations.py @@ -0,0 +1,36 @@ +"""add catagory_user_associations + +Revision ID: 9083452d3a80 +Revises: 48074e6225c6 +Create Date: 2024-02-16 21:38:11.302398 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "9083452d3a80" +down_revision = "48074e6225c6" +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + "catagory_user_associations", + sa.Column("category_uuid", sa.String(), nullable=True), + sa.Column("user_id", sa.String(), nullable=True), + sa.ForeignKeyConstraint( + ["category_uuid"], + ["category.uuid"], + ), + sa.ForeignKeyConstraint( + ["user_id"], + ["user.id"], + ), + ) + + +def downgrade(): + pass diff --git a/subscribie/blueprints/admin/__init__.py b/subscribie/blueprints/admin/__init__.py index a8fcdf20..270d9f95 100644 --- a/subscribie/blueprints/admin/__init__.py +++ b/subscribie/blueprints/admin/__init__.py @@ -808,6 +808,33 @@ def assign_managers_to_plan(): return render_template("admin/assign_managers_to_plan.html", users=users) +@admin.route("/assign-managers-to-plans//assign-plan", methods=["GET", "POST"]) +@login_required +def user_assign_to_plans(user_id): + user = User.query.get(user_id) + plans = Plan.query.execution_options(include_archived=True) + + if request.method == "POST": + # Remove if not selected + for plan in plans: + if plan in user.plans: + user.plans.remove(plan) + + for plan_id in request.form.getlist("assign"): + plan = Plan.query.execution_options(include_archived=True).get(plan_id) + plan.managers.append(user) + + database.session.commit() + flash("User has been assigned the selected plan(s) as a manager of them") + return redirect(url_for("admin.assign_managers_to_plan")) + + return render_template( + "admin/user_assign_plan.html", + user=user, + plans=plans, + ) + + @admin.route("/list-documents", methods=["get"]) @login_required def list_documents(): diff --git a/subscribie/blueprints/admin/templates/admin/assign_managers_to_plan.html b/subscribie/blueprints/admin/templates/admin/assign_managers_to_plan.html index 16ecf55b..5a5c226f 100644 --- a/subscribie/blueprints/admin/templates/admin/assign_managers_to_plan.html +++ b/subscribie/blueprints/admin/templates/admin/assign_managers_to_plan.html @@ -9,8 +9,7 @@

Assign manager(s) to plans

@@ -38,44 +37,29 @@

Allow managers to quickly see plans they're responsible for

- {% if confirm is sameas false %} -
-

Are you sure?

-
diff --git a/subscribie/blueprints/admin/templates/admin/user_assign_plan.html b/subscribie/blueprints/admin/templates/admin/user_assign_plan.html new file mode 100644 index 00000000..7a94f14b --- /dev/null +++ b/subscribie/blueprints/admin/templates/admin/user_assign_plan.html @@ -0,0 +1,61 @@ +{% extends "admin/layout.html" %} +{% block title %} {{ title }} {% endblock %} + +{% block body %} + +

Choice Group - Assign Plan

+ +
+ +
+ +
+
+
+ +

Assign Plan(s) to user: {{ user.email }}

+

Tick which plan(s) you want to assign the user to

+

Doing so will make these plans(s) and any associated subscriptions appear more prominently for them when they login.

+ +
+
Check all
+
Check none
+
+ {% for plan in plans %} + {% if plan.subscriptions | length > 0 %} +
+
+
+ +
+
+ {{ plan.archived }}| Subscriptions: {{ plan.subscriptions|length }} +
+ {% endif %} + {% endfor %} + + +
+ +
+
+
+ + + +{% endblock body %} diff --git a/subscribie/models.py b/subscribie/models.py index f72e3411..64fa0dbd 100644 --- a/subscribie/models.py +++ b/subscribie/models.py @@ -60,6 +60,9 @@ def filter_archived(query): if desc["type"] is Person and "archived-subscribers" in request.path: query = query.filter(entity.archived == 1) return query + elif desc["type"] is User and "assign-managers-to-plan" in request.path: + query = query.execution_options(include_archived=True) + return query elif ( desc["type"] is Person and request.path != "/" @@ -108,6 +111,13 @@ class HasReadOnly(object): read_only = Column(Boolean, nullable=False, default=0) +association_table_plan_to_users = database.Table( + "plan_user_associations", + database.Column("plan_uuid", database.String, ForeignKey("plan.uuid")), + database.Column("user_id", database.String, ForeignKey("user.id")), +) + + class User(database.Model): __tablename__ = "user" id = database.Column(database.Integer(), primary_key=True) @@ -118,6 +128,7 @@ class User(database.Model): login_token = database.Column(database.String) password_reset_string = database.Column(database.String()) password_expired = database.Column(database.Boolean(), default=0) + plans = relationship("Plan", secondary=association_table_plan_to_users) def set_password(self, password): self.password = generate_password_hash(password) @@ -604,12 +615,6 @@ class Company(database.Model): ), ) -association_table_plan_to_users = database.Table( - "plan_user_associations", - database.Column("plan_uuid", database.String, ForeignKey("plan.uuid")), - database.Column("user_id", database.String, ForeignKey("user.id")), -) - class INTERVAL_UNITS(Enum): DAILY = _("daily") @@ -672,10 +677,13 @@ class Plan(database.Model, HasArchived): price_lists = relationship( "PriceList", secondary=association_table_plan_to_price_lists ) + subscriptions = relationship( + "Subscription", primaryjoin="foreign(Subscription.sku_uuid)==Plan.uuid" + ) managers = relationship( "User", secondary=association_table_plan_to_users, - backref=database.backref("plans", lazy="dynamic"), + backref=database.backref("managers", lazy="dynamic"), ) def getPrice(self, currency):