def activate_skill(id): raw_json = request.get_json() if 'target' not in raw_json: abort(401) target = raw_json['target'] target = Participant.query.filter_by(username=target).first() if g.user.is_superuser or (target and g.user.use_skill(id)): skill = Skill.query.get(id) n = Notification( description= f"You have been targeted by: !!! {skill.name} !!! Skill description: {skill.description}", forwared_to=target, sender=g.user) n2 = Notification( description= f"User {g.user.username} activated skill {skill.name} onto {target.username}" ) db.session.add(g.user) db.session.add(n) db.session.add(n2) try: db.session.commit() except: print( f"Error when user {g.user.username} tried to activate skill {skill.id} on {target.username}" ) db.session.rollback() return {}, 204 return {'error': "You cant use this skill!"}, 500
def deactivate(self, county): # noinspection PyPropertyAccess for effect in self.effects: effect.undo(county) # noinspection PyPropertyAccess notice = Notification(county, f"Lost {self.name}", self.description, category="Research") notice.save()
def get_notification(): user = ObjectMother.get_user() return Notification(user=user, title="test title", message="test msg", action="Testing", received_at=datetime.now())
def mana_change(self): county = self.county growth = self._mana_change active_spells = Casting.query.filter_by(county_id=county.id).filter_by( active=True).all() loss = sum(spell.mana_sustain for spell in active_spells) difference = int(growth - loss) if difference < 0 and county.mana + difference < 0: drop_spell = choice(active_spells) drop_spell.active = False drop_spell.save() # might be unneeded notice = Notification( county, "Spell Ended", f"You did not have enough mana and {drop_spell.name} ended.", "Spell End") notice.save() # noinspection PyPropertyAccess return self.mana_change return difference
def notify_all(): participants = Participant.query.all() content = input("Content: ") for participant in participants: n = Notification(forwared_to=participant, description=content) db.session.add(n) try: db.session.commit() print("Done :)") except: print("Oops! Shit happened!") db.session.rollback()
def activate(self, county): # noinspection PyPropertyAccess for effect in self.effects: try: effect.activate(self) except TypeError as ex: tech_name = self.name effect_info = repr(effect.kwargs) # noinspection PyPropertyAccess description = self.description county_name = county.name raise TypeError( f"Tech: {tech_name} of {description} was crashed " f"by {effect_info} in county {county_name}" f"\nThe original exception was\n{ex}") # noinspection PyPropertyAccess notice = Notification(county, f"Discovered {self.name}", self.description, category="Research") notice.save()
def create_user_notification(user, action, title, message): """ Create a User Notification :param user: User object to send the notification to :param action: Action being performed :param title: The message title :param message: The message """ notification = Notification(user=user, action=action, title=title, message=message, received_at=datetime.now()) saved = save_to_db(notification, 'User notification saved') if saved: DataManager.push_user_notification(user)
def create_notification(): raw_json = request.get_json() schema = CreateNotificationSchema() results = schema.load(raw_json) if results.errors: raise Exception("Validation errors:", results.errors) n = Notification(sender=g.user, forwared_to=Participant.query.filter_by( username=results.data['forwared_to']), description=results.data['content']) db.session.add(n) try: db.session.commit() return {}, 204 except: print("Error creating notification", results.data) db.session.rollback() abort(500)
def create_notification_all(): raw_json = request.get_json() if 'content' in raw_json: content = raw_json['content'] else: abort(401) for participant in Participant.query.filter_by(is_superuser=False).all(): n = Notification(sender=g.user, forwared_to=participant, description=content) db.session.add(n) try: db.session.commit() return {}, 204 except: print("Error creating notification", content) db.session.rollback() abort(500)
def notify_user(): participants = Participant.query.all() targets = {} while 1: print("Participants:") for p in participants: print(p.username) targets[p.username] = p target = input("Who are you gonna notify? ") if target in targets: break content = input("Content: ") n = Notification(forwared_to=targets[target], description=content) db.session.add(n) try: db.session.commit() print("Done :)") except: print("Oops! Shit happened!") db.session.rollback()
if completed_challenges and not participant.base_completed_challenges: participant.base_completed_challenges = completed_challenges print("Setting base challenges...") if score and not participant.base_score: participant.base_score = score print("Setting base score...") if completed_challenges > participant.completed_challenges + participant.base_completed_challenges: print( "\t\t\tWow!! Participant", participant.codewars_username, "has completed", completed_challenges - (participant.completed_challenges + participant.base_completed_challenges), "additional katas since last time!") n = Notification(description="Congrats on the new kata!", forwared_to=participant) db.session.add(n) for skill in skills: if skill.can_consume(participant): participant.add_skill(skill.id) n = Notification( description= f"It looks like you have a new skill! \n You've received {skill.name}!!", forwared_to=participant) db.session.add(n) print("Participant", participant.codewars_username, "won skill", skill.name) break participant.score = score - participant.base_score participant.completed_challenges = completed_challenges - participant.base_completed_challenges print("Saving participant stats and skills...")