def import_projects(self): proj_names = [] mapping, proj_df = self.get_mapping_and_df('project_project') for values in mapping.iterate_dataframe(proj_df): proj_names.append(values['name']) self.log_info(f'Importing {len(proj_names)} projects: ' + ', '.join(proj_names)) project_types = set() # type: Set[str] for values in mapping.iterate_dataframe(proj_df): proj_id, proj_name, proj_type = values['id'], values['name'], str( values['type_id']) project_types.add(proj_type) if proj_type in self.missing_doc_types: raise Exception( f'Doc. type {self.missing_doc_types[proj_type]} ' + f'was not found, project "{proj_name}"') proj_type = self.document_types[proj_type] # TRANSACTION starts here with transaction.atomic(): with ModelLock(None, Project, ModelLock.LOCK_MODE_ACCESS_EXCLUSIVE): existing_projects = list( Project.all_objects.filter(name=proj_name).values_list( 'pk', 'type_id')) if not existing_projects: # create project self.log_info(f'Creating project "{proj_name}"') new_proj = Project() new_proj.name = proj_name new_proj.type_id = proj_type new_proj.status_id = values['status_id'] new_proj.send_email_notification = values[ 'send_email_notification'] == 't' new_proj.hide_clause_review = values[ 'hide_clause_review'] == 't' new_proj.save() self.project_ids[proj_id] = new_proj.pk continue # there might be more than one project with the same name # some of these projects might have the required type matching_projects = [ p for p in existing_projects if p[1] == proj_type ] if not matching_projects: raise Exception(f'''Importing document type ({proj_type}) differs from the document type of the selected project ({existing_projects[0][1]}).''') self.project_ids[proj_id] = matching_projects[0][0] if self.project: project_types = {self.project.type_id} self.check_project_doc_field_types(project_types)
def post(self, request): project_form = ProjectForm(request.POST) if project_form.is_valid(): name = request.POST['name'] type = request.POST['type'] detail = request.POST['detail'] creator = UserProfile.objects.get(username=request.user) project = Project(name=name, type=type, detail=detail, creator=creator) project.save() else: return render(request, 'project_add.html', {'error': project_form.errors}) return HttpResponseRedirect(reverse('project_list'))
def getProjectDetail(): if request.method == 'GET': projects = Project.query.filter( Project.is_delete == True).order_by(-Project.create_time).all() return render_template('add/projectDetail.html', projects=projects) else: projectname = request.form.get('projectname') if projectname: project = Project.query.filter_by(project_name=projectname).all() if not project: project = Project(project_name=projectname) db.session.add(project) db.session.commit() return render_template('add/projectDetail.html') else: return render_template('add/projectDetail.html') else: pass
def create(self, validated_data): user = self.context.get('request').user project = Project(**validated_data, user=user) project.save() return project