Allow pet names to be associated with characters

This commit is contained in:
Aaron Kimbre 2022-02-11 23:40:15 -06:00
parent 9cc3dbb4c4
commit d792596926
4 changed files with 66 additions and 1 deletions

View File

@ -582,6 +582,19 @@ class PetNames(db.Model):
server_default='0' 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): def save(self):
db.session.add(self) db.session.add(self)
db.session.commit() db.session.commit()

View File

@ -1,6 +1,6 @@
from flask import render_template, Blueprint, redirect, url_for, request, abort, flash from flask import render_template, Blueprint, redirect, url_for, request, abort, flash
from flask_user import login_required 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 datatables import ColumnDT, DataTables
from app.forms import CreatePlayKeyForm, EditPlayKeyForm from app.forms import CreatePlayKeyForm, EditPlayKeyForm
from app import gm_level, log_audit from app import gm_level, log_audit
@ -47,10 +47,14 @@ def reject_pet(id):
@login_required @login_required
@gm_level(3) @gm_level(3)
def get_pets(status="all"): def get_pets(status="all"):
# call this to make things nicer
accociate_pets_and_owners()
columns = [ columns = [
ColumnDT(PetNames.id), ColumnDT(PetNames.id),
ColumnDT(PetNames.pet_name), ColumnDT(PetNames.pet_name),
ColumnDT(PetNames.approved), ColumnDT(PetNames.approved),
ColumnDT(PetNames.owner_id),
] ]
query = None query = None
@ -107,4 +111,21 @@ def get_pets(status="all"):
""" """
pet_data["2"] = "<span class='text-danger'>Rejected</span>" pet_data["2"] = "<span class='text-danger'>Rejected</span>"
pet_data["3"] = f"""
<a role="button" class="btn btn-primary btn btn-block"
href='{url_for('characters.view', id=pet_data["3"])}'>
{CharacterInfo.query.filter(CharacterInfo.id==pet_data['3']).first().name}
</a>
"""
return data 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()

View File

@ -34,6 +34,7 @@
<th>Actions</th> <th>Actions</th>
<th>Name</th> <th>Name</th>
<th>Status</th> <th>Status</th>
<th>Owner</th>
</tr> </tr>
</thead> </thead>
<tbody></tbody> <tbody></tbody>

View File

@ -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 ###