Пример #1
0
def enter_rooms():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    if not user:
        return False
    

    my_conversations = Conversation.query.filter(or_(Conversation.user_owner_id == user.id,
                                                        Conversation.user_guest_id == user.id))
    rooms_joined = 0
    for c in my_conversations:
        join_room(c.id)
        rooms_joined = rooms_joined + 1
        emit('room_response', "Joined a room",room=c.id)
    
    if user.id in waiting_conversations:
        c = waiting_conversations[user.id]
        del waiting_conversations[user.id]
        print(c)
        other_id = c.user_guest_id if user.id == c.user_owner_id else c.user_owner_id
        print(other_id)
        other_account = Accounts.query.filter(Accounts.id == other_id).first()
        print(other_account)
        other_name = other_account.first_name + ' ' + other_account.last_name
        print(other_name)
        other_id = other_account.id
        emit('conversation_received', {
            "id": c.id,
            "other_id": other_id,
            "text": "Send a message now!",
            "time": datetime.now().isoformat(),
            "other_name": other_name
        },room=c.id)
Пример #2
0
def handle_classroom():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    all_users_query = Accounts.query.all()
    following = []
    all_users = []

    for u in all_users_query:
        if Followers.query.filter(Followers.follower == user.id,
                                  Followers.leader == u.id).first():
            following.append(u)
            u.following = True
        all_users.append(u)

    following = sorted(following, key=lambda leader: leader.last_name)
    all_users = sorted(all_users, key=lambda au: au.last_name)

    return render_template('classroom.html',
                           classroom=all_users,
                           client=user,
                           following=following)
Пример #3
0
def on_connect():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    if not user:
        return False
    active_sockets[request.sid] = user
Пример #4
0
def follow_user():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    data = request.get_json()
    leader = Accounts.query.filter(Accounts.id == data['id']).first()
    leader_obj = {
        'last_name': leader.last_name,
        'first_name': leader.first_name,
        'id': leader.id,
        'email': leader.email
    }

    if Followers.query.filter(Followers.follower == user.id,
                              Followers.leader == data['id']).first():
        return jsonify({'success': 200, 'user': leader_obj}), 200

    if not leader:
        return jsonify({'success': 404}), 404

    db.session.add(Followers(follower=user.id, leader=data['id']))
    db.session.commit()

    return jsonify({'success': 200, 'user': leader_obj}), 200
Пример #5
0
def conversationsCRUD():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return Response(status=404)

    if request.method == 'GET':
        conversation_id = request.args['conversation_id']
        conversation = Conversation.query.filter(
            Conversation.id == conversation_id).first()
        if not conversation:
            return Response(status=404)
        if not (conversation.user_guest_id == user.id
                or conversation.user_owner_id == user.id):
            return Response(status=403)

        data = get_all_messages_of_conversation(conversation_id, user.id)
        return jsonify(data)

    elif request.method == 'POST':
        user_guest_id = request.form['guest_user']
        conversation = Conversation(user_owner_id=user.id,
                                    user_guest_id=user_guest_id)
        db.session.add(conversation)
        db.session.commit()
        emit_conversation(conversation)
        return jsonify({"id": conversation.id})
Пример #6
0
def messagesCRUD():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return Response(status=404)

    if request.method == 'POST':
        text = request.form['text']
        conversation_id = request.form['conversation_id']
        time = datetime.now()
        text = replace(request.form['text'])

        message = Message(conversation_id=conversation_id,
                          text=text,
                          time=time,
                          sender_id=user.id)
        db.session.add(message)
        db.session.commit()

        account_sender = Accounts.query.filter(Accounts.id == user.id).first()
        name_of_sender = account_sender.first_name + ' ' + account_sender.last_name

        emit_message(conversation_id, message, name_of_sender)

        return Response(status=200)
Пример #7
0
def messages():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    # Getting the conversations for the side bar

    conversation_summary = get_users_conversation_bar(user.id)

    users_to_start_conversation = Accounts.query.filter(Accounts.id != user.id)

    messages_of_top_conversation = []
    if conversation_summary:
        messages_of_top_conversation = get_all_messages_of_conversation(
            conversation_summary[0]["id"], user.id)

    return render_template(
        'messages.html',
        potential_conversation_users=users_to_start_conversation,
        conversations_with_all=conversation_summary,
        messages_of_top_conversation=messages_of_top_conversation,
        uid=user.id)
Пример #8
0
def assignments():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    return render_template('assignments.html')
Пример #9
0
def conversations():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    user_guest_id = request.form['guest_user']
    conversation = Conversation(user_owner_id=user.id,
                                user_guest_id=user_guest_id)
    db.session.add(conversation)
    db.session.commit()

    return jsonify({'success': True})
Пример #10
0
def home_page():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    users_name = user.first_name + ' ' + user.last_name
    # We now have info about the user and can put their info in the top right
    return render_template('home.html', name=users_name)
Пример #11
0
def post_comment_to_question(q_id):
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    if not os.path.exists(app.config['UPLOAD_FOLDER']):
        os.makedirs(app.config['UPLOAD_FOLDER'])

    form_text = request.form['comment-string']

    form_text = replace(form_text)

    print(form_text)

    comment = Comment(text=form_text,
                      likes=0,
                      question_id=q_id,
                      user_id=user.id)

    #  msg = msg.replace(">","&gt;"); msg = msg.replace("<","&lt;"); msg = msg.replace("&", "&amp;");
    #  </div><script>alert('PeePeePooPoo');</script>

    db.session.add(comment)
    db.session.commit()

    attachments = []
    for file in request.files.getlist('attachments-input'):
        if file.filename != '':
            client_file_name = escape(secure_filename(file.filename))
            extension = os.path.splitext(client_file_name)[1]
            server_file_name = uuid.uuid4().hex + extension
            server_path = os.path.join(app.config['UPLOAD_FOLDER'],
                                       server_file_name)
            href_path = remove_prefix(server_path, "/app/biazza/static")
            attachment = Attachment(user_filename=client_file_name,
                                    path=href_path,
                                    comment_id=comment.id)
            db.session.add(attachment)
            db.session.commit()
            attachments.append(attachment)
            file.save(
                os.path.join(app.config['UPLOAD_FOLDER'], server_file_name))
            print("Attachment saved: " + repr(attachment))
    emit_comment(comment, attachments)
    return jsonify({'success': True})
Пример #12
0
def get_question(q_id):
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    try:
        q = Question.query.filter(Question.id == q_id).one()
    except:
        return Response(status=404)

    poster = Accounts.query.filter(Accounts.id == q.user_id).first()
    q.user_name = poster.first_name + ' ' + poster.last_name

    comments = Comment.query.filter(Comment.question_id == q_id)
    comments_json = []
    for c in comments:
        c.all_attachments = Attachment.query.filter(
            Attachment.comment_id == c.id)
        comment_poster = Accounts.query.filter(
            Accounts.id == c.user_id).first()
        c.user_name = comment_poster.first_name + ' ' + comment_poster.last_name

        attachments_json = []
        for a in c.all_attachments:
            a = {"path": a.path, "name": a.user_filename}
            attachments_json.append(a)
        c.all_attachments = attachments_json
        comments_json.append({
            'c_id': c.id,
            'c_text': c.text,
            'c_likes': c.likes,
            'attachments': c.all_attachments,
            'name': c.user_name
        })

    return jsonify({
        'id': q_id,
        'name': q.user_name,
        'title': q.title,
        'content': q.content,
        'likes': q.likes,
        'comments': comments_json
    })
Пример #13
0
def unfollow_user():
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    data = request.get_json()

    follow_obj = Followers.query.filter(
        Followers.follower == user.id, Followers.leader == data['id']).first()

    if follow_obj:
        db.session.delete(follow_obj)
        db.session.commit()

    return jsonify({'success': 200}), 200
Пример #14
0
def home():
    if request.method == 'GET':
        print("IN GET")
        token = request.cookies.get('biazza_token')
        print(token)
        if token and get_user_with_token(token):
            return redirect('/home')
        return render_template("login.html")

    else:
        form_data = request.form

        email = form_data.get("email")
        password = form_data.get("password")

        email = replace(email)
        password = replace(password)

        password = password.encode('utf-8')

        # verify that the user is present in the database if not present stay, else go to the home page

        emails_query = Accounts.query.filter_by(email=email).first()

        if emails_query is None:
            return jsonify("email_not_found")
        else:

            # check if the password is valid Abcdef1!
            stored_password = emails_query.password
            stored_password = stored_password.encode('utf-8')

            hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())

            if bcrypt.checkpw(password, stored_password):
                token = create_token_for_user(emails_query.id)
                response = make_response(jsonify("Success"))
                response.set_cookie('biazza_token', token)
                return response
            else:
                return jsonify("invalid_password")
Пример #15
0
def question_filter_status(q_id, f):
    token = request.cookies.get('biazza_token')
    user = get_user_with_token(token)

    # If the user could not be found in the db make them login
    if not user:
        return render_template("login.html")

    question = Question.query.filter(Question.id == q_id).first()

    if not question:
        return jsonify(False), 404

    if f == 'me':
        return jsonify(question.user_id == user.id)
    elif f == 'following':
        return jsonify(not Followers.query.filter(
            Followers.follower == user.id, Followers.leader ==
            question.user_id).first() is None)

    return jsonify(False), 404
Пример #16
0
def questions_handler():
    if request.method == 'GET':

        token = request.cookies.get('biazza_token')
        user = get_user_with_token(token)

        # If the user could not be found in the db make them login
        if not user:
            return render_template("login.html")

        questions = []
        questions_filter = request.args.get("filter")
        if not questions_filter or questions_filter == 'all':
            questions_filter = 'all'
            questions = Question.query.all()
        elif questions_filter == 'following':
            questions = Question.query.filter(
                Question.user_id.in_(
                    db.session.query(Followers.leader).filter(
                        Followers.follower == user.id))).all()
        elif questions_filter == 'me':
            questions = Question.query.filter(
                Question.user_id == user.id).all()

        questions.reverse()

        top_question = None
        comments = []

        if questions:
            top_question = questions[0]
            comments = Comment.query.filter(
                Comment.question_id == top_question.id).all()

            top_question_poster = Accounts.query.filter(
                Accounts.id == top_question.user_id).first()
            top_question.user_name = top_question_poster.first_name + ' ' + top_question_poster.last_name

        for i in range(len(comments)):
            comments[i].all_attachments = Attachment.query.filter(
                Attachment.comment_id == comments[i].id)
            comment_poster = Accounts.query.filter(
                Accounts.id == comments[i].user_id).first()
            comments[
                i].user_name = comment_poster.first_name + ' ' + comment_poster.last_name

        return render_template('questions.html',
                               comments=comments,
                               questions=questions,
                               top_question=top_question,
                               filter=questions_filter)

    elif request.method == 'POST':

        token = request.cookies.get('biazza_token')
        user = get_user_with_token(token)

        # If the user could not be found in the db make them login
        if not user:
            return render_template("login.html")

        question_title = replace(request.form['title-input'])
        question_contents = replace(request.form['question-input'])

        if len(question_title) > 0 and len(question_contents) > 0:
            # Need to handle file uploads and clean input from users
            question = Question(title=question_title,
                                content=question_contents,
                                likes=0,
                                user_id=user.id)
            db.session.add(question)
            db.session.commit()
            emit_question(question, [])
            return jsonify({'success': True})
        else:
            return Response(status=400)