diff --git a/app/models.py b/app/models.py index df557a3..5a44a11 100644 --- a/app/models.py +++ b/app/models.py @@ -582,6 +582,19 @@ class PetNames(db.Model): server_default='0' ) + owner_id = db.Column( + mysql.BIGINT, + db.ForeignKey(CharacterInfo.id, ondelete='CASCADE'), + nullable=True + ) + + owner = db.relationship( + 'CharacterInfo', + foreign_keys=[owner_id], + backref="pet_owner", + passive_deletes=True + ) + def save(self): db.session.add(self) db.session.commit() diff --git a/app/moderation.py b/app/moderation.py index a9cd9d6..10c348a 100644 --- a/app/moderation.py +++ b/app/moderation.py @@ -1,6 +1,6 @@ from flask import render_template, Blueprint, redirect, url_for, request, abort, flash from flask_user import login_required -from app.models import PetNames, db +from app.models import PetNames, db, CharacterXML, CharacterInfo from datatables import ColumnDT, DataTables from app.forms import CreatePlayKeyForm, EditPlayKeyForm from app import gm_level, log_audit @@ -47,10 +47,14 @@ def reject_pet(id): @login_required @gm_level(3) def get_pets(status="all"): + # call this to make things nicer + accociate_pets_and_owners() + columns = [ ColumnDT(PetNames.id), ColumnDT(PetNames.pet_name), ColumnDT(PetNames.approved), + ColumnDT(PetNames.owner_id), ] query = None @@ -107,4 +111,21 @@ def get_pets(status="all"): """ pet_data["2"] = "Rejected" + pet_data["3"] = f""" + + {CharacterInfo.query.filter(CharacterInfo.id==pet_data['3']).first().name} + + """ + return data + + +def accociate_pets_and_owners(): + pets = PetNames.query.filter(PetNames.owner_id == None).all() + if pets: + for pet in pets: + owner = CharacterXML.query.filter(CharacterXML.xml_data.like(f"%{pet.id}%")).first() + if owner: + pet.owner_id = owner.id + pet.save() diff --git a/app/templates/moderation/index.html.j2 b/app/templates/moderation/index.html.j2 index 596e5f9..923b61d 100644 --- a/app/templates/moderation/index.html.j2 +++ b/app/templates/moderation/index.html.j2 @@ -34,6 +34,7 @@ Actions Name Status + Owner diff --git a/migrations/versions/e3e8e05f27ee_pet_owners.py b/migrations/versions/e3e8e05f27ee_pet_owners.py new file mode 100644 index 0000000..00c124c --- /dev/null +++ b/migrations/versions/e3e8e05f27ee_pet_owners.py @@ -0,0 +1,30 @@ +"""pet owners + +Revision ID: e3e8e05f27ee +Revises: 3132aaef7413 +Create Date: 2022-02-11 23:18:20.978203 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = 'e3e8e05f27ee' +down_revision = '3132aaef7413' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('pet_names', sa.Column('owner_id', mysql.BIGINT(), nullable=True)) + op.create_foreign_key(None, 'pet_names', 'charinfo', ['owner_id'], ['id'], ondelete='CASCADE') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'pet_names', type_='foreignkey') + op.drop_column('pet_names', 'owner_id') + # ### end Alembic commands ###