user model upgrade, rating, notificaiton

This commit is contained in:
Untone 2021-08-19 13:02:28 +03:00
parent 5bb4553360
commit 0cb0b85bce
6 changed files with 82 additions and 17 deletions

17
orm/notification.py Normal file
View File

@ -0,0 +1,17 @@
from sqlalchemy import Column, Integer, String, ForeignKey, JSON as JSONType
from orm.base import Base
class Notification(Base):
__tablename__ = 'notification'
kind: str = Column(String, primary_key = True)
template: str = Column(String, nullable = False)
variables: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]
class UserNotification(Base):
__tablename__ = 'user_notification'
id: int = Column(Integer, primary_key = True)
user: int = Column(ForeignKey("user.id"))
kind: int = Column(ForeignKey("notification.kind"), nullable = False)
values: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]

10
orm/rating.py Normal file
View File

@ -0,0 +1,10 @@
from sqlalchemy import Column, Integer, String, ForeignKey
from orm.base import Base
class Rating(Base):
__tablename__ = 'rating'
id: int = Column(Integer, primary_key = True)
createdBy: int = Column(ForeignKey("user.id"), primary_key = True)
value: int = Column(Integer, nullable=False)

View File

@ -21,5 +21,6 @@ class Shout(Base):
versionOf: str = Column(ForeignKey("shout.slug"), nullable=True) versionOf: str = Column(ForeignKey("shout.slug"), nullable=True)
tags: str = Column(String, nullable=True) tags: str = Column(String, nullable=True)
topics: str = Column(String, nullable=True) topics: str = Column(String, nullable=True)
views: int = Column(Integer, default=0)
# TODO: add all the fields # TODO: add all the fields

View File

@ -1,28 +1,39 @@
from typing import List from typing import List
from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, DateTime
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from orm import Permission from orm import Permission
from orm.base import Base, local_session from orm.base import Base, local_session
from orm.rating import Rating
from orm.notification import UserNotification
class UserRole(Base): class UserRole(Base):
__tablename__ = 'user_role' __tablename__ = 'user_role'
id = None id: int = Column(Integer, primary_key = True)
user_id: int = Column(ForeignKey("user.id"), primary_key = True) user_id: int = Column(ForeignKey("user.id"), primary_key = True)
role_id: int = Column(ForeignKey("role.id"), primary_key = True) role: str = Column(ForeignKey("role.name"), primary_key = True)
class User(Base): class User(Base):
__tablename__ = 'user' __tablename__ = 'user'
email: str = Column(String, unique=True, nullable=False) email: str = Column(String, unique=True, nullable=False, comment="Email")
username: str = Column(String, nullable=False, comment="Name") username: str = Column(String, nullable=False, comment="Login")
password: str = Column(String, nullable=True, comment="Password") password: str = Column(String, nullable=True, comment="Password")
bio: str = Column(String, nullable=True, comment="Bio")
oauth_id: str = Column(String, nullable=True) userpic: str = Column(String, nullable=True, comment="Userpic")
viewname: str = Column(String, nullable=True, comment="Display name")
rating: int = Column(Integer, nullable=True, comment="Rating")
slug: str = Column(String, unique=True, nullable=True, comment="Slug")
muted: bool = Column(Boolean, default=False)
emailConfirmed: bool = Column(Boolean, default=False)
createdAt: DateTime = Column(DateTime, nullable=False, comment="Created at")
wasOnlineAt: DateTime = Column(DateTime, nullable=False, comment="Was online at")
links: JSON = Column(JSON, nullable=True, comment="Links")
oauth: str = Column(String, nullable=True)
notifications = relationship("Notification", secondary=UserNotification.__table__)
ratings = relationship("Rating", secondary=Rating.__table__)
roles = relationship("Role", secondary=UserRole.__table__) roles = relationship("Role", secondary=UserRole.__table__)
@classmethod @classmethod

View File

@ -55,13 +55,14 @@ type Mutation {
deleteMessage(messageId: Int!): Result! deleteMessage(messageId: Int!): Result!
# auth # auth
# resetPassword(password: String!, token: String!): Token!
confirmEmail(token: String!): AuthResult! confirmEmail(token: String!): AuthResult!
requestPasswordReset(email: String!): Boolean!
confirmPasswordReset(token: String!): Boolean!
registerUser(email: String!, password: String!): AuthResult!
# updatePassword(password: String!, token: String!): Token!
# invalidateAllTokens: Boolean! # invalidateAllTokens: Boolean!
# invalidateTokenById(id: Int!): Boolean! # invalidateTokenById(id: Int!): Boolean!
# requestEmailConfirmation: User! # requestEmailConfirmation: User!
# requestPasswordReset(email: String!): Boolean!
registerUser(email: String!, password: String!): AuthResult!
# shout # shout
createShout(input: ShoutInput!): ShoutResult! createShout(input: ShoutInput!): ShoutResult!
@ -123,21 +124,44 @@ type Role {
desc: String desc: String
} }
type Rating {
createdBy: String!
value: Int!
}
type Notification {
kind: String! # unique primary key
template: String!
variables: [String]
}
type UserNotification {
id: Int! # primary key
user: Int!
kind: String! # NotificationTemplate.name
values: [String]
}
type User { type User {
username: String! # email username: String! # email
createdAt: DateTime!
email: String email: String
password: String password: String
oauth: String # provider:token
viewname: String # to display viewname: String # to display
userpic: String userpic: String
links: [String] links: [String]
emailConfirmed: Boolean emailConfirmed: Boolean # should contain all emails too # TODO: pagination here
id: Int! id: Int!
muted: Boolean muted: Boolean
rating: Int rating: Int
roles: [Role!]! roles: [Role]
createdAt: DateTime! updatedAt: DateTime
updatedAt: DateTime!
wasOnlineAt: DateTime wasOnlineAt: DateTime
ratings: [Rating]
slug: String
bio: String
notifications: [Int]
} }
type Message { type Message {
@ -161,6 +185,7 @@ type Shout {
deletedAt: DateTime deletedAt: DateTime
deletedBy: Int deletedBy: Int
rating: Int rating: Int
ratigns: [Rating]
published: DateTime # if there is no published field - it is not published published: DateTime # if there is no published field - it is not published
replyTo: String # another shout replyTo: String # another shout
tags: [String] # actual values tags: [String] # actual values
@ -169,6 +194,7 @@ type Shout {
versionOf: String versionOf: String
visibleForRoles: [String] # role ids are strings visibleForRoles: [String] # role ids are strings
visibleForUsers: [Int] visibleForUsers: [Int]
views: Int
} }
type Topic { type Topic {

View File

@ -3,7 +3,7 @@ from os import environ
PORT = 8080 PORT = 8080
DB_URL = environ.get("DB_URL") or "sqlite:///database.sqlite3" DB_URL = environ.get("DB_URL") or "sqlite:///db.sqlite3"
JWT_ALGORITHM = "HS256" JWT_ALGORITHM = "HS256"
JWT_SECRET_KEY = "8f1bd7696ffb482d8486dfbc6e7d16dd-secret-key" JWT_SECRET_KEY = "8f1bd7696ffb482d8486dfbc6e7d16dd-secret-key"
JWT_LIFE_SPAN = 24 * 60 * 60 # seconds JWT_LIFE_SPAN = 24 * 60 * 60 # seconds