Skip to content
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

Python lint #133

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 111 additions & 105 deletions python/mdvtools/dbutils/dbmodels.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
from flask_sqlalchemy import SQLAlchemy

Check failure on line 1 in python/mdvtools/dbutils/dbmodels.py

View workflow job for this annotation

GitHub Actions / pyright (3.12, 1.1.343)

Import "flask_sqlalchemy" could not be resolved (reportMissingImports)
from datetime import datetime
from sqlalchemy import String, Integer, DateTime, Boolean, Text, JSON, ForeignKey

Check failure on line 3 in python/mdvtools/dbutils/dbmodels.py

View workflow job for this annotation

GitHub Actions / pyright (3.12, 1.1.343)

Import "sqlalchemy" could not be resolved (reportMissingImports)
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, relationship

Check failure on line 4 in python/mdvtools/dbutils/dbmodels.py

View workflow job for this annotation

GitHub Actions / pyright (3.12, 1.1.343)

Import "sqlalchemy.orm" could not be resolved (reportMissingImports)
from typing import Any
# https://flask-sqlalchemy.readthedocs.io/en/stable/models/
class Base(DeclarativeBase):
pass

db = SQLAlchemy()
db = SQLAlchemy(model_class=Base)

class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(255), unique=True, nullable=False, default='')
confirmed_at = db.Column(db.DateTime, nullable=True)
password = db.Column(db.String(255), nullable=False, default='')
is_active = db.Column(db.Boolean, nullable=False, default=False)
first_name = db.Column(db.String(50), nullable=False, default='')
last_name = db.Column(db.String(50), nullable=False, default='')
administrator = db.Column(db.Boolean, nullable=False, default=False)
institution = db.Column(db.Text, nullable=True)
projects = db.relationship('UserProject', backref='user', lazy=True)
jobs = db.relationship('Job', backref='user', lazy=True)
permissions = db.relationship('Permission', backref='user', lazy=True)
preferences = db.relationship('UserPreference', backref='user', lazy=True)
#shared_objects = db.relationship('SharedObject', foreign_keys='SharedObject.shared_with', backref='shared_with_user', lazy=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, default='')
confirmed_at: Mapped[datetime] = mapped_column(DateTime, nullable=True)
password: Mapped[str] = mapped_column(String(255), nullable=False, default='')
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
first_name: Mapped[str] = mapped_column(String(50), nullable=False, default='')
last_name: Mapped[str] = mapped_column(String(50), nullable=False, default='')
administrator: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
institution: Mapped[str] = mapped_column(Text, nullable=True)
projects = relationship('UserProject', backref='user', lazy=True)
jobs = relationship('Job', backref='user', lazy=True)
permissions = relationship('Permission', backref='user', lazy=True)
preferences = relationship('UserPreference', backref='user', lazy=True)
#shared_objects = relationship('SharedObject', foreign_keys='SharedObject.shared_with', backref='shared_with_user', lazy=True)

class Project(db.Model):
__tablename__ = 'projects'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), nullable=False, default='unnamed_project')
path = db.Column(db.String(1024), nullable=False, unique=True)
created_timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
is_deleted = db.Column(db.Boolean, nullable=False, default=False)
deleted_timestamp = db.Column(db.DateTime, nullable=True)
update_timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
accessed_timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
owner = db.Column(db.Integer)
type = db.Column(db.Text)
data = db.Column(db.JSON)
is_public = db.Column(db.Boolean, nullable=False, default=False)
date_made_public = db.Column(db.DateTime)
status = db.Column(db.Text)
genome = db.Column(db.String, db.ForeignKey('genomes.name'))
parent = db.Column(db.Integer)
description = db.Column(db.Text)
access_level = db.Column(db.String(50), nullable=False, default='editable') # Default access level
users = db.relationship('UserProject', backref='project', lazy=True)
files = db.relationship('File', backref='project', lazy=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(255), nullable=False, default='unnamed_project')
path: Mapped[str] = mapped_column(String(1024), nullable=False, unique=True)
created_timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
deleted_timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=True)
update_timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
accessed_timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
owner: Mapped[int] = mapped_column(Integer)
type: Mapped[str] = mapped_column(Text)
data: Mapped[Any] = mapped_column(JSON)
is_public: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
date_made_public: Mapped[datetime] = mapped_column(DateTime)
status: Mapped[str] = mapped_column(Text)
genome: Mapped[str] = mapped_column(String, ForeignKey('genomes.name'))
parent: Mapped[str] = mapped_column(Integer)
description: Mapped[str] = mapped_column(Text)
access_level: Mapped[str] = mapped_column(String(50), nullable=False, default='editable') # Default access level
users = relationship('UserProject', backref='project', lazy=True)
files = relationship('File', backref='project', lazy=True)
Comment on lines +31 to +50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Project model typed columns.

Be mindful of line 46:

parent: Mapped[str] = mapped_column(Integer)

The type annotation (str) conflicts with the column type (Integer). This may cause runtime confusion.

-    parent: Mapped[str] = mapped_column(Integer)
+    parent: Mapped[int] = mapped_column(Integer)


__table_args__ = (
db.Index('idx_projects_genome', 'genome'),
Expand All @@ -51,100 +57,100 @@

class File(db.Model):
__tablename__ = 'files'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), nullable=False)
file_path = db.Column(db.String(255), nullable=False, unique=True)
upload_timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
update_timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), nullable=False)
#project = db.relationship('Project', backref=db.backref('files', lazy=True))
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
file_path: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
upload_timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
update_timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
project_id: Mapped[int] = mapped_column(Integer, ForeignKey('projects.id'), nullable=False)
#project = relationship('Project', backref=db.backref('files', lazy=True))

class UserProject(db.Model):
__tablename__ = 'user_projects'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), nullable=False)
can_read = db.Column(db.Boolean, nullable=False, default=False)
can_write = db.Column(db.Boolean, nullable=False, default=False)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey('users.id'), nullable=False)
project_id: Mapped[int] = mapped_column(Integer, ForeignKey('projects.id'), nullable=False)
can_read: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
can_write: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)

class Genome(db.Model):
__tablename__ = 'genomes'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50), nullable=False, unique=True)
label = db.Column(db.Text)
data = db.Column(db.JSON)
database = db.Column(db.Text)
date_added = db.Column(db.DateTime, nullable=False, default=datetime.now)
connections = db.Column(db.Integer)
icon = db.Column(db.Text)
is_public = db.Column(db.Boolean, nullable=False, default=True)
chrom_sizes = db.Column(db.JSON)
small_icon = db.Column(db.Text)
projects = db.relationship('Project', backref='genomes', lazy=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
label: Mapped[str] = mapped_column(Text)
data: Mapped[Any] = mapped_column(JSON)
database: Mapped[str] = mapped_column(Text)
date_added: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
connections: Mapped[int] = mapped_column(Integer)
icon: Mapped[str] = mapped_column(Text)
is_public: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
chrom_sizes: Mapped[Any] = mapped_column(JSON)
small_icon: Mapped[str] = mapped_column(Text)
projects = relationship('Project', backref='genomes', lazy=True)

class Job(db.Model):
__tablename__ = 'jobs'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
inputs = db.Column(db.JSON)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
outputs = db.Column(db.JSON)
sent_on = db.Column(db.DateTime, nullable=False, default=datetime.now)
status = db.Column(db.String(200))
class_name = db.Column(db.String(200))
genome = db.Column(db.String(100))
finished_on = db.Column(db.DateTime)
is_deleted = db.Column(db.Boolean, nullable=False, default=False)
type = db.Column(db.String(200))
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
inputs: Mapped[Any] = mapped_column(JSON)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey('users.id'), nullable=True)
outputs: Mapped[Any] = mapped_column(JSON)
sent_on: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
status: Mapped[str] = mapped_column(String(200))
class_name: Mapped[str] = mapped_column(String(200))
genome: Mapped[str] = mapped_column(String(100))
finished_on: Mapped[datetime] = mapped_column(DateTime)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
type: Mapped[str] = mapped_column(String(200))

class Permission(db.Model):
__tablename__ = 'permissions'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
permission = db.Column(db.String(200), nullable=False)
value = db.Column(db.String(200), nullable=False)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey('users.id'), nullable=True)
permission: Mapped[str] = mapped_column(String(200), nullable=False)
value: Mapped[str] = mapped_column(String(200), nullable=False)

class SharedObject(db.Model):
__tablename__ = 'shared_objects'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
owner = db.Column(db.Integer)
shared_with = db.Column(db.Integer)
object_id = db.Column(db.Integer)
date_shared = db.Column(db.DateTime, nullable=False, default=datetime.now)
level = db.Column(db.Text, nullable=False, default='view')
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
owner: Mapped[int] = mapped_column(Integer)
shared_with: Mapped[int] = mapped_column(Integer)
object_id: Mapped[int] = mapped_column(Integer)
date_shared: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
level: Mapped[str] = mapped_column(Text, nullable=False, default='view')

class UserPreference(db.Model):
__tablename__ = 'user_preferences'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
preference = db.Column(db.Text, nullable=False)
data = db.Column(db.JSON)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
preference: Mapped[str] = mapped_column(Text, nullable=False)
data: Mapped[Any] = mapped_column(JSON)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey('users.id'), nullable=True)

class ViewSet(db.Model):
__tablename__ = 'view_sets'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
table_name = db.Column(db.String(100))
name = db.Column(db.String(200))
description = db.Column(db.Text)
date_added = db.Column(db.DateTime, nullable=False, default=datetime.now)
date_modified = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
owner = db.Column(db.Integer, default=0)
is_public = db.Column(db.Boolean, default=False)
fields = db.Column(db.JSON)
data = db.Column(db.JSON)
date_made_public = db.Column(db.DateTime)
status = db.Column(db.Text)
is_deleted = db.Column(db.Boolean, default=False)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
table_name: Mapped[str] = mapped_column(String(100))
name: Mapped[str] = mapped_column(String(200))
description: Mapped[str] = mapped_column(Text)
date_added: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
date_modified: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
owner: Mapped[int] = mapped_column(Integer, default=0)
is_public: Mapped[bool] = mapped_column(Boolean, default=False)
fields: Mapped[Any] = mapped_column(JSON)
data: Mapped[Any] = mapped_column(JSON)
date_made_public: Mapped[datetime] = mapped_column(DateTime)
status: Mapped[str] = mapped_column(Text)
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)

class GeneSet(db.Model):
__tablename__ = 'gene_sets'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(100), nullable=False)
table_name = db.Column(db.String(150))
data = db.Column(db.JSON)
date_added = db.Column(db.DateTime, nullable=False, default=datetime.now)
date_modified = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
is_deleted = db.Column(db.Boolean, default=False)
description = db.Column(db.Text)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(100), nullable=False)
table_name: Mapped[str] = mapped_column(String(150))
data: Mapped[Any] = mapped_column(JSON)
date_added: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now)
date_modified: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
description: Mapped[str] = mapped_column(Text)

# Indexes
db.Index('idx_genes_name', GeneSet.name)
Expand Down
22 changes: 12 additions & 10 deletions python/mdvtools/dbutils/dbservice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# project_service.py
# dbservice.py

from mdvtools.dbutils.dbmodels import db, Project, File
from datetime import datetime
Expand Down Expand Up @@ -34,9 +34,12 @@ def get_next_project_id():

@staticmethod
# Routes -> /create_project
def add_new_project(path, name='unnamed_project'):
def add_new_project(path, name='unnamed project'):
try:
new_project = Project(name=name, path=path)
# can't see simple way to make type check happy with kwargs here
new_project = Project()
new_project.name = name
new_project.path = path
db.session.add(new_project)
db.session.commit()
return new_project
Expand Down Expand Up @@ -169,13 +172,12 @@ def add_or_update_file_in_project(file_name, file_path, project_id):
print(f"Updated file name in DB: {existing_file}")
else:
# Add new file to the database
new_file = File(
name=file_name,
file_path=file_path,
project_id=project_id,
upload_timestamp=datetime.now(),
update_timestamp=datetime.now()
)
new_file = File()
new_file.name = file_name
new_file.file_path = file_path
new_file.project_id = project_id
new_file.upload_timestamp = datetime.now()
new_file.update_timestamp = datetime.now()
db.session.add(new_file)
print(f"Added new file to DB: {new_file}")

Expand Down
Loading
Loading