class TicketListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(TicketPageOutSchema) def get(self, pagination, year): """List ticket posts""" try: event = Event.get(year=year) except Event.DoesNotExist: abort(404, message='Event does not exist') return paginate(event.tickets, pagination) @blueprint.arguments(TicketSchema(exclude=['canceled'])) @blueprint.response(TicketSchema) def post(self, args, year): """Create ticket post""" user_id = get_jwt_identity() try: visitor = User.get(id=user_id) except User.DoesNotExist: abort(404, message='User not found') try: event = Event.get(year=year) except Event.DoesNotExist: abort(404, message='Event does not exist') ticket = Ticket(event=event, visitor=visitor, **args) ticket.save() return ticket
class HallListAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(HallPageOutSchema) def get(self, pagination, year): """Get list of halls""" try: event = Event.get(year=int(year)) except Event.DoesNotExist: abort(404, message='No such event') return paginate(event.halls, pagination) @jwt_required @blueprint.arguments(HallSchema) @blueprint.response(HallSchema) def post(self, args, year): """Create new hall""" try: user = User.get(id=get_jwt_identity()) if not user.admin: abort(403, message='Permission denied') except User.DoesNotExist: abort(404, message='User not found') try: event = Event.get(year=int(year)) except Event.DoesNotExist: abort(404, message='No such event') hall = Hall(**args) hall.event = event hall.save() return hall
class ListPublishedBlogPostsAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(BlogPostPageOutSchema) def get(self, pagination): '''List published blog posts sorted by user_id''' sortedPublishedBlogPosts = BlogPost.select().where( BlogPost.published).order_by(BlogPost.author) return paginate(sortedPublishedBlogPosts, pagination)
class BlogListAPI(MethodView): @jwt_optional @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(BlogPageOutSchema) def get(self, pagination): """List blog posts""" user_id = get_jwt_identity() if user_id is None: query = Blog.select().where(Blog.published) else: query = Blog.select() return paginate(query, pagination) @jwt_required @blueprint.arguments(PageInSchema(), location='headers') @blueprint.arguments(BlogSchema) @blueprint.response(BlogPageOutSchema) def post(self, pagination, args): """Create blog post""" blog = Blog(**args) blog.date = datetime.utcnow() user_id = get_jwt_identity() try: user = User.get() except User.DoesNotExist: abort(404, message='User not found') try: Blog.find( blog.date.year, blog.date.month, blog.date.day, blog.slug, ) abort(409, message='Post with the same title already exists') except Blog.DoesNotExist: blog.author = user blog.save() if user_id is None: query = Blog.select().where(Blog.published) else: query = Blog.select() return paginate(query, pagination)
class CfSAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(CfSPageOutSchema) def get(self, pagination, year): """Get list of sponsors""" try: event = Event.get(year=int(year)) except Event.DoesNotExist: abort(404, message='Event not found') return paginate(event.cfs, pagination)
class ListBlogPostsByUserAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(BlogPostPageOutSchema) def get(self, pagination, userId): '''List blog posts by user''' try: user = User.get(id=userId) except User.DoesNotExist: abort(404, message='User not found') blogPostsByUser = BlogPost.select().where(BlogPost.author == user) return paginate(blogPostsByUser, pagination)
class GalleryAlbumListAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(GalleryAlbumPageOutSchema) def get(self, pagination, year=None): """Get list of albums""" if year is None: return paginate(GalleryAlbum.select(), pagination) try: event = Event.get(year=year) except Event.DoesNotExist: abort(404, message='No such event') return paginate(event.albums, pagination)
class PlanListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(PlanPageOutSchema) def get(self, pagination): """List plans""" query = Plan.select() return paginate(query, pagination) @blueprint.arguments(PlanSchema) @blueprint.response(PlanSchema) def post(self, args): """Create plan""" plan = Plan(**args) plan.save() return plan
class UserTalkListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(TalkPageOutSchema) def get(self, pagination, year_id): """Get list of talks by user""" try: user = User.get(id=get_jwt_identity()) except User.DoesNotExist: abort(404, message='User not found') try: event = Event.get(year=int(year_id)) except Event.DoesNotExist: abort(404, message='No such event') allTalks = event.talks.join(User).where(Talk.user == user) query = allTalks.order_by(Talk.start) return paginate(query, pagination)
class GalleryAlbumListAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(GalleryAlbumPageOutSchema) def get(self, pagination, year=None): """Get list of albums""" query = GalleryAlbum.select() return paginate(query, pagination) @blueprint.arguments(GalleryAlbumSchema) @blueprint.response(GalleryAlbumSchema) def post(self, args): """Create album post""" try: album = GalleryAlbum.get(name=args['name']) abort(409, message='Album with the same name already exists') except GalleryAlbum.DoesNotExist: album = GalleryAlbum(**args) album.save() return album
class InstanceListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(InstancePageOutSchema) def get(self, pagination): """List instances""" instances = Instance.fetchAll(current_app.config['SOCKET']) result = { 'data': [i.data() for i in instances], 'total': len(instances), 'pages': 1, } return result @blueprint.arguments(InstanceSchema) @blueprint.response(InstanceSchema) def post(self, args): """Create instance""" instance = Instance(**args) instance.save() return instance
class SocialListAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(SocialPageOutSchema) def get(self, pagination): """List socials""" query = Social.select().order_by(Social.name) return { 'data': query, 'pages': 1, 'total': query.count(), } @jwt_required @blueprint.arguments(SocialSchema) @blueprint.response(SocialSchema) def post(self, args): """Create social""" social = Social(**args) social.save() return social
class MedicListAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(MedicPageOutSchema) def get(self, pagination): """List medics""" query = Medic.select().order_by(Medic.name) return { 'data': query, 'pages': 1, 'total': query.count(), } @jwt_required @blueprint.arguments(MedicSchema) @blueprint.response(MedicSchema) def post(self, args): """Create medic""" medic = Medic(**args) medic.save() return medic
class TalkListAPI(ProtectedMethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(TalkPageOutSchema) def get(self, pagination, year_id): """Get list of talks""" try: event = Event.get(year=int(year_id)) except Event.DoesNotExist: abort(404, message='No such event') query = event.talks.order_by(Talk.start) response = paginate(query, pagination) return response @jwt_required @blueprint.arguments(TalkSchema) @blueprint.response(TalkSchema) def post(self, args, year_id): """Create new talk""" try: user = User.get(id=get_jwt_identity()) except User.DoesNotExist: abort(404, message='User not found') try: event = Event.get(year=int(year_id)) except Event.DoesNotExist: abort(404, message='No such event') talk = Talk(**args) if not user.admin: talk.user = user else: try: talk_user = talk.user try: talk.user = User.get(email=talk_user.email) except User.DoesNotExist: abort(404, message='User not found') except User.DoesNotExist: talk.user = user talk.event = event talk.save() return talk
class EventListAPI(MethodView): @jwt_optional @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(EventPageOutSchema) def get(self, pagination): """List events""" identity = get_jwt_identity() query = Event.select().order_by(Event.year.desc()) user = None if identity: try: user = User.get(id=identity) except User.DoesNotExist: abort(404, message='No such user') if not user or not user.admin: query = query.where(Event.published) return paginate(query, pagination) @jwt_required @blueprint.arguments(EventSchema) @blueprint.response(EventSchema) def post(self, args): """Create event""" event = Event(**args) try: Event.get(year=event.year) abort(409, message='Event in that year already exists') except Event.DoesNotExist: event.published = False event.save() try: user = User.get(id=get_jwt_identity()) except User.DoesNotExist: abort(404, message='User not found') if not user.admin: abort(403, message='Admin area') gallery_album = GalleryAlbum(event=event, name='main') gallery_album.save() hall = Hall(event=event, name='main') hall.save() return event
class GalleryAlbumAPI(MethodView): @blueprint.arguments(PageInSchema(), location='headers') @blueprint.response(GalleryAlbumSchema) def get(self, pagination, name, year=None): """Get album details""" try: album = GalleryAlbum.get(name=name) except GalleryAlbum.DoesNotExist: abort(404, message='No such album') album.files = paginate( album.files.order_by(GalleryFile.filename), pagination, ) prefix = current_app.config.get('MEDIA_URL', None) if prefix is None: abort(409, message='Backend misconfiguration, no MEDIAL_URL') album.prefix = prefix return album @jwt_optional @blueprint.arguments(GalleryUploadSchema, location='files') @blueprint.arguments(ResumableGalleryUploadSchema, location='form') @blueprint.response(ResumableGalleryUploadSchema) def post(self, fileargs, formargs, name, year=None): """Upload picture to album""" try: album = GalleryAlbum.get(name=name) except GalleryAlbum.DoesNotExist: abort(404, message='No such album') user_id = get_jwt_identity() if user_id is None: abort(401, message='Must be logged in to upload!') try: user = User.get(id=user_id) except User.DoesNotExist: abort(404, message='No such user') if not user.admin and album.name != 'avatars': abort(403, message='Only avatar uploads are allowed!') prefix = current_app.config.get('MEDIA_URL', None) if prefix is None: abort(409, message='Backend misconfiguration, no MEDIAL_URL') uploadFile = fileargs['file'] chunkNumber = formargs['resumableChunkNumber'] chunkSize = formargs['resumableChunkSize'] total = formargs['resumableTotalChunks'] identifier = formargs['resumableIdentifier'] media_path = os.path.abspath( current_app.config.get( 'MEDIA_PATH', None, ), ) filePath = f'/tmp/{identifier}' with open(filePath, 'ab') as tempfile: offset = (chunkNumber - 1) * chunkSize tempfile.seek(offset) tempfile.write(uploadFile.read()) if chunkNumber == total: if album.name == 'avatars': category = formargs['resumableType'][:5] if category != 'image': abort(409, message='Only image type allowed') imgtype = formargs['resumableType'][6:] filename = f'{user.id}.{imgtype}' else: filename = secure_filename(uploadFile.filename) try: finalFile = GalleryFile.get(album=album, filename=filename) except GalleryFile.DoesNotExist: finalFile = GalleryFile( album=album, filename=secure_filename(filename), ) print(finalFile.filename) formargs['resumableFilename'] = finalFile.filename file_dir = f'{media_path}/{album.name}' if not os.path.exists(file_dir): os.makedirs(file_dir) finalPath = finalFile.url(prefix=media_path) shutil.move(filePath, finalPath) os.chmod(finalPath, 0o644) finalFile.save() if album.name == 'avatars': user.avatar = finalFile.url(prefix=prefix) user.save() return formargs