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 odesílá notifikace o přiřazení práce #296

Open
thejoeejoee opened this issue Oct 11, 2024 · 0 comments
Open

Testový import odesílá notifikace o přiřazení práce #296

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

tahle importující elektrárna běží v transakci, co se rollbackne pokud jsou chyby nebo pokud je ten import pouze testovací -- to ale neví django-lifecycle, který přes hooky odesílá emaily o přiřazení

def import_from_file(self, file_to_import: TemporaryUploadedFile, published_at: datetime.date,
is_final_import: bool):
sid = transaction.savepoint()
try:
wb = load_workbook(file_to_import.temporary_file_path(), read_only=True, data_only=True, )
data_or_response = self._load_xls(wb=wb)
except InvalidFileException as e:
data_or_response = self._load_csv(file_to_import)
if isinstance(data_or_response, HttpResponse):
return data_or_response
statuses = []
for line in data_or_response:
thesis = self.model(published_at=published_at, note=dict(imported_from=tuple(line)))
line_status: List[Dict[str, Any]] = []
data_or_response = self._line_to_dict(line=line)
_store_value = self._prepare_store_value(thesis=thesis, data=data_or_response)
line_status.extend((
_store_value(self._set_category),
_store_value(self._set_title),
_store_value(self._set_supervisor),
_store_value(self._set_opponent),
_store_value(self._set_submit_deadline),
))
try:
thesis.full_clean(exclude=('registration_number', 'published_at'))
except ValidationError as e:
line_status.append(dict(
value=', '.join(map(lambda t: f'{t[0]}: {t[1]}', e.error_dict.items())),
error=True,
))
else:
line_status.append(dict(
success=True,
))
line_has_error = any(map(methodcaller('get', 'error'), line_status))
if not line_has_error:
thesis.save()
line_status.insert(0, _store_value(self._set_authors))
else:
line_status.insert(0, dict())
statuses.append(dict(
statuses=line_status,
error=line_has_error or line_status[0].get('error'),
))
has_error = any(map(methodcaller('get', 'error'), statuses))
if is_final_import and not has_error:
transaction.savepoint_commit(sid)
message = _('Theses have been imported.')
else:
transaction.savepoint_rollback(sid)
message = _('Cannot import theses containing errors.')
return Response(data=dict(
statuses=statuses,
error=has_error,
message=message,
success=True,
), status=HTTP_400_BAD_REQUEST if has_error else HTTP_201_CREATED)

asi by šel použít tenhle parametr při thesis.save() https://rsinger86.github.io/django-lifecycle/advanced/#suppressing-hooked-methods

ale nutno zkontrolovat, že to umí i ve verzi, kterou používáme (byl na té knihovně celkem zběsilý vývoj a máme tam zapinovanou nějakou starší stable verzi)

@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