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

Testový import vrací HTTP500 příliš dlouhá jména oponentů/vedoucích #297

Open
thejoeejoee opened this issue Oct 11, 2024 · 0 comments
Labels
bug Something isn't working python Pull requests that update Python code

Comments

@thejoeejoee
Copy link
Member

pokud se při importu testové práce vloží "externí" jméno vedoucího/oponenta, vyrábíme User instaci pro něj podle ekvilibristiky jména -- tohle parsování (nebo validace v importu) neumí zkontrolovat, že vyráběné jméno (resp. asi username) je příliš dlouhé a pětikiluje namísto validační chyby

řešení je asi vyrobit ten model user, pustit nad ním full_clean a ukládat až později (což by mělo Django dělat už teď, takže je zvlášní, že je to chyba)

@staticmethod
def get_or_create_from_name(*, name: str, thesis_id: Optional[str]) -> typing.Tuple[Optional['User'], bool]:
from apps.accounts.models import User
name = name.replace('.', '. ').strip()
if name.strip() == '-':
return None, False
if len(name) > 28:
# two names in one cell
name = name.split(',', 1)[0]
degree_after = None
if ',' in name:
name, degree_after = name.rsplit(',', 1)
[last_name, *degrees_before] = tuple(filter(None, name.strip().rsplit(' ', 2)[::-1]))
first_name = degrees_before[0] if degrees_before else ''
if thesis_id:
# is student
username = f'{last_name.strip()[:3].ljust(3, "0")}{thesis_id:05}'
else:
username = last_name.strip()
username = slugify(username.strip().lower())
if User.objects.filter(username=username).exists():
pass # username = f'{username}.{thesis_id}'
return User.objects.get_or_create(
username=username,
defaults=dict(
last_name=last_name.strip(),
first_name=first_name.strip(),
degree_after=degree_after or None,
degree_before=''.join(degrees_before[1:]).replace(' ', '').replace('.', '. ') or None,
is_active=False, # activation by ldap sync
),
)

def _load_reviewer(self, reviewer: str):
from apps.accounts.models import User
from django.contrib.auth.models import Group
if ' ' not in reviewer:
# probably username, internal
user = User.school_users.teachers().filter(username=reviewer).first()
if not user:
raise ValidationError(_('Unknown user {}').format(reviewer))
return user
# external
user = User.objects.get_or_create_from_name(name=reviewer, thesis_id=None)[0]
user.groups.add(Group.objects.get_or_create(name='teacher')[0])
user.is_active = False
return user

@thejoeejoee thejoeejoee added bug Something isn't working python Pull requests that update Python code labels Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Pull requests that update Python code
Projects
None yet
Development

No branches or pull requests

1 participant