sensible fix for pet names

and automoderation for pet names
This commit is contained in:
Aaron Kimbre 2022-02-12 21:37:11 -06:00
parent d63dd807e3
commit 78ed39905f
3 changed files with 62 additions and 31 deletions

View File

@ -584,17 +584,9 @@ class PetNames(db.Model):
owner_id = db.Column( owner_id = db.Column(
mysql.BIGINT, mysql.BIGINT,
db.ForeignKey(CharacterInfo.id, ondelete='CASCADE'),
nullable=True 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

@ -23,8 +23,8 @@ def approve_pet(id):
pet_data = PetNames.query.filter(PetNames.id == id).first() pet_data = PetNames.query.filter(PetNames.id == id).first()
pet_data.approved = 2 pet_data.approved = 2
log_audit(f"Approved pet name {pet_data.pet_name} from {pet_data.owner.name}") log_audit(f"Approved pet name {pet_data.pet_name} from {pet_data.owner_id}")
flash(f"Approved pet name {pet_data.pet_name} from {pet_data.owner.name}", "success") flash(f"Approved pet name {pet_data.pet_name} from {pet_data.owner_id}", "success")
pet_data.save() pet_data.save()
return redirect(request.referrer if request.referrer else url_for("main.index")) return redirect(request.referrer if request.referrer else url_for("main.index"))
@ -37,8 +37,8 @@ def reject_pet(id):
pet_data = PetNames.query.filter(PetNames.id == id).first() pet_data = PetNames.query.filter(PetNames.id == id).first()
pet_data.approved = 0 pet_data.approved = 0
log_audit(f"Rejected pet name {pet_data.pet_name} from {pet_data.owner.name}") log_audit(f"Rejected pet name {pet_data.pet_name} from {pet_data.owner_id}")
flash(f"Rejected pet name {pet_data.pet_name} from {pet_data.owner.name}", "danger") flash(f"Rejected pet name {pet_data.pet_name} from {pet_data.owner_id}", "danger")
pet_data.save() pet_data.save()
return redirect(request.referrer if request.referrer else url_for("main.index")) return redirect(request.referrer if request.referrer else url_for("main.index"))
@ -47,9 +47,6 @@ 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),
@ -111,29 +108,43 @@ def get_pets(status="all"):
""" """
pet_data["2"] = "<span class='text-danger'>Rejected</span>" pet_data["2"] = "<span class='text-danger'>Rejected</span>"
try: if pet_data["3"]:
pet_data["3"] = f""" try:
<a role="button" class="btn btn-primary btn btn-block" pet_data["3"] = f"""
href='{url_for('characters.view', id=pet_data["3"])}'> <a role="button" class="btn btn-primary btn btn-block"
{CharacterInfo.query.filter(CharacterInfo.id==pet_data['3']).first().name} href='{url_for('characters.view', id=pet_data["3"])}'>
</a> {CharacterInfo.query.filter(CharacterInfo.id==pet_data['3']).first().name}
""" </a>
except Exception as e: """
PetNames.query.filter(PetNames.id==id).first().delete() except Exception as e:
pet_data["3"] = "Deleted Character" PetNames.query.filter(PetNames.id==id).first().delete()
pet_data["0"] = "<span class='text-danger'>Deleted Refresh to make go away</span>"
pet_data["3"] = "<span class='text-danger'>Character Deleted</span>"
else:
pet_data["3"] = "Pending Character Association"
return data return data
def accociate_pets_and_owners(): @scheduler.task("cron", id="pet_name_maintenance", mintute=0, timezone="UTC")
pets = PetNames.query.filter(PetNames.owner_id == None).all() def pet_name_maintenance():
if pets: # associate pet names to characters
for pet in pets: unassociated_pets = PetNames.query.filter(PetNames.owner_id == None).all()
owner = CharacterXML.query.filter(CharacterXML.xml_data.like(f"%{pet.id}%")).first() if unassociated_pets:
for pet in unassociated_pets:
owner = CharacterXML.query.filter(CharacterXML.xml_data.like(f"%<p id=\"{pet.id}\" l=\"%")).first()
if owner: if owner:
pet.owner_id = owner.id pet.owner_id = owner.id
pet.save() pet.save()
else: else:
pet.delete() pet.delete()
# auto-moderate based on already moderated names
unmoderated_pets = PetNames.query.filter(PetNames.approved==1).all()
if unmoderated_pets:
for pet in unmoderated_pets:
existing_pet = PetNames.query.filter(PetNames.approved.in_([0,2])).first()
if existing_pet:
pet.approved = existing_pet.approved
pet.save()

View File

@ -0,0 +1,28 @@
"""make pet owner not a forein key
Revision ID: b470795db8e1
Revises: e3e8e05f27ee
Create Date: 2022-02-12 20:51:12.318782
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'b470795db8e1'
down_revision = 'e3e8e05f27ee'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('pet_names_ibfk_1', 'pet_names', type_='foreignkey')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_foreign_key('pet_names_ibfk_1', 'pet_names', 'charinfo', ['owner_id'], ['id'], ondelete='CASCADE')
# ### end Alembic commands ###