class Project(db.Document): title = db.StringField(required=True, unique=True) status = db.StringField(required=True, default='active', choices={'active', 'archived'}) created_by = db.ReferenceField('User') created_date = db.DateTimeField(required=True, default=datetime.now) cards = db.ListField(db.ReferenceField('Card'), reverse_delete_rule=db.PULL) class Meta: collection_name = "project" strict = False def find_all(self): items = self._repo.find_all() return items
class Card(db.Document): title = db.StringField(required=True) content = db.StringField() start_date = db.DateTimeField() end_date = db.DateTimeField() status = db.StringField(required=True, default='received', choices={'received', 'started', 'checked', 'completed'}) assigned_to = db.ListField(db.ReferenceField('User')) created_by = db.ReferenceField('User') project = db.ReferenceField('Project') created_date = db.DateTimeField(required=True, default=datetime.now) completion_date = db.DateTimeField() comments = db.ListField(db.EmbeddedDocumentField('Comment')) class Meta: collection_name = "card"
class Comment(db.EmbeddedDocument): content = db.StringField(required=True) sender = db.ReferenceField('User') created_date = db.DateTimeField(required=True, default=datetime.now) class Meta: collection_name = "comment"
class User(db.Document): email = db.EmailField(required=True, unique=True) password = db.StringField(required=True, min_lenght=6) wishlist = db.ListField(db.ReferenceField('Wish', reverse_delete_rule=db.PULL)) def hash_password(self): self.password = generate_password_hash(self.password).decode('utf8') def check_password(self, password): return check_password_hash(self.password, password)
class User(db.Document): email = db.EmailField(max_length=254, required=True, unique=True) password = db.StringField(min_length=6, required=True) movies = db.ListField(db.ReferenceField( 'Movie', reverse_delete_rule=db.PULL)) def hash_password(self): self.password = generate_password_hash(self.password).decode('utf8') def check_password(self, password): return check_password_hash(self.password, password)
class Item(db.Document): label = db.StringField(required=True) description = db.StringField(required=False) image = db.EmbeddedDocumentField(UploadedImage, required=False, null=True) bbox = db.EmbeddedDocumentField(BoundingBox, required=False, null=True) # audit fields created_by = db.StringField(choices=USERS.keys(), default=USERS['auto'], required=True) modified_by = db.StringField(choices=USERS.keys(), default=USERS['auto'], required=False, null=True) created_at = db.DateTimeField(required=True, default=datetime.now()) updated_at = db.DateTimeField(required=True, ) @property def get_creator(self): return USERS[self.created_by] @property def get_modifier(self): return USERS[self.modified_by]
class User(db.Document): email = db.EmailField(required=True, unique=True) password = db.StringField(required=True, min_length=6) projects = db.ListField(db.ReferenceField('Project'), reverse_delete_rule=db.PULL) cards = db.ListField(db.ReferenceField('Card'), reverse_delete_rule=db.PULL) assignments = db.ListField(db.ReferenceField('Card'), reverse_delete_rule=db.PULL) class Meta: collection_name = "user" def hash_password(self): self.password = generate_password_hash(self.password).decode('utf8') def check_password(self, password): return check_password_hash(self.password, password)
class UploadedImage(db.EmbeddedDocument): name = db.StringField() source_origin = db.StringField()
class Noticia(db.Document): titulo = db.StringField(required=True) texto = db.StringField(required=True) autor = db.ListField(db.StringField(), required=True)
class User(db.Document): _id = db.StringField() email = db.StringField() password = db.StringField() pseudo = db.StringField() my_team = db.DictField() friends_list = db.ListField() request_friends_list = db.ListField() waiting_friends_list = db.ListField() money = db.IntField() players = db.ListField() date_created = db.DateTimeField(default=datetime.utcnow()) players_selected_by_position = db.DictField() def register(self): request_json = json.loads(request.data) self._id = uuid.uuid4().hex self.email = request_json["email"] self.password = pbkdf2_sha256.encrypt(request_json["password"]) self.pseudo = request_json["pseudo"] self.my_team = { 'GK': '', 'LB': '', 'RB': '', 'CB': '', 'MC': '', 'LM': '', 'RM': '', 'CAM': '', 'LW': '', 'RW': '', 'ST': '' } self.friends_list = [] self.request_friends_list = [] self.waiting_friends_list = [] self.money = 600000 self.players = [] self.players_selected_by_position = { 'GK': '', 'LB': '', 'RB': '', 'CB': '', 'MC': '', 'LM': '', 'RM': '', 'CAM': '', 'LW': '', 'RW': '', 'ST': '' } # Check for existing email address if User.objects(email=request_json["email"]): return jsonify({"error": "Email address already in use"}), 400 return self.do_save() def get_player_instance(): return Player() def do_save(self): if self.save(): return "", 200 return jsonify({"error": "Signup failed"}), 400 def get_my_team(self): for key, value in self.players_selected_by_position.items(): self.my_team[key] = Player().get_one(value) self.save() return self.my_team def get_friends_list(self): if self.friends_list: return jsonify({"_list": self.friends_list}), 200 else: return jsonify({"error": "You don't have any friends"}), 400 def get_waiting_friend_list(self): if self.waiting_friends_list: return jsonify({"_list": self.waiting_friends_list}), 200 else: return jsonify({"error": "You didn't ask for friends"}), 400 def get_request_friend_list(self): if self.request_friends_list: return jsonify({"_list": self.request_friends_list}), 200 else: return jsonify({"error": "You have no friend request"}), 400 def act_rm_friend_friend_list(self): request_json = json.loads(request.data) friend_email = request_json["user_email"] _copy_friends_list = self.friends_list.copy() for index, friend in enumerate(_copy_friends_list, start=0): if friend["email"] == friend_email: self.friends_list.pop(index) return self.do_save() return jsonify({"error": "Remove Friend In List Failed"}), 400 def act_accept_friend_request_list(self): request_json = json.loads(request.data) friend_email = request_json["user_email"] _friend = self.get_user_by_email(friend_email) _copy_request_friends_list = self.request_friends_list.copy() for index, friend in enumerate(_copy_request_friends_list, start=0): if friend["email"] == friend_email: self.request_friends_list.pop(index) self.friends_list.append({ 'email': _friend.email, 'pseudo': _friend.pseudo }) return self.do_save() return jsonify({"error": "Accept Friend Request List Failed"}), 400 def act_refuse_friend_request_list(self): request_json = json.loads(request.data) friend_email = request_json["user_email"] _copy_request_friends_list = self.v.copy() for index, friend in enumerate(_copy_request_friends_list, start=0): if friend["email"] == friend_email: self.request_friends_list.pop(index) return self.do_save() return jsonify({"error": "Refuse Friend Request List Failed"}), 400 def act_cancel_friend_waiting_list(self): request_json = json.loads(request.data) friend_email = request_json["user_email"] _copy_waiting_list = self.waiting_friends_list.copy() for index, friend in enumerate(_copy_waiting_list, start=0): if friend["email"] == friend_email: self.waiting_friends_list.pop(index) return self.do_save() return jsonify({"error": "Cancel Friend Waiting List Failed"}), 400 def act_add_friend_waiting_list(self): request_json = json.loads(request.data) friend_email = request_json["user_email"] _friend = self.get_user_by_email(friend_email) if _friend: available, msg = self.check_available_friend_request(_friend.email) if not available: return jsonify({"error": msg}), 400 self.waiting_friends_list.append({ 'email': _friend.email, 'pseudo': _friend.pseudo }) _friend.request_friends_list.append({ 'email': self.email, 'pseudo': self.pseudo }) _friend.do_save() return self.do_save() else: return jsonify({"error": "No user had this email address"}), 400 def check_available_friend_request(self, _friend_email): if self.email == _friend_email: return False, "You cannot add yourself" elif _friend_email in [user["email"] for user in self.friends_list]: return False, "This user are in your friend list" elif _friend_email in [ user["email"] for user in self.waiting_friends_list ]: return False, "This user are in your waiting friend list" elif _friend_email in [ user["email"] for user in self.request_friends_list ]: return False, "This user are in your request friend list" else: return True, "" def get_user_by_id(self, _id): return User.objects(_id=_id).first() def get_user_by_email(self, _email): return User.objects(email=_email).first() def signout(self): session.clear() def login(self): request_json = json.loads(request.data) user = User.objects(email=request_json["email"]).first() if user and pbkdf2_sha256.verify(request_json["password"], user.password): access_token = create_access_token(identity=user._id) return jsonify({ "access_token": access_token, "user_id": user._id }), 200 return jsonify({"error": "Invalid login credentials"}), 401 def get_money(self): return jsonify({"money": self.money}), 200 def get_players_selected_by_position(self): return jsonify({ "players_selected_by_position": self.players_selected_by_position }), 200 def save_my_team(self): request_json = json.loads(request.data) _selected_players = request_json["_selected_players"] for key, value in _selected_players.items(): if value in [player['name'] for player in self.players]: if self.my_team[key] and value != self.my_team[key].name: self.my_team[key] = Player().get_one(value) if self.players_selected_by_position[key] != value: self.players_selected_by_position[key] = value elif value == "": continue else: return jsonify({ "error": "The players selected are not in list of my players." }), 401 return self.do_save() def get_my_players(self, filters): _filters_true = [] for key, value in filters.items(): if value: _filters_true.append(key) if _filters_true: _to_return = multikeysort(self.players, _filters_true) return jsonify({"my_players": _to_return}), 200 else: return jsonify({"my_players": self.players}), 200 def get_one_of_my_players(self): request_json = json.loads(request.data) _selected_player = request_json["player_name"] if True if _selected_player in [ my_player["name"] for my_player in self.players ] else False: return jsonify({ "player": player for player in self.players if player["name"] == _selected_player }), 200 else: return jsonify({"error": "Get one player failed"}), 400 def remove_player_in_selected_list_and_team(self, player_name): for key, value in self.players_selected_by_position.items(): if value == player_name: self.players_selected_by_position[key] = "" for key, value in self.my_team.items(): if value == player_name: self.my_team[key] = "" def sell_one_player(self): request_json = json.loads(request.data) name_player_to_add = request_json["sell_one_player"] _len_list_player = len(self.players) _index_player = 0 _break = False while _index_player <= _len_list_player: if name_player_to_add == self.players[_index_player]['name']: if name_player_to_add in self.players_selected_by_position.values( ): self.remove_player_in_selected_list_and_team( name_player_to_add) self.money += int(self.players[_index_player]['price']) self.players.pop(_index_player) self.save() _break = True break _index_player += 1 if _break: return self.do_save() else: return jsonify({"error": "Sell One Player Failed"}), 400 def add_one_player(self): request_json = json.loads(request.data) name_player_to_add = request_json["add_one_player"] player = Player().get_one(name_player_to_add) if player: if player.get_price() > self.money: return jsonify({"error": "you don't have enough money"}), 400 else: if player.to_json() in self.players: return jsonify({"error": "You already have this player"}), 400 self.players.append(player.to_json()) self.money = self.money - player.get_price() return self.do_save() else: return jsonify({"error": "Add One Player Failed"}), 400
class Wish(db.Document): title = db.StringField(required=True) description = db.StringField() url = db.StringField() image = db.StringField() added_by = db.ReferenceField('User')
class Player(db.Document): name = db.StringField() foot = db.StringField() position = db.StringField() league = db.StringField() price = db.IntField() age = db.IntField() height = db.IntField() overall = db.IntField() crossing = db.IntField() finishing = db.IntField() short_passing = db.IntField() volleys = db.IntField() dribbling = db.IntField() curve = db.IntField() long_passing = db.IntField() ball_control = db.IntField() acceleration = db.IntField() sprint_speed = db.IntField() agility = db.IntField() reactions = db.IntField() balance = db.IntField() shot_power = db.IntField() strength = db.IntField() long_shots = db.IntField() aggression = db.IntField() interceptions = db.IntField() positioning = db.IntField() vision = db.IntField() penalties = db.IntField() marking = db.IntField() standing_tackle = db.IntField() sliding_tackle = db.IntField() gk_diving = db.IntField() gk_handling = db.IntField() gk_kicking = db.IntField() gk_positioning = db.IntField() gk_reflexes = db.IntField() def to_json(self, just_int=False): to_return = { 'price': self.price, 'age': self.age, 'height': self.height, 'overall': self.overall, 'crossing': self.crossing, 'finishing': self.finishing, 'short_passing': self.short_passing, 'volleys': self.volleys, 'dribbling': self.dribbling, 'curve': self.curve, 'long_passing': self.long_passing, 'ball_control': self.ball_control, 'acceleration': self.acceleration, 'sprint_speed': self.sprint_speed, 'agility': self.agility, 'reactions': self.reactions, 'balance': self.balance, 'shot_power': self.shot_power, 'strength': self.strength, 'long_shots': self.long_shots, 'aggression': self.aggression, 'interceptions': self.interceptions, 'positioning': self.positioning, 'vision': self.vision, 'penalties': self.penalties, 'marking': self.marking, 'standing_tackle': self.standing_tackle, 'sliding_tackle': self.sliding_tackle, 'gk_diving': self.gk_diving, 'gk_handling': self.gk_handling, 'gk_kicking': self.gk_kicking, 'gk_positioning': self.gk_positioning, 'gk_reflexes': self.gk_reflexes, } if not just_int: string_to_return = { 'name': self.name, 'foot': self.foot, 'position': self.position, 'league': self.league, } to_return.update(string_to_return) return to_return def __repr__(self): return '{' + self.name + ', ' + self.foot + ', ' + self.position + ', ' + self.league + ', ' + str( self.price ) + ', ' + str(self.age) + ', ' + str(self.height) + ', ' + str( self.overall) + ', ' + str(self.crossing) + ', ' + str( self.finishing) + ', ' + str(self.short_passing) + ', ' + str( self.volleys) + ', ' + str(self.dribbling) + ', ' + str( self.curve ) + ', ' + str(self.long_passing) + ', ' + str( self.ball_control ) + ', ' + str(self.acceleration) + ', ' + str( self.sprint_speed ) + ', ' + str(self.agility) + ', ' + str( self.reactions ) + ', ' + str(self.balance) + ', ' + str( self.shot_power) + ', ' + str( self.strength ) + ', ' + str(self.long_shots) + ', ' + str( self.aggression) + ', ' + str( self.interceptions) + ', ' + str( self.positioning) + ', ' + str( self.vision) + ', ' + str( self.penalties) + ', ' + str( self.marking) + ', ' + str( self.standing_tackle ) + ', ' + str( self.sliding_tackle ) + ', ' + str( self.gk_diving ) + ', ' + str( self.gk_handling ) + ', ' + str( self.gk_kicking ) + ', ' + str( self.gk_positioning ) + ', ' + str( self.gk_reflexes) + '}' def get_one(self, name): player = Player.objects(name=name).first() if player: return player else: return None def get_price(self): return self.price def get_all(self, start, end, filters): _filters_true = [] for key, value in filters.items(): if value: if key == "name" or key == "league": _filters_true.append(key) continue _filters_true.append('-' + key) if start and end: players = Player.objects.order_by(*_filters_true)[start:end] elif start: players = Player.objects.order_by(*_filters_true)[start:] elif end: players = Player.objects.order_by(*_filters_true)[:end] else: players = Player.objects() if players: to_result = [] for player in players: to_result.append(player.to_json()) return jsonify({"players": to_result}), 200 else: return jsonify({"error": " Bad Request"}), 400
class Movie(db.Document): name = db.StringField(required=True, unique=True) casts = db.ListField(db.StringField(), required=True) genres = db.ListField(db.StringField(), required=True) added_by = db.ReferenceField('User')