Skip to content

Commit

Permalink
Merging Backend and Backend-new (#125)
Browse files Browse the repository at this point in the history
* added pylint workflow w/artifacts

run: pylint --fail-under=1 $(find . -name "*.py" | xargs)
note: set the failure threshold as indicated above

actions/starter-workflows#636 (comment)

* Added CodeQL & Pylint badges, edits

* Added CodeQL badge
* Added Pylint badge
* Minor edits: updated git clone steps

* Lowered pylint failure theshold

* Update and rename build.yml to coveralls.yml

* Added test files to pylint.yml

* Updated py files to improve pylint score

* Fixed error in coveralls.yml

* Removed individual tests in pylint.yml

* Added pylint labels

* Added test workflow

* Update and rename nodejsTest.yml to nodejs.yml

* Update nodejs.yml

* Update nodejs.yml

* Added workflow build badge to README

* Added yet another workflow

 All for the sake of a test badge.

* Added test badge

* Revert "group25 workflows"

* Added post to Service.js and connected login form (incomplete)

* Added appropriate error messages and display for errors

* Added Sign up form

* Added error handling and redirecting for login and sign up

* fix to react compile warning

* Dashboard and tag edits are now limited by user

* New tags can now be saved in the database

* Adding Product Controller and Test Cases

* Adding more user auth and product auth test cases

* Changing gitignore

* Fixing test cases for user-auth class

* Removing test cases which are not used

* Commented out unused tests, added pylint comments

* Restored and commented out unused tests
* Added pylint skip comments

* Removing merging backend and backend-new into a single directory

* Increasing Pylint Score

* Increasing Pylint score

Co-authored-by: Leila Moran <lmmoran@ncsu.edu>
Co-authored-by: Emily Tracey <etracey@ncsu.edu>
Co-authored-by: peeyush10234 <ptaneja@ncsu.edu>
  • Loading branch information
4 people committed Nov 3, 2021
1 parent c270a16 commit 25488ac
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 497 deletions.
14 changes: 5 additions & 9 deletions backend-new/app.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# pylint: skip-file
# pylint: disable=pointless-string-statement,undefined-variable,line-too-long

from flask import Flask, render_template, request, url_for, redirect, session
import pymongo
import bcrypt
import json
from flask import Flask
from os import environ

app = Flask(__name__)
from auth_controller import *

from products import *
from product_controller import *
from db_init import db
app.secret_key = "testing"
client = pymongo.MongoClient("mongodb+srv://bot:bot123@cluster0.xph5e.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = client.get_database('feature-hunt')
records = db.user
product_records = db.product

if __name__ == "__main__":
app.run(debug=True, port=environ.get("PORT", 5000) , host='0.0.0.0')
Expand Down
78 changes: 43 additions & 35 deletions backend-new/auth_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

# pylint: disable=wrong-import-position,pointless-string-statement,undefined-variable,line-too-long
from app import *
import json
from flask import request, session, redirect, url_for
from flask import Response
import bcrypt
from app import app
from db_init import records


@app.route("/signup", methods=['POST'])
def signup():
Expand All @@ -11,86 +15,90 @@ def signup():
user_found = records.find_one({"name": user})
email_found = records.find_one({"email": email})
if user_found or email_found:
errorDict = {
"code": 409,
"message":"This email already is already registered.",
"email":email
}
message = json.dumps(errorDict)

error_dict = {
"code": 409,
"message": "This email already is already registered.",
"email": email
}
message = json.dumps(error_dict)
return message

hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
user_input = {'name': user, 'email': email, 'password': hashed}
records.insert_one(user_input)
errorDict = {
"code": 200,
"message":"Registration Successful"
}
message = json.dumps(errorDict)
error_dict = {
"code": 200,
"message": "Registration Successful"
}
message = json.dumps(error_dict)
return message


@app.route('/logged_in')
def logged_in():
print(session)
if "email" in session:
email = session["email"]
name = session["name"]
loggedinDict = {
"code": 200,
"email":email,
"name":name
}
message = json.dumps(loggedinDict)

logged_in_dict = {
"code": 200,
"email": email,
"name": name
}
message = json.dumps(logged_in_dict)
return message
else:
return redirect(url_for("login"))


@app.route("/login", methods=["POST", "GET"])
def login():
message = 'Please login to your account'

if request.method == "POST":
email = request.form.get("email", None)
print(email, flush=True)
password = request.form.get("password", None)

if len(password) == 0 or len(email) == 0:
if password is None or email is None:

return Response(status=403)

email_found = records.find_one({"email": email})
if email_found:
email_val = email_found['email']
passwordcheck = email_found['password']
password_check = email_found['password']
name = email_found['name']

if bcrypt.checkpw(password.encode('utf-8'), passwordcheck):
if bcrypt.checkpw(password.encode('utf-8'), password_check):
session["email"] = email_val
session["name"] = name
return redirect(url_for('logged_in'))
else:
if "email" in session:
return redirect(url_for("logged_in"))
errorDict = {
error_dict = {
"code": 403,
"message":"Password is incorrect"
"message": "Password is incorrect"
}
message = json.dumps(errorDict)
message = json.dumps(error_dict)
return message
else:
errorDict = {
error_dict = {
"code": 403,
"message":"We are unable to find a user with that email. Please double check you entered your email correctly"
"message": "We are unable to find a user with that email. Please double check you entered your email "
"correctly "
}
message = json.dumps(errorDict)
message = json.dumps(error_dict)
return message
loggedinDict = {
"code": 200,
"message":"Sucessfully Logged In"
}
message = json.dumps(loggedinDict)
loggedin_dict = {
"code": 200,
"message": "Sucessfully Logged In"
}
message = json.dumps(loggedin_dict)
return message


@app.route("/logout", methods=["POST", "GET"])
def logout():
if "email" in session:
Expand Down
6 changes: 6 additions & 0 deletions backend-new/db_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pymongo

client = pymongo.MongoClient("mongodb+srv://bot:bot123@cluster0.xph5e.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = client.get_database('feature-hunt')
records = db.user
product_records = db.products
File renamed without changes.
57 changes: 29 additions & 28 deletions backend-new/product_controller.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
from app import *
# pylint: disable=wrong-import-position,pointless-string-statement,undefined-variable,line-too-long

from flask import jsonify
from flask import request
from app import app
from db_init import product_records


@app.route("/addProduct", methods=['Post'])
def addProduct():
def add_product():
try:
product_name = request.form.get("productName")
product_description = request.form.get("productDescription")
image_url = request.form.get("imageUrl")
email = request.form.get("email")

product_input = {'product_name' : product_name, 'product_desc': project_description,
'image_url' : image_url, 'email' : email}
product_input = {'name': product_name, 'description': product_description,
'image_url': image_url, 'users': [email]}

product_records.insert_one(product_input)

return jsonify(success=True)
except:
return jsonify(success=False)

@app.route("/<productName>/getFeature", method=['Get'])
def getFeature(productName):
result = product_record.find({"productName"})
return dumps(result)


@app.route("/<productName>/addFeature", method=['Post'])
def addFeature(productName):
if request.method == 'POST':
data = request.json
data['_id'] = ObjectId()
print(data)
if data is None or data == {}:
return Response(response=json.dumps({"Error": "Please provide connection information"}),
status=400,
mimetype='application/json')
result = product_records.find_one_and_update({"project_name": productName}, {"$push": {"features": data}})

return jsonify(success=True)

elif request.method == 'GET':
result = mongo.db.products.find({"name": productname}, {"features": 1})
return dumps(result)

# @app.route("/<productName>/addFeature", method=['Post'])
# def addFeature(productName):
# if request.method == 'POST':
# data = request.json
# data['_id'] = ObjectId()
# print(data)
# if data is None or data == {}:
# return Response(response=json.dumps({"Error":
# "Please provide connection information"}),
# status=400,
# mimetype='application/json')
# result = product_records.find_one_and_update(
# {"project_name": productName}, {"$push": {"features": data}}
# )
#
# return jsonify(success=True)
#
# elif request.method == 'GET':
# result = mongo.db.products.find({"name": productname}, {"features": 1})
# return dumps(result)
109 changes: 109 additions & 0 deletions backend-new/products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
Copyright (C) 2021 Feature Hunt - All Rights Reserved
You may use, distribute and modify this code under the terms of the MIT license.
You should have received a copy of the XYZ license with
this file. If not, please write to: featurehuntteam@gmail.com
"""

# pylint: disable=wrong-import-position,poiackend-lontless-string-statement,undefined-variable,line-too-long

import os
from sys import stderr
from flask import request, jsonify
from flask import json
from app import app

from bson.json_util import dumps
from bson.objectid import ObjectId
from db_init import product_records

'''
Function: products
Description: Get/ Add/ Update/ Delete the products from the database
Inputs:
- NA
Outputs:
- NA
'''


@app.route('/products', methods=['GET', 'POST', 'DELETE', 'PATCH'])
def products():
if request.method == 'GET':
data = product_records.find()
return dumps(data)

data = request.get_json()

if request.method == 'POST':
if data is None or data == {}:
return Response(response=json.dumps({"Error": "Please provide all necessary input"}),
status=400,
mimetype='application/json')
return jsonify({'ok': True, 'message': 'Product added successfully'}), 200

if request.method == 'DELETE':
if data is None or data == {}:
return Response(response=json.dumps({"Error": "Please provide all necessary input"}),
status=400,
mimetype='application/json')

db_response = product_records.delete_one({'id': data[id]})
if db_response.deleted_count == 1:
response = {'ok': True, 'message': 'record deleted'}
else:
response = {'ok': True, 'message': 'no record found'}
return jsonify(response), 200

if request.method == 'PATCH':
if data.get('query', {}) != {}:
product_records.update_one(
data['query'], {'$set': data.get('payload', {})})
return jsonify({'ok': True, 'message': 'record updated'}), 200
else:
return jsonify({'ok': False, 'message': 'Bad request parameters!'}), 400


'''
Function: get_feature
Description: Get the list of all features for given product name
Inputs:
- productName: Name of the product
Outputs:
- results: List of features that are available in that product
'''


@app.route('/<productname>/getFeature', methods=['GET', 'POST'])
def get_feature(product_name):
if request.method == 'GET':
data = product_records.find({"name": product_name})
return dumps(data)


'''
Function: features
Description: You can add/get features of a product
Inputs:
- productName: Name of the product
Outputs:
- results: Add features to that product or return feature list
'''


@app.route('/<productname>/features', methods=['GET', 'POST'])
def features(product_name):
result = ''
if request.method == 'POST':
data = request.form.get('features')
data = json.loads(data)
print(data, flush=True)
if data is None or data == {}:
return Response(response=json.dumps({"Error": "Please provide connection information"}),
status=400,
mimetype='application/json')
result = product_records.find_one_and_update({"name": productname}, {"$set": {"features": data}})

elif request.method == 'GET':
result = product_records.find({"name": product_name}, {"features": 1})
return dumps(result)
9 changes: 0 additions & 9 deletions backend/Dockerfile

This file was deleted.

0 comments on commit 25488ac

Please sign in to comment.