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(
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()

View File

@ -23,8 +23,8 @@ def approve_pet(id):
pet_data = PetNames.query.filter(PetNames.id == id).first()
pet_data.approved = 2
log_audit(f"Approved pet name {pet_data.pet_name} from {pet_data.owner.name}")
flash(f"Approved pet name {pet_data.pet_name} from {pet_data.owner.name}", "success")
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_id}", "success")
pet_data.save()
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.approved = 0
log_audit(f"Rejected pet name {pet_data.pet_name} from {pet_data.owner.name}")
flash(f"Rejected pet name {pet_data.pet_name} from {pet_data.owner.name}", "danger")
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_id}", "danger")
pet_data.save()
return redirect(request.referrer if request.referrer else url_for("main.index"))
@ -47,9 +47,6 @@ 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),
@ -111,6 +108,7 @@ def get_pets(status="all"):
"""
pet_data["2"] = "<span class='text-danger'>Rejected</span>"
if pet_data["3"]:
try:
pet_data["3"] = f"""
<a role="button" class="btn btn-primary btn btn-block"
@ -120,20 +118,33 @@ def get_pets(status="all"):
"""
except Exception as e:
PetNames.query.filter(PetNames.id==id).first().delete()
pet_data["3"] = "Deleted Character"
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
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()
@scheduler.task("cron", id="pet_name_maintenance", mintute=0, timezone="UTC")
def pet_name_maintenance():
# associate pet names to characters
unassociated_pets = PetNames.query.filter(PetNames.owner_id == None).all()
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:
pet.owner_id = owner.id
pet.save()
else:
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 ###