New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #832 : Adds JSON-API standards #847
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
from flask import Blueprint, jsonify, request | ||
from firebase_admin import auth | ||
from api.utils.response import Response | ||
from api.models.user import User | ||
from api.schemas.user import UserSchema | ||
|
||
|
||
router = Blueprint('registerUser', __name__) | ||
|
||
|
||
@router.route('/register', methods=['POST']) | ||
def registerUser(): | ||
try: | ||
data = request.get_json() | ||
except Exception as e: | ||
return jsonify( | ||
Response(500).exceptWithMessage( | ||
str(e), | ||
'Unable to get json')) | ||
|
||
def register_user(): | ||
schema = UserSchema() | ||
input_data = request.get_json() | ||
data, err = schema.load(input_data) | ||
if err: | ||
return err | ||
user = auth.create_user( | ||
email=data['email'], | ||
email_verified=False, | ||
|
@@ -29,14 +27,6 @@ def registerUser(): | |
email=user.email, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, but what ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a user for backend and a user for firebase. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabru-md ignore this comment, I have seen something wrong 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of many |
||
password=data['password']) | ||
|
||
try: | ||
newUser.save_to_db() | ||
except Exception as e: | ||
return jsonify( | ||
Response(401).exceptWithMessage( | ||
str(e), | ||
'User already exists with the same Username')) | ||
newUser.save_to_db() | ||
|
||
return jsonify( | ||
Response(200).generateMessage( | ||
'User created successfully')) | ||
return jsonify(schema.dump(newUser)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
from api.db import db | ||
import uuid | ||
|
||
|
||
class File(db.Model): | ||
__tablename__ = 'File' | ||
|
||
id = db.Column(db.String(100)) | ||
filename = db.Column(db.String(100), nullable=False, primary_key=True) | ||
filetype = db.Column(db.String(100), nullable=False) | ||
user_id = db.Column(db.String(100), db.ForeignKey('User.id', ondelete='CASCADE')) | ||
|
||
def save_to_db(self): | ||
self.id = str(uuid.uuid4()) | ||
db.session.add(self) | ||
try: | ||
db.session.commit() | ||
except Exception as e: | ||
db.session.rollback() | ||
db.session.flush() | ||
print(e) | ||
|
||
def __repr__(self): | ||
return '<File: {}>'.format(self.name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
class User(db.Model): | ||
__tablename__ = 'User' | ||
|
||
id = db.Column(db.String(100), primary_key=True) | ||
username = db.Column(db.String(80)) | ||
id = db.Column(db.String(100)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. user must be recognised by the username and not the id. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabru-md Agreed, but why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you are not reading the implementation of the code before creating comments. I expect this much explanation is sufficient. 😄 |
||
username = db.Column(db.String(80), primary_key=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one can add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both are different. |
||
password = db.Column(db.String(100)) | ||
email = db.Column(db.String(100)) | ||
files = db.relationship('File', backref='uploader') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
oauthToken | ||
) | ||
|
||
|
||
app = create_app() | ||
|
||
|
||
db.init_app(app) | ||
migrate = Migrate(app, db) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from marshmallow_jsonapi.flask import Schema | ||
from marshmallow_jsonapi import fields | ||
|
||
|
||
class FileSchema(Schema): | ||
class Meta: | ||
type_ = 'File' | ||
self_view = 'fileUploader.get_file' | ||
kwargs = {'id': '<id>'} | ||
|
||
id = fields.Str(required=True, dump_only=True) | ||
filename = fields.Str(required=True) | ||
filetype = fields.Str(required=True) | ||
user_id = fields.Relationship( | ||
self_url='/api/upload/get_file', | ||
self_url_kwargs={'file_id': '<id>'}, | ||
related_url='/user/register', | ||
related_url_kwargs={'id': '<id>'}, | ||
include_resource_linkage=True, | ||
type_='User' | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from marshmallow_jsonapi.flask import Schema | ||
from marshmallow_jsonapi import fields | ||
|
||
|
||
class UserSchema(Schema): | ||
class Meta: | ||
type_ = 'Users' | ||
self_view = 'loginUser.login' | ||
kwargs = {'id': '<id>'} | ||
|
||
id = fields.Str(required=True, dump_only=True) | ||
username = fields.Str(required=True) | ||
email = fields.Str(required=True) | ||
password = fields.Str(required=True, load_only=True) | ||
name = fields.Str(required=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabru-md This will return an error to the console, but what will the user see? Please make use
Response
class and return proper error to the user endpointThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will iterate over this PR.
So incase you are worried about the error stuff, this is just a basic implementation.
Since it works and @yashLadha said it is important, therefore I have given a basic setup so that the frontend can be linked.
If you have any other questions I would suggest waiting for a day or two, so that I can work with @ParthS007 and @yashLadha.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vaibhavsingh97 #848 can you refer to the comments there, cheers 👍