-
Tor-Einar Skog authoredTor-Einar Skog authored
models.py 4.16 KiB
#
# Copyright (c) 2014 NIBIO <http://www.nibio.no/>.
#
# This file is part of VIPSWeb.
# VIPSWeb is free software: you can redistribute it and/or modify
# it under the terms of the NIBIO Open Source License as published by
# NIBIO, either version 1 of the License, or (at your option) any
# later version.
#
# VIPSWeb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# NIBIO Open Source License for more details.
#
# You should have received a copy of the NIBIO Open Source License
# along with VIPSWeb. If not, see <http://www.nibio.no/licenses/>.
#
import requests
from django.conf import settings
from django.utils import translation
class CropCategory:
def __init__(self,
crop_category_id,
name,
crop_ids
):
self.crop_category_id = crop_category_id
self.name = name
self.crop_ids = crop_ids
@staticmethod
def get_crop_categories(language):
crop_categories = []
for item in CropCategory.get_crop_categories_from_vipslogic_as_json():
crop_categories.append(CropCategory.get_instance_from_dict(item,language))
return crop_categories
@staticmethod
def get_crop_categories_from_vipslogic():
request_result = requests.get("%s://%s/rest/organism/cropcategory/%s" % (settings.VIPSLOGIC_PROTOCOL, settings.VIPSLOGIC_SERVER_NAME, settings.VIPS_ORGANIZATION_ID))
return request_result
@staticmethod
def get_crop_categories_from_vipslogic_as_json():
return CropCategory.get_crop_categories_from_vipslogic().json()
@staticmethod
def get_crop_categories_from_vipslogic_as_text():
return CropCategory.get_crop_categories_from_vipslogic().text
@staticmethod
def get_instance_from_dict(dict,language):
local_name = dict["defaultName"]
for crop_category_local in dict["cropCategoryLocalSet"]:
if crop_category_local["cropCategoryLocalPK"]["locale"] == language:
local_name = crop_category_local["localName"]
return CropCategory(dict["cropCategoryId"],
local_name,
dict["cropOrganismIds"]
)
class Organism:
def __init__(self,
organism_id,
latin_name,
trade_name,
parent_organism_id,
hierarchy_category_id,
local_name
):
self.organism_id = organism_id
self.latin_name = latin_name
self.trade_name = trade_name
self.parent_organism_id = parent_organism_id
self.hierarchy_category_id = hierarchy_category_id
self.local_name = local_name
@staticmethod
def get_instance_from_dict(theDict):
if theDict == None:
return None
# Get the local name (if it exists, fallback is English, then Latin name)
local_name = ""
for item in theDict["organismLocaleSet"]:
if item["organismLocalePK"]["locale"] == translation.get_language() \
or (local_name == "" and item["organismLocalePK"]["locale"] == "en"):
local_name = item["localName"]
if local_name == "":
local_name = theDict["latinName"]
return Organism(theDict["organismId"],
theDict["latinName"],
theDict["tradeName"],
theDict["parentOrganismId"],
theDict["hierarchyCategoryId"],
local_name
)
@staticmethod
def get_all_crops():
crops = []
for item in Organism.get_all_crops_from_vipslogic():
crop = Organism.get_instance_from_dict(item)
crops.append(crop)
return crops
@staticmethod
def get_all_crops_from_vipslogic():
request_result = requests.get("%s://%s/rest/organism/crop/list" % (settings.VIPSLOGIC_PROTOCOL, settings.VIPSLOGIC_SERVER_NAME))
return request_result.json()