Commit 61a3b8ad authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Use utils method to extract many to many fields

parent da92c961
......@@ -8,6 +8,8 @@ from itertools import islice
import django
from django.core.exceptions import ValidationError
from metagenedb.utils.dict_operations import extract_dict
# Before model import, we need to called django.setup() to Load apps
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "metagenedb.settings")
django.setup()
......@@ -57,20 +59,19 @@ def link_to_function(obj_gene, gene_dict):
def insert_gene(gene_dict):
MANY_TO_MANY_FIELDS = ['kegg_ko']
many_to_many_elements = extract_dict(gene_dict, MANY_TO_MANY_FIELDS)
try:
obj_gene = Gene.objects.get(gene_id=gene_dict.get('gene_id'))
for key, value in gene_dict.items():
if key not in MANY_TO_MANY_FIELDS:
setattr(obj_gene, key, value)
setattr(obj_gene, key, value)
except Gene.DoesNotExist:
obj_gene = Gene(gene_id=gene_dict.get('gene_id'),
gene_length=gene_dict.get('gene_length'))
obj_gene.full_clean()
obj_gene.save()
# Add link to KEGG
if gene_dict.get('kegg_ko') != 'unknown':
link_to_function(obj_gene, gene_dict)
if many_to_many_elements.get('kegg_ko') != 'unknown':
link_to_function(obj_gene, many_to_many_elements)
def insert_gene_list(chunk_genes):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment