-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
334 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
''' | ||
Our Main api routes | ||
''' | ||
from functools import wraps | ||
from flask import jsonify, request | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
''' Input Validation Classes ''' | ||
from api.v1.validations import Validations | ||
|
||
|
||
# Registration validations | ||
REGISTER_DRIVER = [ | ||
{'name': [('string', True), ('minimum', 1), | ||
('maximum', 30), ('required', True)]}, | ||
{'email': [('minimum', 6), ('maximum', 30), | ||
('required', True), ('email', True)]}, | ||
{'phone': [('minimum', 8), ('maximum', 10), ('required', True)]}, | ||
{'address': [('minimum', 6), ('maximum', 30)]}, | ||
{'license_number': [('minimum', 6), ('maximum', 30)]}, | ||
{'license_expiry': [('minimum', 6), ('maximum', 30)]}, | ||
{'motocycle_make': [('minimum', 6), ('maximum', 30)]}, | ||
{'motocycle_model': [('minimum', 6), ('maximum', 30)]}, | ||
{'motocycle_year': [('minimum', 2), ('maximum', 4)]}, | ||
] | ||
# Login validation | ||
LOGIN_RULES = [ | ||
{'email': [('minimum', 6), ('maximum', 30), | ||
('required', True), ('email', True)]}, | ||
{'password': [('minimum', 6), ('required', True)]}, | ||
{'station': [('minimum', 1)]} | ||
] | ||
# Change password validations | ||
REGISTER_STATION = [ | ||
{'name': [('minimum', 3), ('maximum', 30), ('required', True)]}, | ||
{'location': [('minimum', 3), ('maximum', 30), ('required', True)]}, | ||
] | ||
# Reset password validations | ||
REGISTER_BATTERY_RULES = [ | ||
{'battery_type': [('minimum', 3), ('maximum', 14), | ||
('required', True)]}, | ||
{'manufacture_date': [('minimum', 2), ('maximum', 4), ('required', True)]}, | ||
{'serial_number': [('minimum', 5), ('maximum', 30), | ||
('required', True)]}, | ||
{'station': [('minimum', 1), ('maximum',10), | ||
('required', True)]}, | ||
] | ||
|
||
# Reset password validations | ||
REGISTER_SWAP_RULE = [ | ||
{'battery': [('required', True), ('minimum', 1), | ||
('maximum', 10)]}, | ||
{'driver': [('required', True), ('minimum', 1), | ||
('maximum', 10)]} | ||
] | ||
|
||
REGISTER_MOVEMENT_RULE = [ | ||
{'lat': [('required', True)]}, | ||
{'long': [('required', True)]}, | ||
{'battery_percentage': [('required', True), ('minimum', 1), | ||
('maximum', 2)]} | ||
] | ||
|
||
|
||
def validate(inputs, all_rules): | ||
''' Register validation method ''' | ||
error_bag = {} | ||
valid = Validations(inputs) | ||
for rules in all_rules: | ||
for key in rules: | ||
rule_key = key | ||
for rule in rules[rule_key]: | ||
execute = getattr(valid, rule[0])(rule_key, rule[1]) | ||
if execute is True: | ||
pass | ||
if execute is not True: | ||
if rule_key in error_bag: | ||
error_bag[rule_key].append(execute) | ||
else: | ||
error_bag[rule_key] = [] | ||
error_bag[rule_key].append(execute) | ||
if len(error_bag) is not 0: | ||
return error_bag | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
''' | ||
Validations Methods Class | ||
''' | ||
|
||
import re | ||
|
||
|
||
class Validations(): | ||
'''Validations class''' | ||
|
||
def __init__(self, all_inputs): | ||
''' All inputs dictionary should be available to the class''' | ||
for key, value in all_inputs.items(): | ||
if (all_inputs[key] is not None and | ||
not isinstance(all_inputs[key], int)): | ||
if str(all_inputs[key]).strip() == '': | ||
all_inputs[key] = None | ||
self.all = all_inputs | ||
|
||
def string(self, key, string): | ||
'''Check if input is required''' | ||
if key in self.all and self.all[key] is not None: | ||
if not re.match(r"[^[a-zA-Z0-9]+$", self.all[key]): | ||
return True | ||
return key.capitalize() + " should be string" | ||
return True | ||
|
||
def minimum(self, key, minimum): | ||
'''Check required character size''' | ||
if key in self.all and self.all[key] is not None: | ||
if len(str(self.all[key])) < int(minimum): | ||
return "{} should not be less than {} characters".format( | ||
key.capitalize(), str(minimum)) | ||
return True | ||
return True | ||
|
||
def maximum(self, key, maximum): | ||
'''Check required character size''' | ||
if key in self.all and self.all[key] is not None: | ||
if len(str(self.all[key])) > int(maximum): | ||
return "{} should not be greater than {} characters".format( | ||
key.capitalize(), str(maximum) | ||
) | ||
return True | ||
return True | ||
|
||
def email(self, key, email): | ||
'''Check required character size''' | ||
if key in self.all and self.all[key] is not None: | ||
if not re.match(r"[^@\s]+@[^@\s]+\.[a-zA-Z]+$", self.all[key]): | ||
return "Invalid email address" | ||
return True | ||
return True | ||
|
||
def required(self, key, is_required=True): | ||
'''Check input it is required''' | ||
if key in self.all: | ||
if self.all[key] is None or str(self.all[key]).strip() == '': | ||
return key.capitalize() + " should not be empty" | ||
return True | ||
return key.capitalize() + " is required" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
from api.v1 import auth | ||
from sqlalchemy import desc, func | ||
|
||
from api.v1.inputs.inputs import LOGIN_RULES, REGISTER_DRIVER, validate | ||
|
||
|
||
creds = { | ||
"[email protected]": { | ||
|
@@ -27,25 +29,34 @@ | |
|
||
@app_views.route("/login", methods=["POST"], strict_slashes=False) | ||
def login(): | ||
sent_data = request.get_json() | ||
sent_data = request.get_json(force=True) | ||
|
||
valid = validate(sent_data, LOGIN_RULES) | ||
|
||
if valid is not True: | ||
return jsonify( | ||
status='error', | ||
message="Please provide valid details", | ||
errors=valid), 400 | ||
|
||
email = sent_data["email"] | ||
password = sent_data["password"] | ||
station_id = sent_data.get("station_id", None) | ||
station = sent_data.get("station", None) | ||
|
||
if email in creds and password == creds[email]["password"]: | ||
|
||
# check if he is an admin | ||
if station_id is None and creds[email]["role"] == "admin": | ||
if station is None and creds[email]["role"] == "admin": | ||
# Generate JWT token | ||
token = get_token(creds[email]) | ||
|
||
# Return the token as a JSON response | ||
return jsonify({"email": email, "token": token}) | ||
|
||
# check if a user is a manager | ||
elif station_id is not None and creds[email]["role"] == "manager": | ||
elif station is not None and creds[email]["role"] == "manager": | ||
# append the station_id to the user obj | ||
creds[email]["station_id"] = station_id | ||
creds[email]["station_id"] = station | ||
|
||
# Generate JWT token | ||
token = get_token(creds[email]) | ||
|
@@ -61,7 +72,16 @@ def login(): | |
@app_views.route("drivers/addriver", methods=["POST"], strict_slashes=False) | ||
def create_driver(): | ||
try: | ||
sent_data = request.get_json() | ||
sent_data = request.get_json(force=True) | ||
|
||
valid = validate(sent_data, REGISTER_DRIVER) | ||
|
||
if valid is not True: | ||
return jsonify( | ||
status='error', | ||
message="Please provide valid details", | ||
errors=valid), 400 | ||
|
||
data = { | ||
"name": sent_data.get("name"), | ||
"email": sent_data.get("email"), | ||
|
@@ -83,7 +103,7 @@ def create_driver(): | |
return jsonify({ | ||
"status": "error", | ||
"message":( | ||
"You have already " "registered driver with the same name"), | ||
"You have alreadyregistered driver with the same name"), | ||
}), 400 | ||
|
||
resp = Driver.save(data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.