Beispiel #1
0
def editDog(request):
    params = request.REQUEST
    dog = models.Dog.objects.get(id=params["id"])
    if dog.user != request.user:
        return JsonResponse({
            "error": "You don't have rights to execute this method",
        })
    dog.checkFinishedWalks()
    if "nick" in params:
        dog.nick = params["nick"]
    if "breed" in params:
        dog.breed = params["breed"]
    if "birth_date" in params:
        birthDate = params["birth_date"]
        if birthDate:
            birthDate = dateFromStr(birthDate)
        else:
            birthDate = None
        dog.birthDate = birthDate
    if "weight" in params:
        weight = params["weight"]
        dog.weight = float(weight) if weight else None
    if request.FILES:
        dog.avatarFile = request.FILES["avatarFile"]
    if "collar_id" in params:
        collarId = params["collar_id"]
        dog.collarIdHash = hashlib.md5(collarId).hexdigest() if collarId else None
    dog.save()
    return JsonResponse(dog.toDict())
Beispiel #2
0
def addDog(request):
    params = request.REQUEST
    birthDate = dateFromStr(params["birth_date"]) if "birth_date" in params else None
    dog = models.Dog.objects.create(
        nick=params["nick"],
        breed=params["breed"] if "breed" in params else None,
        birthDate=birthDate,
        weight=params.get("weight", None),
        user=request.user,
        collarIdHash=hashlib.md5(params["collar_id"]).hexdigest() if "collar_id" in params else None,
        avatarFile=request.FILES["avatarFile"] if request.FILES else None
    )
    return JsonResponse(dog.toDict())