mirror of
https://github.com/DarkflameUniverse/NexusDashboard.git
synced 2024-08-30 18:12:11 +00:00
Merge pull request #17 from DarkflameUniverse/pet_owner
Allow pet names to be associated with characters
This commit is contained in:
commit
0dbe9d87fc
@ -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()
|
||||
|
@ -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"] = "<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
|
||||
|
||||
|
||||
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()
|
||||
|
@ -34,6 +34,7 @@
|
||||
<th>Actions</th>
|
||||
<th>Name</th>
|
||||
<th>Status</th>
|
||||
<th>Owner</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
|
30
migrations/versions/e3e8e05f27ee_pet_owners.py
Normal file
30
migrations/versions/e3e8e05f27ee_pet_owners.py
Normal 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 ###
|
Loading…
Reference in New Issue
Block a user