def handle(self, *app_labels, **options): verbosity = options["verbosity"] using = options["using"] model_db = options["model_db"] comment = options["comment"] batch_size = options["batch_size"] meta = options["meta"] meta_models = [] for label in meta.keys(): try: model = apps.get_model(label) meta_models.append(model) except LookupError: raise CommandError("Unknown model: {}".format(label)) meta_values = meta.values() # Create revisions. using = using or router.db_for_write(Revision) with transaction.atomic(using=using): for model in self.get_models(options): # Check all models for empty revisions. if verbosity >= 1: self.stdout.write("Creating revisions for {name}".format( name=model._meta.verbose_name, )) created_count = 0 live_objs = _safe_subquery( "exclude", model._default_manager.using(model_db), model._meta.pk.name, Version.objects.using(using).get_for_model( model, model_db=model_db, ), "object_id", ) # Save all the versions. ids = list(live_objs.values_list("pk", flat=True).order_by()) total = len(ids) for i in range(0, total, batch_size): chunked_ids = ids[i:i+batch_size] objects = live_objs.in_bulk(chunked_ids) for obj in objects.values(): with create_revision(using=using): if meta: for model, values in zip(meta_models, meta_values): add_meta(model, **values) set_comment(comment) add_to_revision(obj, model_db=model_db) created_count += 1 reset_queries() if verbosity >= 2: self.stdout.write("- Created {created_count} / {total}".format( created_count=created_count, total=total, )) # Print out a message, if feeling verbose. if verbosity >= 1: self.stdout.write("- Created {total} / {total}".format( total=total, ))
def obj_create(self, bundle, **kwargs): ''' created objects should automatically have a status of Human Created ''' status_update = StatusUpdate.objects.get(status_en='Human Created') username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] status_id = status_update.id if can_assign_users(user) is False and 'assigned_user' in bundle.data: del(bundle.data['assigned_user']) comment_uri = self.create_comment( bundle.data['comment'], status_id, user ) bundle.data['actor_comments'] = [ comment_uri ] with reversion.create_revision(): bundle = super(ActorResource, self)\ .obj_create(bundle, **kwargs) reversion.add_meta( VersionStatus, status='created', user=user ) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def obj_update(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] if self.is_finalized( Incident, kwargs['pk'], 'most_recent_status_incident') and can_finalize(user) is False: raise ImmediateHttpResponse( HttpForbidden('This item has been finalized')) if can_assign_users(user) is False and 'assigned_user' in bundle.data: del (bundle.data['assigned_user']) status_id = self.id_from_url(bundle.data) status_update = StatusUpdate.filter_by_perm_objects.get_update_status( user, status_id) comment_uri = self.create_comment(bundle.data['comment'], status_update.id, user) try: bundle.data['incident_comments'].append(comment_uri) except KeyError: bundle.data['incident_comments'] = [ comment_uri, ] with reversion.create_revision(): bundle = super(IncidentResource, self)\ .obj_update(bundle, **kwargs) reversion.add_meta(VersionStatus, user=user, status='edited') reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def edit_mpa(request, pk): mpa = get_object_or_404(Mpa, pk=pk) if (request.POST): # Got a form submission editform = MpaForm(request.POST, instance=mpa) if editform.is_valid(): mpasaved = editform.save() try: reversion.set_comment(editform.cleaned_data.get("edit_comment")) except: pass try: reversion.set_user(request.user) except: pass try: reversion.add_meta(VersionMetadata, comment=editform.cleaned_data.get("edit_comment"), reference=editform.cleaned_data.get("edit_reference")) except: pass return HttpResponseRedirect(reverse('mpa-siteinfo', kwargs={'pk': pk})) else: editform = MpaForm(instance=mpa) return render_to_response('mpa/Mpa_editform.html', { 'form': editform, 'mpa': mpa, 'respond_url': reverse('mpa-editsite', kwargs={'pk': pk}), }, context_instance=RequestContext(request))
def update_entities(model_dict, model_objects, appendable_keys): ''' model_dict is the key value store of updated elements model_objects are the models, these can be of type Actor, Bulletin or Incident delegates actual updating of fields to update_entity where the field is to be replaced and to update_entity_appendable where the field is to be added to ''' appendable_dict, remainder_dict = separate_field_types( model_dict, appendable_keys) for model in model_objects: model_dict_copy = remainder_dict.copy() model_dict_copy = update_related_actors(model_dict_copy, model) model = update_entity_appendable(appendable_dict, model) model = update_entity_status(model_dict, model) model = update_entity(model_dict_copy, model) user = model_dict['user'] with reversion.create_revision(): model.save() reversion.add_meta(VersionStatus, status='edited', user=user) reversion.set_user(user) comment_text = model_dict['comment'] reversion.set_comment(comment_text)
def update_entities(model_dict, model_objects, appendable_keys): ''' model_dict is the key value store of updated elements model_objects are the models, these can be of type Actor, Bulletin or Incident delegates actual updating of fields to update_entity where the field is to be replaced and to update_entity_appendable where the field is to be added to ''' appendable_dict, remainder_dict = separate_field_types( model_dict, appendable_keys) for model in model_objects: model_dict_copy = remainder_dict.copy() model_dict_copy = update_related_actors(model_dict_copy, model) model = update_entity_appendable(appendable_dict, model) model = update_entity_status(model_dict, model) model = update_entity(model_dict_copy, model) user = model_dict['user'] with reversion.create_revision(): model.save() reversion.add_meta( VersionStatus, status='edited', user=user ) reversion.set_user(user) comment_text = model_dict['comment'] reversion.set_comment(comment_text)
def handle(self, *app_labels, **options): verbosity = options["verbosity"] using = options["using"] model_db = options["model_db"] comment = options["comment"] batch_size = options["batch_size"] meta = options["meta"] meta_models = [] for label in meta.keys(): try: model = apps.get_model(label) meta_models.append(model) except LookupError: raise CommandError("Unknown model: {}".format(label)) meta_values = meta.values() # Create revisions. using = using or router.db_for_write(Revision) with transaction.atomic(using=using): for model in self.get_models(options): # Check all models for empty revisions. if verbosity >= 1: self.stdout.write("Creating revisions for {name}".format( name=model._meta.verbose_name, )) created_count = 0 live_objs = _safe_subquery( "exclude", model._default_manager.using(model_db), model._meta.pk.name, Version.objects.using(using).get_for_model( model, model_db=model_db, ), "object_id", ) # Save all the versions. ids = list(live_objs.values_list("pk", flat=True).order_by()) total = len(ids) for i in range(0, total, batch_size): chunked_ids = ids[i:i + batch_size] objects = live_objs.in_bulk(chunked_ids) for obj in objects.values(): with create_revision(using=using): if meta: for model, values in zip( meta_models, meta_values): add_meta(model, **values) set_comment(comment) add_to_revision(obj, model_db=model_db) created_count += 1 reset_queries() if verbosity >= 2: self.stdout.write( "- Created {created_count} / {total}".format( created_count=created_count, total=total, )) # Print out a message, if feeling verbose. if verbosity >= 1: self.stdout.write("- Created {total} / {total}".format( total=total, ))
def create_revision(self, bundle, user, status_update): with reversion.create_revision(): reversion.set_user(user) reversion.set_comment(bundle.data['comment']) reversion.add_meta( VersionStatus, status=status_update, user=user )
def wrapped(self, request, *args, **kwargs): with reversion.create_revision(): ret = fn(self, request, *args, **kwargs) reversion.set_user(request.user) reversion.add_meta(RevisionProject, project=request.project) if getattr(self, 'log_obj', None): reversion.add_meta(RevisionLogActivity, log_activity=self.log_obj) return ret
def obj_update(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] bundle.data['id'] = kwargs['pk'] if self.can_edit(user, bundle, Actor) is False: raise ImmediateHttpResponse( HttpForbidden('You do not have permission to edit this entity') ) if self.is_finalized( Actor, kwargs['pk'], 'most_recent_status_actor' ) and can_finalize(user) is False: raise ImmediateHttpResponse( HttpForbidden('This item has been finalized') ) if can_assign_users(user) is False and 'assigned_user' in bundle.data: del(bundle.data['assigned_user']) status_id = self.id_from_url(bundle.data) status_update = StatusUpdate.filter_by_perm_objects.get_update_status( user, status_id ) comment_uri = self.create_comment( bundle.data['comment'], status_update.id, user ) if "condition" not in bundle.data: bundle.data['condition'] = None if "POB" not in bundle.data: bundle.data['POB'] = None if "current_location" not in bundle.data: bundle.data['current_location'] = None try: bundle.data['actor_comments'].append(comment_uri) except KeyError: bundle.data['actor_comments'] = [comment_uri, ] with reversion.create_revision(): bundle = super(ActorResource, self)\ .obj_update(bundle, **kwargs) reversion.add_meta( VersionStatus, status='edited', user=user ) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def obj_delete(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] with reversion.create_revision(): bundle = super(BulletinResource, self)\ .obj_delete(bundle, **kwargs) reversion.add_meta(VersionStatus, status='deleted', user=user) reversion.set_user(user) reversion.set_comment('Deleted') update_object.delay(username) return bundle
def obj_update(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] if self.can_edit(user, bundle, Bulletin) is False: raise ImmediateHttpResponse( HttpForbidden('You do not have permission to edit this entity') ) # permission checks if self.is_finalized( Bulletin, kwargs['pk'], 'most_recent_status_bulletin' ) and can_finalize(user) is False: raise ImmediateHttpResponse( HttpForbidden('This item has been finalized') ) if can_assign_users(user) is False and 'assigned_user' in bundle.data: del(bundle.data['assigned_user']) # decide on available status status_id = self.id_from_url(bundle.data) status_update = StatusUpdate.filter_by_perm_objects.get_update_status( user, status_id ) # create the commnt from the status update and the bundled comment comment_uri = self.create_comment( bundle.data['comment'], status_update.id, user ) try: bundle.data['bulletin_comments'].append(comment_uri) except KeyError: bundle.data['bulletin_comments'] = [comment_uri, ] with reversion.create_revision(): bundle = super(BulletinResource, self)\ .obj_update(bundle, **kwargs) reversion.add_meta( VersionStatus, status='edited', user=user ) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def testCanAddMetaToRevision(self): # Create a revision with lots of meta data. with create_revision(): self.test11.save() set_comment("Foo bar") self.assertEqual(get_comment(), "Foo bar") set_user(self.user) self.assertEqual(get_user(), self.user) add_meta(RevisionMeta, age=5) # Test the revision data. revision = get_for_object(self.test11)[0].revision self.assertEqual(revision.user, self.user) self.assertEqual(revision.comment, "Foo bar") self.assertEqual(revision.revisionmeta.age, 5)
def obj_delete(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] with reversion.create_revision(): bundle = super(BulletinResource, self)\ .obj_delete(bundle, **kwargs) reversion.add_meta( VersionStatus, status='deleted', user=user ) reversion.set_user(user) reversion.set_comment('Deleted') update_object.delay(username) return bundle
def obj_update(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] bundle.data['id'] = kwargs['pk'] if self.can_edit(user, bundle, Actor) is False: raise ImmediateHttpResponse( HttpForbidden( 'You do not have permission to edit this entity')) if self.is_finalized( Actor, kwargs['pk'], 'most_recent_status_actor') and can_finalize(user) is False: raise ImmediateHttpResponse( HttpForbidden('This item has been finalized')) if can_assign_users(user) is False and 'assigned_user' in bundle.data: del (bundle.data['assigned_user']) status_id = self.id_from_url(bundle.data) status_update = StatusUpdate.filter_by_perm_objects.get_update_status( user, status_id) comment_uri = self.create_comment(bundle.data['comment'], status_update.id, user) if "condition" not in bundle.data: bundle.data['condition'] = None if "POB" not in bundle.data: bundle.data['POB'] = None if "current_location" not in bundle.data: bundle.data['current_location'] = None try: bundle.data['actor_comments'].append(comment_uri) except KeyError: bundle.data['actor_comments'] = [ comment_uri, ] with reversion.create_revision(): bundle = super(ActorResource, self)\ .obj_update(bundle, **kwargs) reversion.add_meta(VersionStatus, status='edited', user=user) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def obj_update(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] if self.is_finalized( Incident, kwargs['pk'], 'most_recent_status_incident' ) and can_finalize(user) is False: raise ImmediateHttpResponse( HttpForbidden('This item has been finalized') ) if can_assign_users(user) is False and 'assigned_user' in bundle.data: del(bundle.data['assigned_user']) status_id = self.id_from_url(bundle.data) status_update = StatusUpdate.filter_by_perm_objects.get_update_status( user, status_id ) comment_uri = self.create_comment( bundle.data['comment'], status_update.id, user ) try: bundle.data['incident_comments'].append(comment_uri) except KeyError: bundle.data['incident_comments'] = [comment_uri, ] with reversion.create_revision(): bundle = super(IncidentResource, self)\ .obj_update(bundle, **kwargs) reversion.add_meta( VersionStatus, user=user, status='edited' ) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def obj_update(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] if self.can_edit(user, bundle, Bulletin) is False: raise ImmediateHttpResponse( HttpForbidden( 'You do not have permission to edit this entity')) # permission checks if self.is_finalized( Bulletin, kwargs['pk'], 'most_recent_status_bulletin') and can_finalize(user) is False: raise ImmediateHttpResponse( HttpForbidden('This item has been finalized')) if can_assign_users(user) is False and 'assigned_user' in bundle.data: del (bundle.data['assigned_user']) # decide on available status status_id = self.id_from_url(bundle.data) status_update = StatusUpdate.filter_by_perm_objects.get_update_status( user, status_id) # create the commnt from the status update and the bundled comment comment_uri = self.create_comment(bundle.data['comment'], status_update.id, user) try: bundle.data['bulletin_comments'].append(comment_uri) except KeyError: bundle.data['bulletin_comments'] = [ comment_uri, ] with reversion.create_revision(): bundle = super(BulletinResource, self)\ .obj_update(bundle, **kwargs) reversion.add_meta(VersionStatus, status='edited', user=user) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def obj_create(self, bundle, **kwargs): username = bundle.request.GET['username'] user = User.objects.filter(username=username)[0] status_update = StatusUpdate.objects.get(status_en='Human Created') if can_assign_users(user) is False and 'assigned_user' in bundle.data: del (bundle.data['assigned_user']) comment_uri = self.create_comment(bundle.data['comment'], status_update.id, user) bundle.data['incident_comments'] = [comment_uri] with reversion.create_revision(): bundle = super(IncidentResource, self)\ .obj_create(bundle, **kwargs) reversion.add_meta(VersionStatus, status='created', user=user) reversion.set_user(user) reversion.set_comment(bundle.data['comment']) update_object.delay(username) return bundle
def record_destroy(self, request, instance): commit_comment = request.data.get('commit-comment', '') instance.save() reversion.set_user(user=self.request.user) reversion.set_comment(comment=commit_comment) reversion.add_meta(CommitDeletion)
def create_revision(self, bundle, user, status_update): with reversion.create_revision(): reversion.set_user(user) reversion.set_comment(bundle.data['comment']) reversion.add_meta(VersionStatus, status=status_update, user=user)