Created
May 21, 2019 06:06
-
-
Save izznfkhrlislm/61ceb8b40c0d0fd779f2cd5b54255508 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.management.base import BaseCommand | |
from rest_api.models import (Phase, ProcessArea, CMMIPractices, Ceremony, | |
Problem, Glossary) | |
from django.db import IntegrityError | |
import csv | |
import re | |
# phaseTitle = ["Product Backlog","Sprint Planning","Sprint Execution","Sprint Evaluation"] | |
# for i in phaseTitle: | |
class Command(BaseCommand): | |
dct_manytomany = {'can_be_solved_by': Ceremony,'ceremonies_that_contain': Ceremony, 'problem_that_contain': Problem} | |
dct_foreignkey = {'to_satisfy': ProcessArea, 'phase': Phase, 'question_for': Phase, | |
'related_ceremony': Ceremony,'process_area':ProcessArea} | |
dct_combinedforeignkey = {'containing_ceremony':Ceremony,'containing_process_area':ProcessArea} | |
def _create_models(self, models, file_name): | |
with open(file_name) as csv_file: | |
csv_reader = csv.reader(csv_file, delimiter=',') | |
line_count = 0 | |
lst = [] | |
for row in csv_reader: | |
attr = {} | |
if line_count == 0: | |
for col in row: | |
lst.append(col) | |
line_count += 1 | |
else: | |
many_to_many = {} | |
for i in range(len(row)): | |
if row[i] == '': | |
continue | |
if lst[i] in self.dct_combinedforeignkey: | |
if self.dct_combinedforeignkey[lst[i]] == Ceremony: | |
attr['process_area'] = ProcessArea.objects.get(related_ceremony=Ceremony.objects.get(title=row[i]),title=row[i+1]) | |
elif lst[i] in self.dct_manytomany: | |
many_to_many[lst[i]] = row[i].split(';') | |
elif lst[i] in self.dct_foreignkey: | |
attr[lst[i]] = self.dct_foreignkey[lst[i]].objects.get(title=row[i]) | |
else: | |
attr[lst[i]] = row[i] | |
try: | |
obj = models.objects.create(**attr) | |
if (many_to_many): | |
for key, values in many_to_many.items(): | |
many_to_many_field = getattr(obj, key) | |
for value in values: | |
value_class = self.dct_manytomany[key] | |
value_obj = value_class.objects.get(title=value) | |
many_to_many_field.add(value_obj) | |
print(models.__name__, obj.title, 'has been created successfully') | |
except IntegrityError: | |
print(models.__name__, attr[lst[0]], 'has already been created') | |
def formatify_text(text, glossaries): | |
for glossary in glossaries: | |
regex = r'(?i)\b' + re.escape(glossary) + r'\b' | |
subs = '#' + glossary + '#' | |
done = False | |
index = 0 | |
new_text = "" | |
while not done: | |
first = text[index:].find('#') | |
if first > -1: | |
new_text += re.sub(regex, subs, text[index:index + first]) | |
second = text[index + first + 1:].find('#') | |
if second == -1: | |
new_text += re.sub(regex, subs, text[index + first:]) | |
break | |
new_text += text[index + first:index + first + second + 2] | |
index = index + first + second + 2 | |
else: | |
new_text += re.sub(regex, subs, text[index:]) | |
done = True | |
text = new_text | |
return text | |
def formatify_details(models, glossaries): | |
for obj in models.objects.all(): | |
new_glossaries = glossaries | |
if models == Glossary: | |
new_glossaries = new_glossaries.copy() | |
new_glossaries.remove(obj.title) | |
new_detail = Command.formatify_text(obj.detail, new_glossaries) | |
obj.detail = new_detail | |
obj.save() | |
print(models.__name__, obj.title, 'detail has been formated successfully') | |
def handle(self, *args, **options): | |
print("POPULATE DATABASE BEGIN!") | |
self._create_models(Phase, 'rest_api/management/commands/Phase.csv') | |
self._create_models(Ceremony, 'rest_api/management/commands/Ceremony.csv') | |
self._create_models(Problem, 'rest_api/management/commands/Problem.csv') | |
self._create_models(ProcessArea, 'rest_api/management/commands/ProcessArea.csv') | |
self._create_models(CMMIPractices, 'rest_api/management/commands/CMMIPractice.csv') | |
self._create_models(Glossary, 'rest_api/management/commands/Glossary.csv') | |
dct = {} | |
for glossary in Glossary.objects.all(): | |
word_count = len(glossary.title.split()) | |
if word_count not in dct: | |
dct[word_count] = [] | |
dct[word_count].append(glossary.title) | |
lst = [] | |
for key in sorted(dct.keys(), reverse=True): | |
lst += dct[key] | |
Command.formatify_details(Phase, lst) | |
Command.formatify_details(Ceremony, lst) | |
Command.formatify_details(Problem, lst) | |
Command.formatify_details(Glossary, lst) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment