from api.resources import AuthenticatedResource from tracker import db class ActivateArtists(AuthenticatedResource): def put(self, id_str): db.Session.execute("update {tablename} set active = {make_active} where {id_field} = :id_value".format( tablename=self.tablename, make_active=self.make_active, id_field=self.id_field, ), params=dict(id_value=id_str)) db.Session.commit() class SnoozeArtists(AuthenticatedResource): def put(self, id_str): db.Session.execute(""" update {tablename} set snooze_until = case when {should_snooze} then CURRENT_DATE + interval '15 days' else '2000-01-01 00:00:00' end where {id_field} = :id_value """.format( tablename=self.tablename, should_snooze=self.should_snooze, id_field=self.id_field, ), params=dict(id_value=id_str)) db.Session.commit() def register_urls(api, root="/api/admin"): for true_or_false in (True, False): for artist_mixin in (db.ScUser, db.InUser, db.SpyArtist, db.TrackedEntity): classname = ( ('Activate' if true_or_false else 'Deactivate') + artist_mixin.__name__ ) new_class = type(classname, (ActivateArtists,), dict( tablename=artist_mixin.__tablename__, id_field=artist_mixin.id_field, make_active=true_or_false, )) path = "/".join([ root, artist_mixin.artist_association_type, "", ('activate' if true_or_false else 'deactivate'), ]) api.add_resource(new_class, path) classname = ( ('Snooze' if true_or_false else 'Unsnooze') + artist_mixin.__name__ ) new_class = type(classname, (SnoozeArtists,), dict( tablename=artist_mixin.__tablename__, id_field=artist_mixin.id_field, should_snooze=true_or_false, )) path = "/".join([ root, artist_mixin.artist_association_type, "", ('snooze' if true_or_false else 'unsnooze'), ]) api.add_resource(new_class, path)