def copy_job_version(user, job): last_version = JobHistory.objects.get(job=job, version=job.version) job.version += 1 new_version = JobHistory.objects.create( job=job, version=job.version, change_author=user, comment='', description=last_version.description, global_role=last_version.global_role ) roles = [] for ur in UserRole.objects.filter(job=last_version): roles.append(UserRole(job=new_version, user=ur.user, role=ur.role)) UserRole.objects.bulk_create(roles) try: fdata = FileData(last_version).filedata for i in range(len(fdata)): fdata[i]['type'] = str(fdata[i]['type']) SaveFileData(fdata, new_version) except Exception: new_version.delete() raise job.change_date = new_version.change_date job.change_author = user job.save()
def update(self, instance, validated_data): user_roles = validated_data.pop('user_roles') instance = super().update(instance, validated_data) # Update user roles UserRole.objects.filter(job=instance).delete() UserRole.objects.bulk_create(list(UserRole(job=instance, **rkwargs) for rkwargs in user_roles)) return instance
def save(self, data): user_roles = dict((int(ur['user']), ur['role']) for ur in json.loads(data)) user_roles_to_create = [] for u in User.objects.filter(id__in=user_roles).only('id'): if user_roles[u.id] not in set(x[0] for x in JOB_ROLES): raise BridgeException() user_roles_to_create.append(UserRole(job=self._version, user=u, role=user_roles[u.id])) if len(user_roles_to_create) > 0: UserRole.objects.bulk_create(user_roles_to_create)
def save_job_copy(user, job, name=None): last_version = JobHistory.objects.get(job=job, version=job.version) if isinstance(name, str) and len(name) > 0: job_name = name try: Job.objects.get(name=job_name) except ObjectDoesNotExist: pass else: raise BridgeException('The job name is used already.') else: cnt = 1 while True: job_name = "%s #COPY-%s" % (job.name, cnt) try: Job.objects.get(name=job_name) except ObjectDoesNotExist: break cnt += 1 newjob = Job.objects.create( identifier=hashlib.md5(now().strftime("%Y%m%d%H%M%S%f%z").encode('utf-8')).hexdigest(), name=job_name, change_date=now(), change_author=user, parent=job ) new_version = JobHistory.objects.create( job=newjob, version=newjob.version, change_author=user, change_date=newjob.change_date, comment='', description=last_version.description, global_role=last_version.global_role ) roles = [] for ur in UserRole.objects.filter(job=last_version): roles.append(UserRole(job=new_version, user=ur.user, role=ur.role)) UserRole.objects.bulk_create(roles) try: fdata = FileData(last_version).filedata for i in range(len(fdata)): fdata[i]['type'] = str(fdata[i]['type']) SaveFileData(fdata, new_version) except Exception: new_version.delete() job.version -= 1 job.save() raise return newjob
def create_version(job, kwargs): new_version = JobHistory( job=job, version=job.version, change_author=job.change_author, change_date=job.change_date, comment=kwargs.get('comment', ''), description=kwargs.get('description', '') ) if 'global_role' in kwargs and kwargs['global_role'] in set(x[0] for x in JOB_ROLES): new_version.global_role = kwargs['global_role'] new_version.save() if 'user_roles' in kwargs: user_roles = dict((int(ur['user']), ur['role']) for ur in kwargs['user_roles']) user_roles_to_create = [] for u in User.objects.filter(id__in=list(user_roles)).only('id'): user_roles_to_create.append(UserRole(job=new_version, user=u, role=user_roles[u.id])) if len(user_roles_to_create) > 0: UserRole.objects.bulk_create(user_roles_to_create) return new_version
def create(self, validated_data): user_roles = validated_data.pop('user_roles') instance = super().create(validated_data) UserRole.objects.bulk_create( list(UserRole(job=instance, **rkwargs) for rkwargs in user_roles)) return instance