This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
feat: instatiate db using sqlalch #238
Open
internnos
wants to merge
30
commits into
onlydustxyz:main
Choose a base branch
from
internnos:feat/backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
361cd6c
feat: instatiate db using sqlalch
internnos 968e6ee
refactor: use env variable
internnos 2ab02ae
feat: remove unecessary file
internnos d468859
feat(routes): change user model
internnos b682657
feat(user): add github field
internnos 7da1016
feat: verify signature
internnos ed4d5ff
feat(register_user): convert signature string to tuple
internnos 2bc2261
feat: github is not mandatory
internnos ab9d079
feat: remove unused code
internnos 1143eb7
fix: existing user error handling
internnos 6687ca6
fix: abi path
internnos 7c770e6
feat(db): move
internnos b8a08ed
feat: add package
internnos 1f0805a
fix: comment out broken import due to cairo v0.10
internnos c22801a
docs: add env variables
internnos f47bc6d
fix(fetchUserInfo): fix user query
internnos 05a5b34
fix(registerUser): verify signature
internnos 3aa42aa
fix: signature datatype
internnos 89fba89
fix(StarklingsUser): change wallet address length
internnos 4a7ea17
fix(StarklingsUser): signature datatype
internnos 2abe463
feat: add abi
internnos b4645ea
fix: instantiate db
internnos 4e119b9
fix: signature datatype
internnos 672bb3e
fix: signature datatype
internnos 77af810
fix: signature datatype
internnos 304d100
feat(updateUser): add
internnos c7d8ecc
fix(ValidatedExercise): validated_exercise_id length
internnos 247448b
fix(ValidatedExercise): wallet_address length
internnos f0bb2e2
fix(ValidatedExercise): validated_exercise_id length
internnos 4b4f71d
feat(updateUser): verify signature
internnos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
import pymysql | ||
from sqlalchemy.orm import declarative_base, sessionmaker | ||
from models import StarklingsUser,Path,Exercise,ValidatedExercise, Base | ||
from sqlalchemy import create_engine | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
host=os.environ.get('DATABASE_HOST', '') | ||
database=os.environ.get('DATABASE_NAME', '') | ||
user=os.environ.get('DATABASE_USER', '') | ||
password=os.environ.get('DATABASE_PWD', '') | ||
|
||
engine = create_engine(f'mysql+pymysql://{user}:{password}@{host}/{database}', echo=True) | ||
|
||
Session = sessionmaker(bind=engine) | ||
Base.metadata.create_all(engine) |
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,42 @@ | ||
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey | ||
from sqlalchemy.orm import sessionmaker, relationship, declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
class StarklingsUser(Base): | ||
__tablename__ = "starklings_user" | ||
wallet_address = Column(String(42), primary_key=True) | ||
signature = Column(String(255), nullable=False) | ||
github = Column(String(255), nullable=True) | ||
username = Column(String(255), nullable=False) | ||
score = Column(Integer, nullable=False, default=0) | ||
starklings_user = relationship("ValidatedExercise") | ||
|
||
|
||
class Path(Base): | ||
__tablename__ = "path" | ||
path_name = Column(String(255), primary_key=True) | ||
num_exercises = Column(Integer, nullable=False) | ||
path = relationship("Exercise") | ||
|
||
|
||
class Exercise(Base): | ||
__tablename__ = "exercise" | ||
exercise_name = Column(String(255), primary_key=True) | ||
score = Column(Integer, nullable=False, default=0) | ||
path_name = Column(String(255), ForeignKey("path.path_name"), nullable=False) | ||
exercise = relationship("ValidatedExercise") | ||
|
||
|
||
class ValidatedExercise(Base): | ||
__tablename__ = "validated_exercise" | ||
validated_exercise_id = Column(String(64), primary_key=True) | ||
exercise_name = Column( | ||
String(255), | ||
ForeignKey("exercise.exercise_name"), | ||
nullable=False, | ||
) | ||
wallet_address = Column( | ||
String(42), ForeignKey("starklings_user.wallet_address"), nullable=False | ||
) | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -3,15 +3,19 @@ | |
import bcrypt | ||
from sqlalchemy.exc import IntegrityError | ||
from flask_sqlalchemy import SQLAlchemy | ||
from starklings_backend.utils import verify_email | ||
from starklings_backend.models.shared import db | ||
from starklings_backend.models.user import Starklingsuser | ||
from starklings_backend.utils import verify_email, VerifySignature | ||
from starklings_backend.models import StarklingsUser, Path, Exercise, ValidatedExercise, Base | ||
from starklings_backend.exercise import verify_exercise | ||
from checker import ExerciceFailed | ||
import tempfile | ||
from starklings_backend.db import Session | ||
|
||
|
||
|
||
app_routes = Blueprint('app_routes', __name__) | ||
|
||
db = Session() | ||
|
||
@app_routes.route('/', methods=['GET']) | ||
def landing(): | ||
return 'Starklings API' | ||
|
@@ -27,18 +31,27 @@ def register_user(): | |
""" | ||
try: | ||
signature = request.json.get('signature', None) | ||
# convert string to tuple | ||
signature = eval(signature) | ||
wallet_address = request.json.get('wallet_address', None) | ||
username = request.json.get('username', wallet_address) | ||
github = request.json.get('github', None) | ||
message_hash = request.json.get('message_hash', '') | ||
network = request.json.get('network', 'testnet') | ||
if None in [wallet_address, signature]: | ||
return "Wrong form", 400 | ||
#@TODO: Check Signature validity | ||
|
||
user = Starklingsuser(wallet_address=wallet_address, signature=signature, username=username) | ||
db.session.commit() | ||
return f'Welcome! {username}', 200 | ||
# verify signature | ||
verify_signature = VerifySignature(abi, network, wallet_address) | ||
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. Where is 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. resolved! |
||
is_valid, error = verify_signature.verify_signature(message_hash, signature) | ||
if error is None: | ||
user = StarklingsUser(wallet_address=wallet_address, signature=signature, github=github, username=username) | ||
db.add(user) | ||
db.commit() | ||
return f'Welcome! {username}', 200 | ||
return 'Signature invalid', 400 | ||
|
||
except IntegrityError as e: | ||
db.session.rollback() | ||
session.rollback() | ||
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. Where is session defined 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 have fixed it! |
||
return 'User Already Exists', 400 | ||
except AttributeError: | ||
return 'Provide an Email and Password in JSON format in the request body', 400 | ||
|
@@ -54,7 +67,7 @@ def fetch_user_info(): | |
wallet_address = request.json.get('wallet_address', None) | ||
if not wallet_address: | ||
return 'Missing address', 400 | ||
user = Starklingsuser.query.filter_by(wallet_address=wallet_address).first() | ||
user = StarklingsUser.query.filter_by(wallet_address=wallet_address).first() | ||
if not user: | ||
return 'User Not Found!', 404 | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rename it to validated_exercises
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.
according to the docs, the arg to
relationship
is theClass
name cmiiwThere 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.
if i change to table name, i'll get this error instead