def projectStatus(request: HttpRequest, project: int)->HttpResponse: """ Returns current status on a project for a user. Used to display details about project progression. """ if not request.user.is_authenticated: return HttpResponse(status=403) try: country = Country.objects.get(pk=project) except Country.DoesNotExist: return HttpResponse(status=404) s,e = quarterRange() shapes = Shape.objects.filter( author = request.user, country = country, date__gte=s,date__lte=e).count() try: NonAnswer.objects.get( author=request.user,country=country, date__gte = s, date__lte=e ) except NonAnswer.DoesNotExist: na = False else: na = True return JsonResponse({ "shapes": shapes, "nonanswer": na })
def unfulfilled(request:HttpRequest)->HttpResponse: """ Get a list of the countries not currently fulfilled by the user """ try: period_start, period_end = quarterRange() def has_answered(c:Country): n_shapes = Shape.objects.filter( author=request.user, country=c, date__gte=period_start,date__lte=period_end).count() nonanswers = NonAnswer.objects.filter(author=request.user, country=c, date__gte=period_start,date__lte=period_end).count() return (n_shapes > 0 or nonanswers > 0) countries = (Country .objects .filter(assignees__pk=request.user.profile.pk) ) countries = [c for c in countries if not has_answered(c)] serialized = CountryMetaSerializer(countries,many=True,context={"request":request}).data return JsonResponse({"countries":serialized}) except Exception as e: return Response(str(e),status=500)
def save(self, *args, **kwargs): s, e = quarterRange(self.date) nas = NonAnswer.objects.filter(author=self.author, country=self.country, date__gte=s, date__lte=e) if nas: nas.delete() super().save(*args, **kwargs)
def test_qrange(self): qr = quarterRange(datetime.date(year=1999, month=2, day=10)) self.assertEqual( quarterRange(datetime.date(year=1999, month=2, day=10)), (datetime.date(year=1999, month=1, day=1), datetime.date(year=1999, month=3, day=lastday(1999, 3)))) self.assertEqual( quarterRange(datetime.date(year=2001, month=4, day=10)), (datetime.date(year=2001, month=4, day=1), datetime.date(year=2001, month=6, day=lastday(2001, 6)))) self.assertEqual( quarterRange(datetime.date(year=2001, month=8, day=10)), (datetime.date(year=2001, month=7, day=1), datetime.date(year=2001, month=9, day=lastday(2001, 9)))) self.assertEqual( quarterRange(datetime.date(year=2001, month=12, day=10)), (datetime.date(year=2001, month=10, day=1), datetime.date(year=2001, month=12, day=lastday(2001, 12))))
def calendar(request:HttpRequest)->HttpResponse: """ Returns the server time information used by the server to timestamp answers. """ s,e = quarterRange() return JsonResponse({ "status":"ok", "quarter": currentQuarter(), "year": currentYear(), "starts": str(s), "ends": str(e) })
def save(self,*args,**kwargs): if self.pk is None and self.date is None: self.date = today() s,e = quarterRange(self.date) for model in self.mutex: if type(model) is str: module,model = os.path.splitext(model) model = model[1:] model = getattr(importlib.import_module(module),model) else: pass others = model.objects.filter( author=self.author, country=self.country, date__gte=s,date__lte=e) others.delete() super().save(*args,**kwargs)
def clearShapes(request: HttpRequest, project: int)->HttpResponse: """ Clear all shapes for a project. """ try: country = Country.objects.get(pk=project) except Country.DoesNotExist: return HttpResponse(status=404) s,e = quarterRange() shapes = Shape.objects.filter( author = request.user, country = country, date__gte = s, date__lte = e) d = len(shapes) shapes.delete() return JsonResponse({"status":"ok","deleted":d})
def nonanswer(request:HttpRequest,project:int)->HttpResponse: """ Endpoint for toggling nonanswer status for a given project. """ try: country = Country.objects.get(pk=project) except Country.DoesNotExist: return HttpResponse(status=404) s,e = quarterRange() try: na = NonAnswer.objects.get( author=request.user, country=country, date__gte=s, date__lte=e) except NonAnswer.DoesNotExist: na = NonAnswer(author=request.user,country=country) na.save() return JsonResponse({"status":"ok","nonanswer":True}) else: na.delete() return JsonResponse({"status":"ok","nonanswer":False})
def get_fulfilled_projects(user): period_start, period_end = quarterRange() shapes = Shape.objects.filter(author=user,date__gte=period_start,date__lte=period_end) nonAnswers = NonAnswer.objects.filter(author=user,date__gte=period_start,date__lte=period_end) return {sh.country for sh in shapes}.union({na.country for na in nonAnswers})
def get_queryset(self): s,e = quarterRange() return Shape.objects.filter(author = self.request.user, date__gte=s,date__lte=e)