Skip to content
Merged
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
8 changes: 5 additions & 3 deletions App.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

# Internal
from crypto import pw_hash
from utils import ensure_config, get_user_role, get_role_color
from connectors.discord import DiscordClient
from connectors.rss import RSSUpdateType
from framework.menu import navigation_menu
from framework.roles import role_badge
from utils import ensure_config
from tasks import discord_tasks, backup_task
from db import User
from crypto import generate_signing_keys
Expand Down Expand Up @@ -171,10 +173,10 @@ def extensions_init() -> None:
app.config['webhook'] = webhook

# Add useful template globals
app.add_template_global(get_user_role)
app.add_template_global(get_role_color)
app.add_template_global(current_user, 'current_user')
app.add_template_global(RSSUpdateType)
app.add_template_global(navigation_menu)
app.add_template_global(role_badge)
app.add_template_global(APP_VERSION, 'APP_VERSION')

# Load all the blueprints
Expand Down
4 changes: 3 additions & 1 deletion blueprints/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from datetime import datetime

from db import Article, User, Frontpage, Correction
from framework.roles import role_badge

ApiController = Blueprint('ApiController', __name__)

#TODO: Extract constant
PAGE_ITEMS = 15

def result_ok(result = [], extra_data = {}):
Expand Down Expand Up @@ -95,6 +95,8 @@ def search_user():
'wikidot': u.user.wikidot,
'displayname': u.user.display_name,
'tr_count': u.translation_count,
'cr_count': u.correction_count,
'tr_role_html': role_badge(u.points),
'points': u.points} for u in user]
return result_ok(results)

Expand Down
6 changes: 3 additions & 3 deletions blueprints/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Internal
from forms import NewArticleForm, EditArticleForm, AssignCorrectionForm
from utils import get_user_role
from framework.roles import get_role
from extensions import rss, webhook
from db import User, Article

Expand All @@ -21,8 +21,8 @@ def notify_rolemaster(uid, point_amount):
error(f"How the fuck does this even happen? (Sending promote notify for a nonexistent user {uid})")
return
current_points = promoted_user.stats.first().points
current_role = get_user_role(current_points)
next_role = get_user_role(current_points + point_amount)
current_role = get_role(current_points)
next_role = get_role(current_points + point_amount)
if current_role != next_role:
if promoted_user.discord:
webhook.send_text(f'Uživatel {promoted_user.nickname} (<@{promoted_user.discord}>) dosáhl hranice pro roli {next_role}!')
Expand Down
82 changes: 1 addition & 81 deletions blueprints/autobackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from connectors.wikicomma import Status, Message, MessageType, ErrorKind, generate_config
from db import WikiCommaConfig, Wiki, Backup, User
from crypto import sign_file, get_fingerprint
from framework.api.schemas.backup_schema import status_message_schema, backup_config_schema

# External
import urllib.parse
Expand Down Expand Up @@ -42,87 +43,6 @@ class StatusMutex:

statuses: Dict[str, StatusMutex] = {}

backup_config_schema = {
'type': 'object',
'properties': {
'socks_proxy': {
'type': ['string', 'null']
},
'http_proxy': {
'type': ['string', 'null']
},
'blacklist': {
'type': ['string', 'null']
},
'wikis': {
'type': ['string', 'null']
},
'delay': {
'type': ['integer', 'null'],
'minimum': 0
},
'ratelimit_size': {
'type': ['integer', 'null'],
'minimum': 0
},
'ratelimit_refill': {
'type': ['integer', 'null'],
'minimum': 0
},
}
}

status_message_schema = {
'type': 'object',
'properties': {
'type': {
'type': 'integer',
'minimum': 0,
'maximum': 7
},
'tag': {
'type': 'string'
}
},
'required': ['type', 'tag'],
'oneOf': [
{ 'properties': {
'type': { 'enum': [0, 5, 6, 7] }
}},
{ 'properties': {
'type': {'const': 1},
'total': {'type': 'integer', 'minimum': 0}
}, 'required': ['total']},
{ 'properties': {
'type': {'enum': [3, 4]},
'errorKind': {'type': 'integer', 'minimum': 0, 'maximum': 14}
}, 'required': ['errorKind'],
'oneOf': [
{ 'properties': {
'errorKind': {'enum': [0, 1, 5, 6, 7, 9, 10, 11, 12, 14]}
}},
{ 'properties': {
'errorKind': {'enum': [2, 3, 4, 8, 13]},
'name': {'type': 'string'}
}, 'required': ['name']}
]},
{ 'properties': {
'type': {'const': 2},
'status': {'type': 'integer', 'minimum': 0, 'maximum': 8}
}, 'required': ['status'],
'oneOf': [
{ 'properties': {
'status': {'enum': [0, 2, 3, 4, 5]}
}},
{ 'properties': {
'status': {'const': 1},
'done': {'type': 'integer', 'minimum': 0},
'postponed': {'type': 'integer', 'minimum': 0}
}, 'required': ['done', 'postponed']}
]}
]
}

AutobackupController = Blueprint("AutobackupController", __name__)

# Tiny hack that allows us to call the backup_start route function in the automatically sheduled task, bypassing login
Expand Down
2 changes: 1 addition & 1 deletion blueprints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def add_user():

# Fetch nickname and profile in background
sched.add_job('Immediate nickname update', lambda: discord_tasks.update_nicknames_task(override_users=[user]))
sched.add_job('Immediate profile update', lambda: discord_tasks.download_avatars_task(override_ids=[form.discord.data]))
sched.add_job('Immediate profile update', lambda: discord_tasks.download_avatars_task(override_users=[user]))

if form.can_login.data:
session['tpw'] = temp_password
Expand Down
6 changes: 3 additions & 3 deletions connectors/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _validate_user_id(uid: str):
return True

@staticmethod
def _get_user(uid: int) -> t.Optional[dict]:
def get_user(uid: int) -> t.Optional[dict]:

retry = 0
while retry < RATELIMIT_RETRIES:
Expand Down Expand Up @@ -82,7 +82,7 @@ def get_global_username(uid: int) -> t.Optional[str]:
"""

try:
user = DiscordClient._get_user(uid)
user = DiscordClient.get_user(uid)
except DiscordException as e:
raise e
if user is None:
Expand All @@ -106,7 +106,7 @@ def get_avatar(uid: int, size = 256) -> bytes:
bytes: A bytes object containing the user's avatar in PNG format
"""
try:
user = DiscordClient._get_user(uid)
user = DiscordClient.get_user(uid)
except DiscordException as e:
raise e

Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
APP_VERSION = "1.1.2"
APP_VERSION = "2.0.1"
BRANCH_ID = "CS"
4 changes: 2 additions & 2 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

database = SqliteDatabase("data/scp.db")

# TODO: Extract constant
PAGE_ITEMS = 15

class BaseModel(Model):
class Meta:
database = database

class ViewModel(BaseModel):
def save():
def save(self):
raise RuntimeError("Attempted to insert into an SQL View")

class Meta:
Expand All @@ -27,6 +26,7 @@ class User(BaseModel, UserMixin):
password = BlobField(null=True)
temp_pw = BooleanField(default=True, null=True)
wikidot = TextField(unique=True)
avatar_hash = TextField(default=True, null=True)

@property
def can_login(self) -> bool:
Expand Down
Empty file added framework/__init__.py
Empty file.
Empty file.
80 changes: 80 additions & 0 deletions framework/api/schemas/backup_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
backup_config_schema = {
'type': 'object',
'properties': {
'socks_proxy': {
'type': ['string', 'null']
},
'http_proxy': {
'type': ['string', 'null']
},
'blacklist': {
'type': ['string', 'null']
},
'wikis': {
'type': ['string', 'null']
},
'delay': {
'type': ['integer', 'null'],
'minimum': 0
},
'ratelimit_size': {
'type': ['integer', 'null'],
'minimum': 0
},
'ratelimit_refill': {
'type': ['integer', 'null'],
'minimum': 0
},
}
}

status_message_schema = {
'type': 'object',
'properties': {
'type': {
'type': 'integer',
'minimum': 0,
'maximum': 7
},
'tag': {
'type': 'string'
}
},
'required': ['type', 'tag'],
'oneOf': [
{ 'properties': {
'type': { 'enum': [0, 5, 6, 7] }
}},
{ 'properties': {
'type': {'const': 1},
'total': {'type': 'integer', 'minimum': 0}
}, 'required': ['total']},
{ 'properties': {
'type': {'enum': [3, 4]},
'errorKind': {'type': 'integer', 'minimum': 0, 'maximum': 14}
}, 'required': ['errorKind'],
'oneOf': [
{ 'properties': {
'errorKind': {'enum': [0, 1, 5, 6, 7, 9, 10, 11, 12, 14]}
}},
{ 'properties': {
'errorKind': {'enum': [2, 3, 4, 8, 13]},
'name': {'type': 'string'}
}, 'required': ['name']}
]},
{ 'properties': {
'type': {'const': 2},
'status': {'type': 'integer', 'minimum': 0, 'maximum': 8}
}, 'required': ['status'],
'oneOf': [
{ 'properties': {
'status': {'enum': [0, 2, 3, 4, 5]}
}},
{ 'properties': {
'status': {'const': 1},
'done': {'type': 'integer', 'minimum': 0},
'postponed': {'type': 'integer', 'minimum': 0}
}, 'required': ['done', 'postponed']}
]}
]
}
41 changes: 41 additions & 0 deletions framework/config/menu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
menu:
- login:
text: "Přihlásit se"
icon: "bi-box-arrow-in-right"
auth: "unauthorized_only"
handler: "UserAuth.login"
- logout:
text: "Odhlásit se"
icon: "bi-escape"
auth: "authorized"
handler: "UserAuth.logout"
- leaderboard:
text: "Žebříček překladatelů"
icon: "bi-trophy"
auth: "unauthorized"
handler: "index"
- leaderboard:
text: "Statistiky"
icon: "bi-bar-chart"
auth: "unauthorized"
handler: "StatisticsController.view_stats"
- leaderboard:
text: "Nové stránky"
icon: "bi-rss"
auth: "authorized"
handler: "RssPageController.rss_changes"
- backups:
text: "Zálohy"
icon: "bi-database"
auth: "authorized"
handler: "AutobackupController.backup_index"
- leaderboard:
text: "Přidat uživatele"
icon: "bi-person-add"
auth: "authorized"
handler: "UserController.add_user"
- debug:
text: "Nástroje pro techniky"
icon: "bi-braces"
auth: "authorized"
handler: "DebugTools.debug_index"
37 changes: 37 additions & 0 deletions framework/config/roles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
translator:
min_points: 5
no_role:
name: "Žádná"
point_limit: 0
badge_css: ""
roles:
- translator1:
name: "Překladatel I"
point_limit: 5
badge_css: "bg-gradient-to-r from-gray-400 to-gray-600"
- translator2:
name: "Překladatel II"
point_limit: 10
badge_css: "bg-gradient-to-r from-sky-400 to-blue-500"
- translator3:
name: "Překladatel III"
point_limit: 25
badge_css: "bg-gradient-to-r from-fuchsia-600 to-pink-600"
- translator4:
name: "Překladatel IV"
point_limit: 50
badge_css: "bg-gradient-to-r from-pink-500 via-red-500 to-yellow-500"
- translator5:
name: "Překladatel V"
point_limit: 100
badge_css: "bg-gradient-to-r from-gray-700 via-gray-900 to-black"
- translator6:
name: "Překladatel VI"
point_limit: 200
badge_css: "bg-gradient-to-r from-slate-900 via-purple-900 to-slate-900"
- translator7:
name: "Překladatel VII"
point_limit: 500
badge_css: "gradient-background"


Loading