def populate_crops(): for common_name in CROPS: common_name = common_name family = CROPS[common_name][0] genus = CROPS[common_name][1] species = CROPS[common_name][2] existing_family = Family.objects.filter(name=family) if existing_family.count() > 0: print " Family ", family, " already exists in database." existing_family = existing_family[0] else: f = Family(name=family) f.save() existing_family = f print " Saved", family, "as Family." existing_genus = Genus.objects.filter(name=genus) if existing_genus.count() > 0: print " Genus ", genus, " already exists in database." existing_genus = existing_genus[0] else: g = Genus(name=genus, family=existing_family) g.save() existing_genus = g print " Saved", genus, "as Genus." existing_species = Species.objects.filter(species=species, genus=existing_genus) if existing_species.count() > 0: print " Species ", species, " already exists in database." existing_species = existing_species[0] else: s = Species(species=species, genus=existing_genus) s.save() existing_species = s print " Saved", species, "as Species." existing_crop = Crop.objects.filter(species=existing_species) if existing_crop.count() > 0: print " Crop ", common_name, " already exists in database." existing_crop = existing_crop[0] else: c = Crop(species=existing_species) c.save() existing_crop = c print " Saved", common_name, "as Crop." existing_common_name = CommonName.objects.filter(crop=existing_crop, name=common_name) if existing_common_name.count() > 0: print " Common name for ", species, " already exists in database." existing_common_name = existing_common_name[0] else: cn = CommonName(crop=existing_crop, name=common_name, preferred=True) cn.save() existing_common_name = cn print " Saved", common_name, " for ", species, "as CommonName."
def postSpecies(request): if request.method == 'POST': body = json.loads(request.body) common_name = body['common_name'] newSpecies = Species(common_name=common_name) newSpecies.save() if body.has_key("flower_color"): flower_color = Color.objects.get(color="yellow-green") newSpecies.flower_color.add(flower_color) newSpecies.save() species_serialized = json.dumps(newSpecies.as_dict()) return HttpResponse(species_serialized) return HttpResponse("No body")
def insert_test_species(clean=False): if clean: clean_species() iucn_cat = IucnRedListCategory.objects.get(code='LC') with reversion.create_revision(): element_species = ElementSpecies() element_species.other_code = "lontra_cnd" element_species.iucn_red_list_category = iucn_cat element_species.save() species = Species() species.tsn = 180549 species.first_common = 'North American river otter' species.name_sci = 'Lontra canadensis' species.element_species = element_species species.family = 'Mustelidae' species.family_common = 'Mustelids' species.phylum = 'Chordata' species.phylum_common = 'Chordates' species.save() element_species = ElementSpecies() element_species.other_code = "bl_bear" element_species.iucn_red_list_category = iucn_cat element_species.save() species = Species() species.tsn = 180544 species.first_common = 'American black bear' species.name_sci = 'Ursus americanus' species.element_species = element_species species.family = 'Ursidae' species.family_common = 'Bears' species.phylum = 'Chordata' species.phylum_common = 'Chordate' species.save()
def csvToDB(request): if request.method == 'POST': file = request.FILES['file'] reader = csv.DictReader(file) result = [] for row in reader: newSpecies = Species( common_name=row['Common Name'], scientific_name=row['Scientific Name'], usda_symbol=row['Symbol'], base_age_height=strToFloat( row['Height at Base Age, Maximum (feet)']), mature_height=strToFloat(row['Height, Mature (feet)']), fire_resistant=yesNoToBool(row['Fire Resistance']), flower_conspicuous=yesNoToBool(row['Flower Conspicuous']), allelopathic=yesNoToBool(row['Known Allelopath']), leaf_retentive=yesNoToBool(row['Leaf Retention']), resproutable=yesNoToBool(row['Resprout Ability']), ) newSpecies.save() if row['County']: counties = row['County'].split(", ") for county in counties: newCounty = County.objects.get(county=county) newSpecies.county.add(newCounty) if row['Flower Color']: newFlowerColor = Color.objects.get( color=row['Flower Color'].lower()) newSpecies.flower_color.add(newFlowerColor) if row['Shape and Orientation']: newShape = Shape.objects.get( shape=row['Shape and Orientation'].lower()) newSpecies.shape.add(newShape) if row['Category']: newCategory = Category.objects.get( category=row['Category'].lower()) newSpecies.category.add(newCategory) if row['Duration']: durations = row['Duration'].split(", ") for duration in durations: newDuration = Duration.objects.get( duration=duration.lower()) newSpecies.duration.add(newDuration) if row['Active Growth Period']: if (row['Active Growth Period'] == "Year Round"): row['Active Growth Period'] = "Spring, Summer, Fall, Winter" replaced = row['Active Growth Period'].replace(" and ", ", ") growth_periods = replaced.split(", ") for growth_period in growth_periods: newActiveGrowthPeriod = ActiveGrowthPeriod.objects.get( active_growth_period=growth_period.lower()) newSpecies.active_growth_period.add(newActiveGrowthPeriod) if row['Growth Habit']: growth_habits = row['Growth Habit'].split(", ") if 'and' in row['Growth Habit']: growth_habits = row['Growth Habit'].split(" and ") for growth_habit in growth_habits: newGrowthHabit = GrowthHabit.objects.get( growth_habit=growth_habit.lower()) newSpecies.growth_habit.add(newGrowthHabit) if row['Growth Form']: newGrowthForm = GrowthForm.objects.get( growth_form=row['Growth Form'].lower()) newSpecies.growth_form.add(newGrowthForm) if row['Growth Rate']: newGrothRate = GrowthRate.objects.get( growth_rate=row['Growth Rate'].lower()) newSpecies.growth_rate.add(newGrothRate) if row['Lifespan']: newLifespan = Lifespan.objects.get( lifespan=row['Lifespan'].lower()) newSpecies.lifespan.add(newLifespan) if row['Toxicity']: newToxicity = Toxicity.objects.get( toxicity=row['Toxicity'].lower()) newSpecies.toxicity.add(newToxicity) newSpecies.save() result.append(newSpecies) return JsonResponse(result, safe=False)
def csvToDB(request): if request.method == 'POST': file = request.FILES['file'] reader = csv.DictReader(file) result = [] for row in reader: newSpecies = Species( common_name=row['Common Name'], scientific_name=row['Scientific Name'], usda_symbol=row['Symbol'], base_age_height=strToFloat(row['Height at Base Age, Maximum (feet)']), mature_height=strToFloat(row['Height, Mature (feet)']), fire_resistant=yesNoToBool(row['Fire Resistance']), flower_conspicuous=yesNoToBool(row['Flower Conspicuous']), allelopathic=yesNoToBool(row['Known Allelopath']), leaf_retentive=yesNoToBool(row['Leaf Retention']), resproutable=yesNoToBool(row['Resprout Ability']), ) newSpecies.save() if row['County']: counties = row['County'].split(", ") for county in counties: newCounty = County.objects.get(county=county) newSpecies.county.add(newCounty) if row['Flower Color']: newFlowerColor = Color.objects.get(color= row['Flower Color'].lower()) newSpecies.flower_color.add(newFlowerColor) if row['Shape and Orientation']: newShape = Shape.objects.get(shape=row['Shape and Orientation'].lower()) newSpecies.shape.add(newShape) if row['Category']: newCategory = Category.objects.get(category=row['Category'].lower()) newSpecies.category.add(newCategory) if row['Duration']: durations = row['Duration'].split(", ") for duration in durations: newDuration = Duration.objects.get(duration=duration.lower()) newSpecies.duration.add(newDuration) if row['Active Growth Period']: if(row['Active Growth Period'] == "Year Round"): row['Active Growth Period'] = "Spring, Summer, Fall, Winter" replaced = row['Active Growth Period'].replace(" and ", ", ") growth_periods = replaced.split(", ") for growth_period in growth_periods: newActiveGrowthPeriod = ActiveGrowthPeriod.objects.get(active_growth_period=growth_period.lower()) newSpecies.active_growth_period.add(newActiveGrowthPeriod) if row['Growth Habit']: growth_habits = row['Growth Habit'].split(", ") if 'and' in row['Growth Habit']: growth_habits = row['Growth Habit'].split(" and ") for growth_habit in growth_habits: newGrowthHabit = GrowthHabit.objects.get(growth_habit=growth_habit.lower()) newSpecies.growth_habit.add(newGrowthHabit) if row['Growth Form']: newGrowthForm = GrowthForm.objects.get(growth_form=row['Growth Form'].lower()) newSpecies.growth_form.add(newGrowthForm) if row['Growth Rate']: newGrothRate = GrowthRate.objects.get(growth_rate=row['Growth Rate'].lower()) newSpecies.growth_rate.add(newGrothRate) if row['Lifespan']: newLifespan = Lifespan.objects.get(lifespan=row['Lifespan'].lower()) newSpecies.lifespan.add(newLifespan) if row['Toxicity']: newToxicity = Toxicity.objects.get(toxicity=row['Toxicity'].lower()) newSpecies.toxicity.add(newToxicity) newSpecies.save() result.append(newSpecies) return JsonResponse(result, safe=False)