Esempio n. 1
0
 def post(self):
     args = user_parser_for_adding.parse_args(strict=True)
     session = db_session.create_session()
     if session.query(User).filter(
             User.email == args['email']).first() is not None:
         abort(400,
               success=False,
               message=f"User with email {args['email']} already exists")
     if session.query(User).filter(
             User.username == args['username']).first() is not None:
         abort(400,
               success=False,
               message=f"User {args['username']} already exists")
     user = User(email=args['email'],
                 username=args['username'],
                 first_name=args['first_name'],
                 last_name=args['last_name'],
                 reg_date=datetime.datetime.now())
     user.set_password(args['password'])
     token = user.get_token()
     session.add(user)
     session.commit()
     return jsonify({
         'success': True,
         'authToken': {
             'token': token,
             'expires': str(user.token_expiration)
         }
     })
Esempio n. 2
0
def add_users_to_project(project_id):
    session = db_session.create_session()
    project = session.query(Project).get(project_id)
    if not project:
        abort(404, message=f"Project {project_id} not found")
    if project.team_leader != g.current_user:
        abort(403)

    added_users = []
    for user_id in request.form.getlist('id'):
        user = session.query(User).get(user_id)
        if not user:
            abort(404, message=f"User {user_id} not found")
        if int(user_id) not in list(map(lambda x: x.id, project.users)):
            project.users.append(user)
            added_users.append(user)

    session.commit()
    return jsonify({
        'success':
        True,
        "users": [
            user.to_dict(only=('id', 'email', 'username', 'first_name',
                               'last_name')) for user in added_users
        ]
    })
Esempio n. 3
0
 def get(self, chat_id):
     session = db_session.create_session()
     chat = session.query(Chat).get(chat_id)
     return jsonify({
         'chat': chat.to_dict_myself(),
         'users': [item.to_dict_myself() for item in chat.users]
     })
Esempio n. 4
0
 def post(self):
     args = task_parser_for_adding.parse_args()
     session = db_session.create_session()
     project = session.query(Project).get(args['project_id'])
     if project is None:
         abort(404,
               success=False,
               message=f"Project {args['project_id']} not found")
     if project not in g.current_user.projects:
         abort(403, success=False)
     if args['title'] in map(lambda x: x.title, project.tasks):
         abort(400,
               success=False,
               message=f"Task with title '{args['title']}' already exists")
     task = Task(project_id=args['project_id'],
                 title=args['title'],
                 description=args['description'],
                 duration=args['duration'],
                 creator_id=g.current_user.id,
                 worker_id=args['worker_id'],
                 tag=args['tag'],
                 color=args['color'],
                 condition=args['condition'],
                 image=args['image'],
                 date=datetime.datetime.now())
     if args['items']:
         for title, description in args['items'].items():
             task.items.append(
                 TaskItem(title=title, description=description))
     session.add(task)
     session.commit()
     return jsonify({'success': True})
Esempio n. 5
0
 def post(self):
     args = message_parser_for_adding.parse_args(strict=True)
     session = db_session.create_session()
     chat = session.query(Chat).get(args['chat_id'])
     # Если чат не найден
     if chat is None:
         abort(404,
               success=False,
               message=f"Chat {args['chat_id']} not found")
     # Если текущий пользователь не состоит в чате
     if chat not in g.current_user.chats:
         abort(403, success=False)
     # Если пытаются отправить пустое сообщение
     if not args["message"]:
         abort(400, success=False, message="Message can not be empty")
     message = Message(chat_id=args['chat_id'],
                       user_id=g.current_user.id,
                       message=args['message'],
                       date=datetime.datetime.now())
     session.add(message)
     session.commit()
     return jsonify({
         'success': True,
         'user': message.user.to_dict_myself(),
         'date': ":".join(str(message.date).split(":")[:-1]),
         'id': message.id,
     })
Esempio n. 6
0
def get_project_team_leader(project_id):
    session = db_session.create_session()
    project = session.query(Project).get(project_id)
    if not project:
        abort(404, message=f"Project {project_id} not found")
    if project not in g.current_user.projects:
        abort(403)
    return jsonify(project.team_leader.to_dict_myself())
Esempio n. 7
0
 def delete(self, chat_id):
     session = db_session.create_session()
     chat = session.query(Chat).get(chat_id)
     if chat.creator != g.current_user:
         abort(403, success=False)
     session.delete(chat)
     session.commit()
     return jsonify({'success': True})
Esempio n. 8
0
 def delete(self, task_item_id):
     session = db_session.create_session()
     task_item = session.query(TaskItem).get(task_item_id)
     if not (g.current_user == task_item.task.creator or g.current_user == task_item.task.worker):
         abort(403, success=False)
     session.delete(task_item)
     session.commit()
     return jsonify({'success': True})
Esempio n. 9
0
 def delete(self, message_id):
     session = db_session.create_session()
     message = session.query(Message).get(message_id)
     if g.current_user != message.chat.user:
         abort(403, success=False)
     session.delete(message)
     session.commit()
     return jsonify({'success': True})
Esempio n. 10
0
 def new_func(self, message_id):
     session = db_session.create_session()
     message = session.query(Message).get(message_id)
     if not message:
         abort(404,
               success=False,
               message=f"Message {message_id} not found")
     return func(self, message_id)
Esempio n. 11
0
 def delete(self, task_id):
     session = db_session.create_session()
     task = session.query(Task).get(task_id)
     if g.current_user != task.creator:
         abort(403, success=False)
     session.delete(task)
     session.commit()
     return jsonify({'success': True})
Esempio n. 12
0
 def new_func(self, project_id):
     session = db_session.create_session()
     project = session.query(Project).get(project_id)
     if not project:
         abort(404,
               success=False,
               message=f"Project {project_id} not found")
     return func(self, project_id)
Esempio n. 13
0
 def delete(self, project_id):
     session = db_session.create_session()
     project = session.query(Project).get(project_id)
     if project.team_leader != g.current_user:
         abort(403, success=False)
     session.delete(project)
     session.commit()
     return jsonify({'success': True})
Esempio n. 14
0
def verify_token(token):
    session = db_session.create_session()
    user = session.query(User).filter(User.token == token).first()
    if user is None or user.token_expiration < datetime.datetime.now():
        return False
    g.db_session = session
    g.current_user = user
    return True
Esempio n. 15
0
 def get(self):
     session = db_session.create_session()
     messages = session.query(Message).filter(
         Message.user_id == g.current_user.id)
     return jsonify({
         'messages': [{
             'message': message.to_dict_myself()
         } for message in messages],
     })
Esempio n. 16
0
def get_project_users(project_id):
    session = db_session.create_session()
    project = session.query(Project).get(project_id)
    if not project:
        abort(404, message=f"Project {project_id} not found")
    if project not in g.current_user.projects:
        abort(403)
    users = project.users
    return jsonify({'users': [user.to_dict_myself() for user in users]})
Esempio n. 17
0
def verify_password(username, password):
    session = db_session.create_session()
    user = session.query(User).filter((User.username == username) | (User.email == username)).first()
    if user is None:
        return False
    if user.check_password(password):
        g.db_session = session
        g.current_user = user
        return True
    return False
 def put(self, user_id):
     args = element_parser_for_updating.parse_args(
         strict=True)  # Вызовет ошибку, если запрос
     # будет содержать поля, которых нет в парсере
     session = db_session.create_session()
     element = session.query(Element).get(user_id)
     for key, value in args.items():
         if value is not None:
             exec(f"element.{key} = '{value}'")
     session.commit()
     return jsonify({'success': True})
Esempio n. 19
0
def get_username_project(username, project_name):
    session = db_session.create_session()
    user = session.query(User).filter(User.username == username).first()
    if user is None:
        abort(404, message=f"User {username} not found")
    project = user.projects[list(map(lambda p: p.project_name,
                                     user.projects)).index(project_name)]
    return jsonify({
        'project': project.to_dict_myself(),
        'users': [item.id for item in project.users]
    })
 def post(self):
     args = element_parser_for_adding.parse_args(strict=True)
     session = db_session.create_session()
     element = Element(short_name=args['short_name'],
                       full_name=args['full_name'],
                       description=args['description'],
                       mass=args['mass'],
                       row=args['row'],
                       column=args['column'])
     session.add(element)
     session.commit()
     return jsonify({'success': True})
Esempio n. 21
0
 def get(self, project_id):
     session = db_session.create_session()
     project = session.query(Project).get(project_id)
     if project not in g.current_user.projects:
         abort(403, success=False)
     return jsonify({
         'project':
         project.to_dict_myself(),
         'team_leader':
         project.team_leader.to_dict_myself(),
         'users': [user.to_dict_myself() for user in project.users]
     })
Esempio n. 22
0
 def get(self, task_id):
     session = db_session.create_session()
     task = session.query(Task).get(task_id)
     if g.current_user not in task.project.users:
         abort(403, success=False)
     return jsonify({
         'task':
         task.to_dict_myself(),
         'items': [item.to_dict_myself() for item in task.items],
         'canYouEdit':
         g.current_user == task.worker or g.current_user == task.creator,
     })
Esempio n. 23
0
def get_user_by_name(username):
    session = db_session.create_session()
    user = session.query(User).filter(User.username == username).first()
    if user is None:
        abort(404, message=f"User {username} not found")
    return jsonify({
        'user':
        user.to_dict_myself(),
        'projects': [{
            'project': project.to_dict_myself(),
            'team_leader': project.team_leader.to_dict_myself()
        } for project in user.projects]
    })
Esempio n. 24
0
 def put(self, task_item_id):
     args = task_item_parser_for_updating.parse_args(strict=True)
     session = db_session.create_session()
     task_item = session.query(TaskItem).get(task_item_id)
     if not (g.current_user == task_item.task.creator or g.current_user == task_item.task.worker):
         abort(403, success=False)
     if 'title' in args and args['title'] in map(lambda x: x.title, task_item.task.items):
         abort(400, success=False, message=f"Task item with title '{args['title']}' already exists")
     for key, value in args.items():
         if value is not None:
             exec(f"task_item.{key} = '{value}'")
     session.commit()
     return jsonify({'success': True})
Esempio n. 25
0
def set_task_condition(task_id, condition):
    session = db_session.create_session()

    task = session.query(Task).get(task_id)
    if not task:
        abort(404, message=f"Task {task_id} not found")
    if not (g.current_user == task.worker or g.current_user == task.creator):
        abort(403)
    if condition not in (0, 1, 2):
        abort(400, message=f"Condition must be one of (0, 1, 2)")
    task.condition = condition
    session.commit()
    return jsonify({'success': True, 'condition': condition})
Esempio n. 26
0
 def put(self, message_id):
     args = message_parser_for_updating.parse_args(
         strict=True)  # Вызовет ошибку, если запрос
     # будет содержать поля, которых нет в парсере
     session = db_session.create_session()
     message = session.query(Message).get(message_id)
     # Если пользователь пытается редактировать не свое сообщение
     if g.current_user != message.chat.user:
         abort(403, success=False)
     for key, value in args.items():
         if value is not None:
             exec(f"message.{key} = '{value}'")
     session.commit()
     return jsonify({'success': True})
Esempio n. 27
0
def search_user_by_username(username):
    session = db_session.create_session()
    users = session.query(User).filter(User.username.ilike(f"%{username}%"))
    if request.args.get("project_id", False):
        project = session.query(Project).get(request.args.get("project_id"))
        users = users.filter(
            User.id.notin_(list(map(lambda x: x.id, project.users))))
    users = users.all()

    return jsonify({
        "success": True,
        "users": [user.to_dict_myself() for user in users],
        'found': len(users)
    })
Esempio n. 28
0
def get_project_chats(project_id):
    session = db_session.create_session()
    project = session.query(Project).get(project_id)
    if not project:
        abort(404, message=f"Project {project_id} not found")
    if project not in g.current_user.projects:
        abort(403)
    chats = project.chats
    return jsonify({
        'chats': [{
            'chat': chat.to_dict_myself(),
            'users': [item.id for item in chat.users]
        } for chat in chats]
    })
Esempio n. 29
0
def get_chat_messages(chat_id):
    session = db_session.create_session()
    chat = session.query(Chat).get(chat_id)
    # Если передан параметр last_message_id, возвращаем только те сообщения, которые были после
    # сообщения с этим id. Иначе - возвращаем все
    last_message_id = int(request.args.get("last_message_id", 0))
    messages = filter(lambda message: message.id > last_message_id,
                      chat.messages)
    messages = sorted(messages, key=lambda x: x.date)
    return jsonify({
        'messages': [{
            'message': message.to_dict_myself(),
            'user': message.user.to_dict_myself()
        } for message in messages],
    })
Esempio n. 30
0
def add_user_to_project(project_id, user_id):
    session = db_session.create_session()
    project = session.query(Project).get(project_id)
    if not project:
        abort(404, message=f"Project {project_id} not found")
    if project.team_leader != g.current_user:
        abort(403)
    user = session.query(User).get(user_id)
    if not user:
        abort(404, message=f"User {user_id} not found")

    if user.id not in list(map(lambda x: x.id, project.users)):
        project.users.append(user)

    session.commit()
    return jsonify({'success': True})