def setUp(self): # Add some pets Pet.objects.bulk_create([ Pet(id="1", name="Meow", type="cat"), Pet(id="2", name="Bark", type="dog"), Pet(id="A3", name="", type="rat"), ])
def pet_create(req): if req.method == 'GET': context = { 'form': CreatePetForm() } return render(req, 'pets/pet_create.html', context=context) else: form = CreatePetForm(req.POST) if form.is_valid(): pet = Pet(**form.cleaned_data) pet.save() return redirect('list pets') context = { 'form': form } return render(req, 'pets/pet_create.html', context=context)
def preencher(request): pet = Pet() pet.dono_id = request.session['usuario_id'] pet.data_nascimento = request.POST['data_nascimento'] pet.nome = request.POST['nome'] pet.especie = request.POST['especie'] pet.descricao = request.POST['descricao'] return pet
def upload(request): if request.method == "POST": pet = Pet.create_from_file(request) subprocess.call("convert {} -auto-orient {}".format( pet.mediafile.path, pet.mediafile.path), shell=True) subprocess.call("chmod 744 {}".format(pet.mediafile.path), shell=True) email(pet, request.scheme + "://" + request.get_host()) return redirect("/pets/{}/".format(pet.id)) return render(request, "upload.html")
def home(request): """The home page.""" if not request.GET: return redirect("/?s=" + random.choice(["Cat", "Dog", "Other"])) species = request.GET["s"] if request.GET else "Cat" if request.method == "POST": winner = Pet.objects.get(id=request.POST["winner"]) loser = Pet.objects.get(id=request.POST["loser"]) winner.defeat(loser) return redirect("/?s=" + species + "#choices") pets = Pet.two_random_images(species) return render(request, "home.html", {"pets": pets, "species": species})
def get(self, request: Request) -> Response: """Get a random cat. Args: request (Request): API's request. Returns: Response: A cat's object representation. """ cat = Pet.get_cat() return Response( {"pet": cat.dihydrate}, template_name="pets/pet.html", status=status.HTTP_200_OK, )
def pets_create(request): if request.method == 'GET': context = { 'form': CreatePetForm(), } return render(request, 'pet_create.html', context) form = CreatePetForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('pets index') context = { 'form': form, 'pet': Pet(), } return render(request, 'pet_create.html', context)
class Event(models.Model): pet = models.ForeignKey(Pet(), on_delete=models.CASCADE, default=None) time = models.TimeField(auto_now=False, auto_now_add=False, default=django.utils.timezone.now) date = models.DateField(auto_now=False, auto_now_add=False, default=date.today) comments = models.TextField(blank=True) W = 'Walk' B = 'Bathroom' M = 'Medicine' F = 'Feeding' EVENT_TYPE_CHOICES = { (W, 'Walk'), (B, 'Bathroom'), (M, 'Medicine'), (F, 'Feeding'), } event_type = models.CharField(max_length=1, choices=EVENT_TYPE_CHOICES) class Meta: abstract = False
def create_pet(request): pet = Pet() return persist_pet(request, pet, 'pet_create')
def pull_in_intakes(intakes): for x in intakes: intime = datetime.strptime(x["intake_date"], '%m/%d/%y') if len(Pet.objects.filter(animal_id = x["animal_id"])) == 0: #Create a new pet new_pet = Pet() if x.has_key("name"): new_pet.name = x["name"] else: new_pet.name = "No Name" new_pet.animal_id = x["animal_id"] in_datetime = x["intake_date"] + " " + x["intake_time"] in_datetime_conc = datetime.strptime(in_datetime, '%m/%d/%y %I:%M%p') in_datetime_ft = timezone.make_aware(in_datetime_conc, timezone.utc) new_pet.intake_at = in_datetime_ft new_pet.found_location = x['found_location'] if not location_dict.has_key(x['found_location']): try: geocode_result = gmaps.geocode(x['found_location']) location_dict[x['found_location']] = {"lat": geocode_result[0]['geometry']['location']['lat'], "lon":geocode_result[0]['geometry']['location']['lng']} new_pet.loc_lat = location_dict[x['found_location']]['lat'] new_pet.loc_lon = location_dict[x['found_location']]['lon'] except Exception as e: print "Google exception" + str(e) else: new_pet.loc_lat = location_dict[x['found_location']]['lat'] new_pet.loc_lon = location_dict[x['found_location']]['lon'] new_pet.animal_type = x['animal_type'] new_pet.breed = x['breed'] new_pet.color = x['color'] new_pet.intake_type = x['intake_type'] new_pet.intake_condition = x['intake_condition'] new_pet.sex = x['sex_upon_intake'] new_pet.age = x['age_upon_intake'] new_pet.save() print "New pet added " + x["animal_id"] elif Pet.objects.filter(animal_id = x["animal_id"])[0].loc_lat == "" or Pet.objects.filter(animal_id = x["animal_id"])[0].loc_lat == None or Pet.objects.filter(animal_id = x["animal_id"])[0].sex != x['sex_upon_intake']: #Update existing Pet existing_pet = Pet.objects.filter(animal_id = x["animal_id"])[0] if x.has_key("name"): existing_pet.name = x["name"] else: existing_pet.name = "No Name" existing_pet.found_location = x['found_location'] if not location_dict.has_key(x['found_location']): try: geocode_result = gmaps.geocode(x['found_location']) location_dict[x['found_location']] = {"lat": geocode_result[0]['geometry']['location']['lat'], "lon":geocode_result[0]['geometry']['location']['lng']} existing_pet.loc_lat = location_dict[x['found_location']]['lat'] existing_pet.loc_lon = location_dict[x['found_location']]['lon'] except Exception as e: print "Google exception" + str(e) else: existing_pet.loc_lat = location_dict[x['found_location']]['lat'] existing_pet.loc_lon = location_dict[x['found_location']]['lon'] existing_pet.animal_type = x['animal_type'] existing_pet.breed = x['breed'] existing_pet.color = x['color'] existing_pet.intake_type = x['intake_type'] existing_pet.intake_condition = x['intake_condition'] existing_pet.sex = x['sex_upon_intake'] existing_pet.age = x['age_upon_intake'] existing_pet.save() print "Existing Pet updated " + x["animal_id"] + " Intakes " + str(existing_pet.id)
def handle(self, *args, **options): if Vaccine.objects.exists() or Pet.objects.exists(): print('Pet data already loaded...exiting.') print(ALREADY_LOADED_ERROR_MESSAGE) return print("Creating vaccine data") for vaccine_name in VACCINES_NAMES: vac = Vaccine(name=vaccine_name) vac.save() print("Loading pet data for pets available for adoption") for row in DictReader(open('./pet_data.csv')): pet = Pet() pet.name = row['Pet'] pet.submitter = row['Submitter'] pet.species = row['Species'] pet.breed = row['Breed'] pet.description = row['Pet Description'] pet.sex = row['Sex'] pet.age = row['Age'] raw_submission_date = row['submission date'] submission_date = UTC.localize( datetime.strptime(raw_submission_date, DATETIME_FORMAT)) pet.submission_date = submission_date pet.save() raw_vaccination_names = row['vaccinations'] vaccination_names = [ name for name in raw_vaccination_names.split('| ') if name ] for vac_name in vaccination_names: vac = Vaccine.objects.get(name=vac_name) pet.vaccinations.add(vac) pet.save()
def create_pet(req): return persist(req, Pet(), 'pet_create', 'list_pets')