Clean up and refactor

This commit is contained in:
Ramon Wenger 2023-08-30 11:12:09 +02:00
parent 4011c016dc
commit 8dc080a9fc
3 changed files with 68 additions and 49 deletions

View File

@ -8,6 +8,14 @@ from core.logger import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
class AlreadyTranslatedException(Exception):
pass
class ModuleDoesNotExistException(Exception):
pass
# todo: make this a django command # todo: make this a django command
# todo: add language to translate to as an argument # todo: add language to translate to as an argument
# todo: write a test maybe # todo: write a test maybe
@ -22,36 +30,36 @@ def convert_page_to_translation(slug: str, original_slug: str, language_code="en
try: try:
module = Module.objects.get(slug=slug) module = Module.objects.get(slug=slug)
original = Module.objects.get(slug=original_slug) original = Module.objects.get(slug=original_slug)
if original.translation_key == module.translation_key:
raise AlreadyTranslatedException
except Module.DoesNotExist: except Module.DoesNotExist:
logger.warning("Module does not exist") raise ModuleDoesNotExistException
return locale, _ = Locale.objects.get_or_create(language_code=language_code)
en = Locale.objects.get(language_code=language_code) logger.debug("Starting conversion")
logger.debug("starting this")
topic: Topic = module.get_parent() topic: Topic = module.get_parent()
try: try:
logger.debug("getting translation") logger.debug("Getting existing topic translation")
en_topic = topic.get_translation(en) translated_topic = topic.get_translation(locale)
except Page.DoesNotExist: except Page.DoesNotExist:
logger.debug("creating translation") logger.debug("Creating topic translation")
en_topic = topic.copy_for_translation(en) translated_topic = topic.copy_for_translation(locale)
revision = en_topic.latest_revision revision = translated_topic.latest_revision
en_topic.publish(revision) translated_topic.publish(revision)
logger.debug(f"new topic {en_topic.pk}") module.locale = locale
module.locale = en
if original is not None: if original is not None:
translation_key = original.translation_key translation_key = original.translation_key
module.translation_key = translation_key module.translation_key = translation_key
module.save() module.save()
chapters = module.get_children() chapters = module.get_children()
for chapter in chapters: for chapter in chapters:
chapter.locale = en chapter.locale = locale
chapter.save() chapter.save()
content_blocks = chapter.get_children() content_blocks = chapter.get_children()
for content_block in content_blocks: for content_block in content_blocks:
content_block.locale = en content_block.locale = locale
content_block.save() content_block.save()
module.move(en_topic, "last-child") module.move(translated_topic, "last-child")
print(f"go {module.title} {module.locale.language_code}") return f"Converted page {slug} to be the {language_code} translation of {original_slug}"

View File

@ -1,12 +1,16 @@
import csv import csv
import os import os
from django.core import management from django.core import management
from django.core.management import BaseCommand from django.core.management import BaseCommand, CommandError
from basicknowledge.models import BasicKnowledge from basicknowledge.models import BasicKnowledge
from datetime import date from datetime import date
from core.convert_page_translation import convert_page_to_translation from core.convert_page_translation import (
AlreadyTranslatedException,
ModuleDoesNotExistException,
convert_page_to_translation,
)
EN = "en" EN = "en"
FR = "fr" FR = "fr"
@ -26,18 +30,12 @@ class Command(BaseCommand):
slug = options.get("slug") slug = options.get("slug")
original_slug = options.get("original") original_slug = options.get("original")
language_code = options.get("language") language_code = options.get("language")
self.stdout.write(f"args {slug} {original_slug} {language_code}") try:
convert_page_to_translation( result = convert_page_to_translation(
slug=slug, original_slug=original_slug, language_code=language_code slug=slug, original_slug=original_slug, language_code=language_code
) )
# with open(filename) as f: self.stdout.write(result)
# reader = csv.reader(f) except ModuleDoesNotExistException:
# for name, slugs in reader: raise CommandError("Module does not exist")
# self.stdout.write(name) except AlreadyTranslatedException:
# self.stdout.write(slugs) raise CommandError("Pages already are translations of each other")
# cwd = os.getcwd()
# self.stdout.write(cwd)
# with open(f"{cwd}/exports/{name}.html", "w") as dest:
# management.call_command(
# "export_instruments", slugs=slugs, stdout=dest
# )

View File

@ -8,6 +8,7 @@ from users.models import SchoolClass
logger = get_logger(__name__) logger = get_logger(__name__)
def set_hidden_for(block, visibility_list): def set_hidden_for(block, visibility_list):
for v in visibility_list: for v in visibility_list:
school_class = get_object(SchoolClass, v.school_class_id) school_class = get_object(SchoolClass, v.school_class_id)
@ -31,51 +32,63 @@ def is_private_api_call_allowed(user, body):
# logged in users without valid license have only access to logout, me & coupon mutations # logged in users without valid license have only access to logout, me & coupon mutations
if user.is_anonymous: if user.is_anonymous:
logger.debug('User is anonymous') logger.debug("User is anonymous")
return False return False
if user.is_superuser: if user.is_superuser:
logger.debug('User is superuser') logger.debug("User is superuser")
return True return True
body_unicode = body.decode('utf-8') body_unicode = body.decode("utf-8")
if is_endpoint_allowed(body_unicode): if is_endpoint_allowed(body_unicode):
logger.debug('Endpoint allowed') logger.debug("Endpoint allowed")
return True return True
license_expiry = user.license_expiry_date license_expiry = user.license_expiry_date
# all other resources are denied if the license is not valid # all other resources are denied if the license is not valid
if license_expiry is None: if license_expiry is None:
logger.debug('license expiry is None') logger.debug("license expiry is None")
return False return False
logger.debug('private api call is allowed') # logger.debug('private api call is allowed')
return True return True
# logout, betalogin, me and coupon resources are always allowed. Even if the user has no valid license # logout, betalogin, me and coupon resources are always allowed. Even if the user has no valid license
def is_endpoint_allowed(body): def is_endpoint_allowed(body):
return re.search(r"mutation\s*.*\s*logout\s*{", body) or re.search(r"query\s*.*\s*me\s*{", body) \ return (
or re.search(r"mutation\s*Coupon", body) or re.search(r"mutation\s*BetaLogin", body) re.search(r"mutation\s*.*\s*logout\s*{", body)
or re.search(r"query\s*.*\s*me\s*{", body)
or re.search(r"mutation\s*Coupon", body)
or re.search(r"mutation\s*BetaLogin", body)
)
def sync_hidden_for(model, school_class_template, school_class_to_sync): def sync_hidden_for(model, school_class_template, school_class_to_sync):
if model.hidden_for.filter(id=school_class_template.id).exists() and not model.hidden_for.filter( if (
id=school_class_to_sync.id).exists(): model.hidden_for.filter(id=school_class_template.id).exists()
and not model.hidden_for.filter(id=school_class_to_sync.id).exists()
):
model.hidden_for.add(school_class_to_sync) model.hidden_for.add(school_class_to_sync)
if model.hidden_for.filter(id=school_class_to_sync.id).exists() and not model.hidden_for.filter( if (
id=school_class_template.id).exists(): model.hidden_for.filter(id=school_class_to_sync.id).exists()
and not model.hidden_for.filter(id=school_class_template.id).exists()
):
model.hidden_for.remove(school_class_to_sync) model.hidden_for.remove(school_class_to_sync)
def sync_visible_for(model, school_class_template, school_class_to_sync): def sync_visible_for(model, school_class_template, school_class_to_sync):
if model.visible_for.filter(id=school_class_template.id).exists() and not model.visible_for.filter( if (
id=school_class_to_sync.id).exists(): model.visible_for.filter(id=school_class_template.id).exists()
and not model.visible_for.filter(id=school_class_to_sync.id).exists()
):
model.visible_for.add(school_class_to_sync) model.visible_for.add(school_class_to_sync)
if model.visible_for.filter(id=school_class_template.id).exists() and not model.visible_for.filter( if (
id=school_class_to_sync.id).exists(): model.visible_for.filter(id=school_class_template.id).exists()
and not model.visible_for.filter(id=school_class_to_sync.id).exists()
):
model.visible_for.add(school_class_to_sync) model.visible_for.add(school_class_to_sync)