def import_bdo_excel(file): import xlrd from core.models import project, phases, project_phase, users, groups, priorities wb = xlrd.open_workbook(filename=file) wb.sheet_names() sheet = wb.sheet_by_index(0) for row in range(sheet.nrows): if row == 0: continue try: p = project() p_phase = None p_phase_created = False p.name = sheet.row_values(row)[2] print u'importing {0}'.format(p.name) if len(sheet.row_values(row)[3].__str__()) > 0: phase_num = int(float(sheet.row_values(row)[3])) p_phase = project_phase() p_phase.phase = phases.objects.get(id=phase_num) #p_phase.project = p.id print 'phase seemed to go well' if len(sheet.row_values(row)[4].__str__()) > 0: print 'priority len detected' pri_num = int(float(sheet.row_values(row)[4])) print u'pri_num = {0}'.format(pri_num.__str__()) if not priorities.objects.filter(code=pri_num).exists(): new_pri = priorities() new_pri.code = pri_num new_pri.name = pri_num new_pri.display_order = pri_num new_pri.save() p.priority = priorities.objects.get(code=pri_num) print 'priority seemed to go well' if len(sheet.row_values(row)[5]) > 0: print 'sponsor len detected' p_spo = sheet.row_values(row)[5] p.sponsor_imported = p_spo print 'sponsor seemed to go well' if len(sheet.row_values(row)[6]) > 0: p_grp = sheet.row_values(row)[6] if not groups.objects.filter(code = p_grp): new_grp = groups() new_grp.code = p_grp new_grp.description = p_grp new_grp.save() p.group = groups.objects.get(code=p_grp) print 'group seemed to go well' p.description = sheet.row_values(row)[7] p.save() if p_phase is not None: p_phase.project = p p_phase.save() print 'saved' except Exception, e: print(u'oh f**k: '+e.__str__())
def import_to_projects(): from django.db.models import Max all_excel_projects = project_imported_from_excel.objects.all() for item in all_excel_projects: print "importing item:"+item.name p = project() p.name = item.name p.description = item.name default_group = "unset" default_group_name = "unset" #project group if not groups.objects.filter(code = default_group).exists(): new_group = groups() new_group.code = default_group new_group.description = default_group_name new_group.save() p.group = groups.objects.get(code = default_group) #set sponsor to ignatov p.role_sponsor = users.objects.get(name = 'ignatov') #project manager if not users.objects.filter(name = item.manager).exists(): new_user = users() new_user.name = item.manager new_user.save() p.role_pm = users.objects.get(name = item.manager) #priority if not priorities.objects.filter(name = item.priority).exists(): new_priority = priorities() new_priority.name = item.priority if priorities.objects.count() > 0: new_priority.display_order = priorities.objects.all().aggregate(Max('display_order')).values()[0]+1 else: new_priority.display_order = 0 new_priority.save() p.priority = priorities.objects.get(name = item.priority) #initiating division if not divisions.objects.filter(code = item.initiating_division).exists(): new_division = divisions() new_division.code = item.initiating_division new_division.full_name = item.initiating_division new_division.save() p.initiating_division = divisions.objects.get(code = item.initiating_division) #set initiating person to ignatov p.initiating_person = users.objects.get(name = 'ignatov') #initiation date p.initiation_date = datetime.today() #project_date_start if not item.date_start == None: p.date_start = item.date_start else: p.date_start = datetime.today() #project_date_end if not item.date_end == None: p.date_end = item.date_end else: p.date_end = datetime(2012,6,20) p.save() current_phase = project_phase() current_phase.project = p if not phases.objects.filter(name = item.phase).exists(): new_phase = phases() new_phase.name = item.phase new_phase.code = item.phase new_phase.step_order = phases.objects.all().aggregate(Max('step_order')).values()[0]+1 new_phase.save() current_phase.phase = phases.objects.get(name = item.phase) if not item.date_start == None: current_phase.date_start_plan_original = item.date_start else: current_phase.date_start_plan_original = datetime.today() if not item.phase_date_end == None: current_phase.date_end_plan_original = item.phase_date_end else: current_phase.date_end_plan_original = datetime.today() current_phase.is_closed = False current_phase.is_finished = False current_phase.is_approved_for_sc = False current_phase.save() if (item.progress != None or item.resources != None or item.risks != None): print " oh - there is progress indicators" current_phase_history = project_phase_history() current_phase_history.project_phase = current_phase if item.progress: print " there is progress :)" current_phase_history.indicator_progress = progress_indicator.objects.get(name = item.progress) if item.resources: current_phase_history.indicator_resources = progress_indicator.objects.get(name = item.resources) if item.risks: current_phase_history.indicator_risks = progress_indicator.objects.get(name = item.risks) current_phase_history.comment = 'automatically imported progress from Excel' current_phase_history.date_submitted = datetime.now() if item.date_start: current_phase_history.date_start_plan = item.date_start else: current_phase_history.date_start_plan = datetime.today() current_phase_history.submitted_by = users.objects.get(name = 'sasha') current_phase_history.save()