назначение новости, change_news

This commit is contained in:
cja committed 2025-06-04 13:04:02 +03:00
1 parent 2c6a56560b
commit 9591e96328
1 file changed
+41 -14
+41 -14
View File
@@ -276,6 +276,7 @@ class TestingView(generics.ListCreateAPIView):
dictemployees = DictEmployee.objects.filter(id__in=request.data["user_id"]) dictemployees = DictEmployee.objects.filter(id__in=request.data["user_id"])
users = User.objects.filter(user__in=dictemployees).distinct() users = User.objects.filter(user__in=dictemployees).distinct()
test = Test.objects.get(id=request.data["test_id"]) test = Test.objects.get(id=request.data["test_id"])
testings =[]
for user in users: for user in users:
testing = Testing.objects.create( testing = Testing.objects.create(
user=user, user=user,
@@ -284,8 +285,10 @@ class TestingView(generics.ListCreateAPIView):
completion_date=request.data["completion_date"], completion_date=request.data["completion_date"],
test_passed=False, test_passed=False,
) )
testings.append(testing)
if request.data.get("news"): if request.data.get("news"):
self.create_news(request, testing) if testings:
self.create_news(request, testings)
return Response( return Response(
{"status": "success", "message": "Testing created successfully."}, {"status": "success", "message": "Testing created successfully."},
status=200, status=200,
@@ -302,11 +305,16 @@ class TestingView(generics.ListCreateAPIView):
"""when removing testing from an employee""" """when removing testing from an employee"""
try: try:
testing_ids = request.data.get("id", []) testing_ids = request.data.get("id", [])
Testing.objects.filter(id__in=testing_ids).update(is_active=False) testings = Testing.objects.filter(id__in=testing_ids)
news_ids = Testing.objects.filter(id__in=testing_ids testings.update(is_active=False)
).values_list('news_id', flat=True).distinct() news_ids = testings.values_list('news_id', flat=True).distinct()
news_ids_list = [nid for nid in news_ids if nid is not None] news_ids_list = [nid for nid in news_ids if nid is not None]
Testing.objects.filter(id__in=testing_ids).update(news=None) for testing in testings:
if testing.news:
self.change_news(testing)
testing.news = None
testing.save()
if not Testing.objects.filter(news__id__in=news_ids_list):
News.objects.filter(id__in=news_ids_list).delete() News.objects.filter(id__in=news_ids_list).delete()
return Response({"status": "success", "message": "Testing is not active."}, return Response({"status": "success", "message": "Testing is not active."},
status=200) status=200)
@@ -325,13 +333,13 @@ class TestingView(generics.ListCreateAPIView):
print(e) print(e)
return Response({"status": "error", "message": str(e)}, status=400) return Response({"status": "error", "message": str(e)}, status=400)
def create_news(self, request, testing): def create_news(self, request, testings):
""" """
Комментарий Комментарий
""" """
# formatted_date = datetime.strptime( test_name = testings[0].test.name
# request.data["completion_date"][:-1], "%Y-%m-%dT%H:%M:%S.%f" test_id = testings[0].test.id
# ).strftime("%d.%m.%Y %H:%M") test_file = testings[0].test.file
date_str = request.data["completion_date"] date_str = request.data["completion_date"]
if date_str.endswith('Z'): if date_str.endswith('Z'):
date_str = date_str[:-1] + '+0000' # pytz требует формат +HHMM date_str = date_str[:-1] + '+0000' # pytz требует формат +HHMM
@@ -341,20 +349,39 @@ class TestingView(generics.ListCreateAPIView):
dt_msk = dt_utc.astimezone(msk_tz) dt_msk = dt_utc.astimezone(msk_tz)
formatted_date = dt_msk.strftime("%d.%m.%Y %H:%M") formatted_date = dt_msk.strftime("%d.%m.%Y %H:%M")
testing_url = make_clickable(testing.test.name, testing_url = make_clickable(test_name,
f"/test/testdetail/{testing.test.id}?mode=exam") f"/test/testdetail/{test_id}?mode=exam")
text = f'Пройдите тестирование "{testing_url}" до {formatted_date} '
texts = []
for testing in testings:
texts.append(testing.user.user.fio)
text += ", ".join(testing.user.user.fio for testing in testings) + "."
news = News( news = News(
title="Пройдите тестирование!", title="Пройдите тестирование!",
text=f'Пройдите тестирование "{testing_url}" до {formatted_date}', text=text,
cdate=request.data["datestart"], cdate=request.data["datestart"],
author=request.user, author=request.user,
link=testing.test.file, link=test_file,
) )
news.save() news.save()
for testing in testings:
testing.news = news testing.news = news
testing.save() testing.save()
def change_news(self, testing):
if not testing.news:
return
user_fio = testing.user.user.fio
new_text = testing.news.text
substrings = [f'{user_fio}, ', f', {user_fio}']
for substr in substrings:
if substr in new_text:
new_text = new_text.replace(substr, "")
break
testing.news.text = new_text
testing.news.save()
class TestResultView(generics.ListCreateAPIView): class TestResultView(generics.ListCreateAPIView):
"""API for getting and saving user test results.""" """API for getting and saving user test results."""
queryset = TestResult.objects.all() queryset = TestResult.objects.all()