renamed participants to attendees

This commit is contained in:
Bad_Investment
2021-05-04 20:10:04 +00:00
parent 3f33879d2f
commit 26a23dfa4f
5 changed files with 21 additions and 21 deletions

28
app.py
View File

@@ -20,7 +20,7 @@ API_SETTINGS = FastAPISettings.parse_obj(SETTINGS['fastapi'])
from scraper import RedditScraper
from bot import RedditBot
from models import metadata, database, Event, Participant, Claim
from models import metadata, database, Event, Attendee, Claim
engine = sqlalchemy.create_engine(DB_SETTINGS.url)
metadata.create_all(engine)
@@ -31,7 +31,7 @@ app = FastAPI(
openapi_tags=API_SETTINGS.openapi_tags
)
app.include_router(CRUDRouter(schema=Event, prefix='event'))
app.include_router(CRUDRouter(schema=Participant, prefix='participant'))
app.include_router(CRUDRouter(schema=Attendee, prefix='attendee'))
app.include_router(CRUDRouter(schema=Claim, prefix='claim'))
app.state.database = database
@@ -87,11 +87,11 @@ async def grant_claim(request: Request, event_id: str, username: str, link: str)
except ormar.exceptions.NoMatch:
raise HTTPException(status_code=404, detail=f'Event with id "{event_id}" does not exist')
try:
participant = await Participant.objects.get(pk=username)
attendee = await Attendee.objects.get(pk=username)
except ormar.exceptions.NoMatch:
raise HTTPException(status_code=404, detail=f'Participant with username "{username}" does not exist')
raise HTTPException(status_code=404, detail=f'Attendee with username "{username}" does not exist')
try:
claim = Claim(event=event, participant=participant, link=link)
claim = Claim(event=event, attendee=attendee, link=link)
await claim.save()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@@ -121,18 +121,18 @@ async def upload_claims(request: Request, event_id: str, file: UploadFile = File
usernames = df['username'].tolist()
claim_map = df.set_index('username')['link'].to_dict()
existing_participants = await Participant.objects.filter(id__in=usernames).all()
existing_claims = await Claim.objects.filter(ormar.and_(event__id__exact=event_id, participant__id__in=usernames)).all()
existing_claim_usernames = [c.participant.id for c in existing_claims]
existing_attendees = await Attendee.objects.filter(id__in=usernames).all()
existing_claims = await Claim.objects.filter(ormar.and_(event__id__exact=event_id, attendee__id__in=usernames)).all()
existing_claim_usernames = [c.attendee.id for c in existing_claims]
new_participants = [Participant(id=username) for username in usernames if username not in [p.id for p in existing_participants]]
await Participant.objects.bulk_create(new_participants)
new_attendees = [Attendee(id=username) for username in usernames if username not in [p.id for p in existing_attendees]]
await Attendee.objects.bulk_create(new_attendees)
participants = existing_participants + new_participants
attendees = existing_attendees + new_attendees
for participant in participants:
if participant.id not in existing_claim_usernames:
claim = Claim(event=event, participant=participant, link=claim_map[participant.id])
for attendee in attendees:
if attendee.id not in existing_claim_usernames:
claim = Claim(event=event, attendee=attendee, link=claim_map[attendee.id])
await claim.save()
return True

View File

@@ -29,7 +29,7 @@ class RedditBot:
await message.reply('pong')
elif message.body:
code = message.body.split(' ')[0].lower()
claim = await Claim.objects.filter(event__id__exact=code, participant__id__exact=message.author.name.lower()).get_or_none()
claim = await Claim.objects.filter(event__id__exact=code, attendee__id__exact=message.author.name.lower()).get_or_none()
if claim:
if claim.event.expiry_date < datetime.utcnow():
await message.reply(f'Sorry, your claim for {claim.event.id} has expired')

View File

@@ -17,6 +17,6 @@ class BaseMeta(ormar.ModelMeta):
database = database
from .event import Event
from .participant import Participant
from .attendee import Attendee
from .claim import Claim
from .message import RequestMessage

View File

@@ -4,17 +4,17 @@ from typing import List, Optional
from . import BaseMeta
from .event import Event
from .participant import Participant
from .attendee import Attendee
from .message import RequestMessage
class Claim(ormar.Model):
class Meta(BaseMeta):
tablename = "claims"
constraints = [ormar.UniqueColumns('participant','event')]
constraints = [ormar.UniqueColumns('attendee','event')]
id: int = ormar.Integer(primary_key=True)
participant: Participant = ormar.ForeignKey(Participant)
attendee: Attendee = ormar.ForeignKey(Attendee)
event: Event = ormar.ForeignKey(Event)
link: str = ormar.String(max_length=256)
notified: Optional[bool] = ormar.Boolean(default=False)

View File

@@ -5,10 +5,10 @@ from typing import List, Optional
from . import BaseMeta
from .event import Event
class Participant(ormar.Model):
class Attendee(ormar.Model):
class Meta(BaseMeta):
tablename = "participants"
tablename = "attendees"
id: str = ormar.String(primary_key=True, max_length=100)
attended_events: Optional[List[Event]] = ormar.ManyToMany(Event)