def form_valid(self, form): """ Sets EDID and identification when creating new timing. Used for CreateView and UpdateView. """ # For CreateView, set EDID if not form.instance.EDID_id: form.instance.EDID = form.edid if not form.instance.identification: # Get count of available timings count = self.model.objects.filter(EDID=form.instance.EDID)\ .count() # Set identification to count + 1 form.instance.identification = count + 1 # Set the user form.instance.user = self.request.user # Set revision comment if isinstance(self, CreateView): comment = 'Created %s %s.' elif isinstance(self, UpdateView): comment = 'Updated %s %s.' reversion.set_comment(comment % ( form.instance._meta.verbose_name, form.instance )) reversion.add_to_revision(form.instance.EDID) return super(TimingMixin, self).form_valid(form)
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 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 judge(self, *args, rejudge=False, force_judge=False, rejudge_user=None, **kwargs): if force_judge or not self.is_locked: if rejudge: with revisions.create_revision(manage_manually=True): if rejudge_user: revisions.set_user(rejudge_user) revisions.set_comment('Rejudged') revisions.add_to_revision(self) judge_submission(self, *args, rejudge=rejudge, **kwargs)
def save(self, commit_desc=None, commit_user=None, *args, **kwargs): if not commit_desc: super().save(*args, **kwargs) return with reversion.create_revision(manage_manually=True, atomic=True): super().save(*args, **kwargs) reversion.set_comment(commit_desc) reversion.set_user(commit_user) reversion.add_to_revision(self)
def create(self, commit_desc=None, commit_user=None, *args, **kwargs): if not commit_desc: return super(ProductQuerySet, self).create(*args, **kwargs) with reversion.create_revision(manage_manually=True, atomic=True): obj = super(ProductQuerySet, self).create(*args, **kwargs) reversion.set_comment(commit_desc) reversion.set_user(commit_user) reversion.add_to_revision(obj) return obj
def save(self, commit_desc=None, commit_user=None, *args, **kwargs): self.full_clean() if not commit_desc: return super(Company, self).save(*args, **kwargs) with reversion.create_revision(manage_manually=True, atomic=True): obj = super(Company, self).save(*args, **kwargs) reversion.set_comment(commit_desc) reversion.set_user(commit_user) reversion.add_to_revision(obj) return obj
def get_or_create(self, commit_desc=None, commit_user=None, *args, **kwargs): if not commit_desc: return super().get_or_create(*args, **kwargs) with reversion.create_revision(manage_manually=True, atomic=True): obj, created = super().get_or_create(*args, **kwargs) if created: reversion.set_comment(commit_desc) reversion.set_user(commit_user) reversion.add_to_revision(obj) return obj, created
def save(self, *args, skip_last_updated=False, **kwargs): if self.is_dirty(check_relationship=True): dirty_fields = self.get_dirty_fields(check_relationship=True) # If only the signature changed, skip the rest of the updates here, # to avoid invalidating that signature. If the signature changed # along with something else, raise an error, since the signature # will probably be invalid. Otherwise, try and generate a new # signature for the object. If that fails, remove the signature, # because it is invalid now. dirty_field_names = list(dirty_fields.keys()) should_sign = False if dirty_field_names == ['signature']: super().save(*args, **kwargs) return elif 'signature' in dirty_field_names and self.signature is not None: # Setting the signature while also changing something else is probably # going to make the signature immediately invalid. Don't allow it. raise ValidationError('Signatures must change alone') else: should_sign = True # Increment the revision ID, unless someone else tried to change it. if 'revision_id' not in dirty_field_names: self.revision_id += 1 if reversion.is_active(): reversion.add_to_revision(self) if not skip_last_updated: self.last_updated = timezone.now() if should_sign: try: if self.id is None: # Save now, to get an ID for the signature. super().save(*args, **kwargs) # Change from insert to update, so the save() call at the end doesn't fail kwargs['force_insert'] = False self.update_signature() except ImproperlyConfigured: self.signature = None super().save(*args, **kwargs)
def handle(self, *app_labels, **options): verbosity = getattr(options, "verbosity", 0) using = getattr(options, "using", None) model_db = getattr(options, "model_db", None) comment = getattr(options, "comment", "Initial version.") batch_size = getattr(options, "batch_size", 500) # 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 # _safe_subquery does not work in Django 1.7 # So we replace it with this query for now live_objs = model._default_manager.db_manager(model_db).all() # 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): 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_edid(self, edid_base64, edid_data): # Override RevisionMiddleware # RevisionMiddleware creates a revision per request, # we want a revision per EDID object created with reversion.create_revision(manage_manually=True): # Set revision comment reversion.set_comment('EDID parsed.') # Create EDID entry edid_object = EDID.create(file_base64=edid_base64, edid_data=edid_data) # Save the entry edid_object.save() # Add timings edid_object.populate_timings_from_parser(edid_data) # Save the updated entry edid_object.save() # Create revision for EDID reversion.add_to_revision(edid_object) self.edid_list.append(edid_object)
def save(self, *args, skip_last_updated=False, **kwargs): if self.is_dirty(check_relationship=True): dirty_fields = self.get_dirty_fields(check_relationship=True) # If only the signature changed, skip the rest of the updates here, to avoid # invalidating that signature. If anything else changed, remove the signature, # since it is now invalid. dirty_field_names = list(dirty_fields.keys()) should_sign = False if dirty_field_names == ['signature']: super().save(*args, **kwargs) return elif 'signature' in dirty_field_names and self.signature is not None: # Setting the signature while also changing something else is probably # going to make the signature immediately invalid. Don't allow it. raise ValidationError('Signatures must change alone') else: should_sign = True # Increment the revision ID, unless someone else tried to change it. if 'revision_id' not in dirty_field_names: self.revision_id += 1 if reversion.is_active(): reversion.add_to_revision(self) if not skip_last_updated: self.last_updated = timezone.now() if should_sign: try: self.update_signature() except ImproperlyConfigured: self.signature = None super().save(*args, **kwargs)
def get(self, request, *args, **kwargs): edid_pk = kwargs.get('edid_pk', None) identification = int(kwargs.get('identification', None)) direction = kwargs.get('direction', None) if direction == 'up': if identification == 1: return HttpResponseBadRequest( 'You can not move up a timing if it is the first one.' ) prev_timing = self.model.objects.get( EDID_id=edid_pk, identification=identification - 1 ) current_timing = self.model.objects.get( EDID_id=edid_pk, identification=identification ) prev_timing.identification += 1 prev_timing.user = request.user prev_timing.save() current_timing.identification -= 1 current_timing.user = request.user current_timing.save() # Set revision comment reversion.set_comment('Moved %s %s up.' % ( current_timing._meta.verbose_name, current_timing )) reversion.add_to_revision(current_timing.EDID) return HttpResponseRedirect(self.get_success_url()) if direction == 'down': count = self.model.objects.filter(EDID_id=edid_pk).count() if identification == count: return HttpResponseBadRequest( 'You can not move down a timing if it is the last one.' ) current_timing = self.model.objects.get( EDID_id=edid_pk, identification=identification ) next_timing = self.model.objects.get( EDID_id=edid_pk, identification=identification + 1 ) next_timing.identification -= 1 next_timing.user = request.user next_timing.save() current_timing.identification += 1 current_timing.user = request.user current_timing.save() # Set revision comment reversion.set_comment('Moved %s %s down.' % ( current_timing._meta.verbose_name, current_timing )) reversion.add_to_revision(current_timing.EDID) return HttpResponseRedirect(self.get_success_url())