This commit is contained in:
Cja committed 2025-01-14 15:30:17 +03:00
1 parent b087d2252c
commit 44180e77ae
2 files changed
+61 -1

No files matched your search

+30 -1
View File
@@ -62,9 +62,38 @@ class VoteDetail(generics.RetrieveUpdateDestroyAPIView):
lookup_url_kwarg = 'id'
lookup_field = 'id'
class VoteCreate(generics.CreateAPIView):
serializer_class = VoteCreateSerializer
def create(self, request, *args, **kwargs):
user = request.user
# user = User.objects.get(username='9966-00-125')
request.data['author'] = user
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
try:
vote = serializer.save(author=user)
self.create_news(request, vote)
return Response({'status': 'success', 'message': 'Vote and news created successfully.'}, status=200)
except Exception as e:
return Response({'status': 'error', 'message': str(e)}, status=400)
def create_news(self, request, vote):
vote_name = vote.name
vote_author = vote.author
formatted_date = vote.dateend.strftime("%d.%m.%Y %H:%M")
text = f'Пройдите тестирование "{vote_name}" до {formatted_date}'
News.objects.create(
title='Пройдите тестирование!',
text=text,
cdate=datetime.now(),
author=vote_author,
link=request.FILES.get('image'),
)
class VoteCreate(APIView):
class VoteCreate1(APIView):
def post(self, request):
try:
vote = self.create_vote(request)