80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
from django.shortcuts import render
|
|
from rest_framework.generics import ListAPIView
|
|
# Create your views here.
|
|
from .models import *
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from .serializers import *
|
|
from userauth.models import *
|
|
from general_vacation.models import *
|
|
from django.db.models import Q
|
|
|
|
class LinkAPIView(ListAPIView):
|
|
permission_classes = (AllowAny,)
|
|
serializer_class = LinkSerializer
|
|
|
|
|
|
def get(self, request):
|
|
|
|
queryset = Links.objects.all().order_by('order')
|
|
serializer = self.serializer_class(queryset, many=True)
|
|
return Response(serializer.data)
|
|
|
|
|
|
class NewsAPIView(ListAPIView):
|
|
permission_classes = (AllowAny,)
|
|
serializer_class = NewsSerializer
|
|
|
|
|
|
def get(self, request):
|
|
|
|
queryset = News.objects.all().order_by('cdate')[:10]
|
|
# serializer = self.serializer_class(queryset, many=True)
|
|
# print(serializer.data)
|
|
result = []
|
|
for x in queryset:
|
|
|
|
a=model_to_dict(x)
|
|
a['link']= str(x.link.url)
|
|
a['show'] = False
|
|
a['author'] = model_to_dict(DictEmployee.objects.using('user_directory').get(login=x.author.username))
|
|
result.append(a)
|
|
|
|
return Response(result)
|
|
|
|
class CarouselsAPIView(ListAPIView):
|
|
permission_classes = (AllowAny,)
|
|
serializer_class = CarouselsSerializer
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
|
queryset = Carousels.objects.filter(published=True, cdate__lte=datetime.now(), enddate__gte=datetime.now()).order_by('-cdate')
|
|
|
|
vacation = GeneralVacation.objects.filter(user_id=request.user.id, planned_date__lte=datetime.now() + timedelta(days=7))
|
|
# print(vacation, '-----------------')
|
|
# if vacation:
|
|
# Carousels.objects.update(published=True)
|
|
# else:
|
|
# Carousels.objects.filter().update(published=False)
|
|
# Carousels.objects.update_or_create(id=request.data['nowcomment']['id'],user_id=15, defaults={'comment':request.data['nowcomment']['comment'], 'time_create':datetime.datetime.now()})
|
|
|
|
|
|
result = []
|
|
for x in queryset:
|
|
a=model_to_dict(x)
|
|
a['link']= str(x.link.url)
|
|
a['show'] = False
|
|
a['author'] = model_to_dict(DictEmployee.objects.using('user_directory').get(login=x.author.username))
|
|
if x.sender.img:
|
|
a['sender'] = str(x.sender.img.url)
|
|
else:
|
|
a['sender'] = None
|
|
result.append(a)
|
|
print(request.user.id, '-------------------------------------------')
|
|
|
|
|
|
return Response(result)
|
|
|