Creating or updating a mirror object. Relationships are mirrored
 but content is unique. When updating need to grab mirrors based on
 self.slug_was, new objects will use self.slug.
770116
def sync_mirror
      return if self.is_mirrored || !self.site.is_mirrored?
      
      (Cms::Site.mirrored - [self.site]).each do |site|
        mirror = case self
        when Cms::Layout
          m = site.layouts.find_by_identifier(self.identifier_was || self.identifier) || site.layouts.new
          m.attributes = {
            :identifier => self.identifier,
            :parent_id  => site.layouts.find_by_identifier(self.parent.try(:identifier)).try(:id)
          }
          m
        when Cms::Page
          m = site.pages.find_by_full_path(self.full_path_was || self.full_path) || site.pages.new
          m.attributes = {
            :slug       => self.slug,
            :label      => self.slug.blank?? self.label : m.label,
            :parent_id  => site.pages.find_by_full_path(self.parent.try(:full_path)).try(:id),
            :layout     => site.layouts.find_by_identifier(self.layout.try(:identifier))
          }
          m
        when Cms::Snippet
          m = site.snippets.find_by_identifier(self.identifier_was || self.identifier) || site.snippets.new
          m.attributes = {
            :identifier => self.identifier
          }
          m
        end
        
        mirror.is_mirrored = true
        begin
          mirror.save!
        rescue ActiveRecord::RecordInvalid
          logger.detailed_error($!)
        end
      end
    end
1289407
def url_is_unique
      if container.is_a?(Page) && url.present?
        url_exists = Ninetails::Container.
          where.not(id: container.id).
          includes(:current_revision).
          where(ninetails_revisions: { url: url }).
          exists?

        errors.add :url, "is already in use" if url_exists
      end
    end
1248715
def create_or_update(cls, bucket, key, value):
        """"""
        obj = cls.get(bucket, key)
        if obj:
            obj.value = value
            db.session.merge(obj)
        else:
            obj = cls.create(bucket, key, value)
        return obj
133078
def insert!(mirror=@client)
      timeline_item = self
      result = []
      if file_upload?
        for file in file_to_upload
          media = Google::APIClient::UploadIO.new(file.contentUrl, file.content_type)
          result << client.execute!(
              :api_method => mirror.timeline.insert,
              :body_object => timeline_item,
              :media => media,
              :parameters => {
                  :uploadType => 'multipart',
                  :alt => 'json'})
        end
      else
        result << client.execute(
            :api_method => mirror.timeline.insert,
            :body_object => timeline_item)
      end
      return result.data
    end
700506
def merge_model_instances(self, primary_object, alias_objects):
        
        generic_fields = get_generic_fields()

        # get related fields
        related_fields = list(filter(
            lambda x: x.is_relation is True,
            primary_object._meta.get_fields()))

        many_to_many_fields = list(filter(
            lambda x: x.many_to_many is True, related_fields))

        related_fields = list(filter(
            lambda x: x.many_to_many is False, related_fields))

        # Loop through all alias objects and migrate their references to the
        # primary object
        deleted_objects = []
        deleted_objects_count = 0
        for alias_object in alias_objects:
            # Migrate all foreign key references from alias object to primary
            # object.
            for many_to_many_field in many_to_many_fields:
                alias_varname = many_to_many_field.name
                related_objects = getattr(alias_object, alias_varname)
                for obj in related_objects.all():
                    try:
                        # Handle regular M2M relationships.
                        getattr(alias_object, alias_varname).remove(obj)
                        getattr(primary_object, alias_varname).add(obj)
                    except AttributeError:
                        # Handle M2M relationships with a 'through' model.
                        # This does not delete the 'through model.
                        # TODO: Allow the user to delete a duplicate 'through' model.
                        through_model = getattr(alias_object, alias_varname).through
                        kwargs = {
                            many_to_many_field.m2m_reverse_field_name(): obj,
                            many_to_many_field.m2m_field_name(): alias_object,
                        }
                        through_model_instances = through_model.objects.filter(**kwargs)
                        for instance in through_model_instances:
                            # Re-attach the through model to the primary_object
                            setattr(
                                instance,
                                many_to_many_field.m2m_field_name(),
                                primary_object)
                            instance.save()
                            # TODO: Here, try to delete duplicate instances that are
                            # disallowed by a unique_together constraint

            for related_field in related_fields:
                if related_field.one_to_many:
                    alias_varname = related_field.get_accessor_name()
                    related_objects = getattr(alias_object, alias_varname)
                    for obj in related_objects.all():
                        field_name = related_field.field.name
                        setattr(obj, field_name, primary_object)
                        obj.save()
                elif related_field.one_to_one or related_field.many_to_one:
                    alias_varname = related_field.name
                    related_object = getattr(alias_object, alias_varname)
                    primary_related_object = getattr(primary_object, alias_varname)
                    if primary_related_object is None:
                        setattr(primary_object, alias_varname, related_object)
                        primary_object.save()
                    elif related_field.one_to_one:
                        self.stdout.write("Deleted {} with id {}\n".format(
                            related_object, related_object.id))
                        related_object.delete()

            for field in generic_fields:
                filter_kwargs = {}
                filter_kwargs[field.fk_field] = alias_object._get_pk_val()
                filter_kwargs[field.ct_field] = field.get_content_type(alias_object)
                related_objects = field.model.objects.filter(**filter_kwargs)
                for generic_related_object in related_objects:
                    setattr(generic_related_object, field.name, primary_object)
                    generic_related_object.save()

            if alias_object.id:
                deleted_objects += [alias_object]
                self.stdout.write("Deleted {} with id {}\n".format(
                    alias_object, alias_object.id))
                alias_object.delete()
                deleted_objects_count += 1

        return primary_object, deleted_objects, deleted_objects_count
1260844
def update_object(
        request, model=None, object_id=None, slug=None,
        slug_field='slug', template_name=None, template_loader=loader,
        extra_context=None, post_save_redirect=None, login_required=False,
        context_processors=None, template_object_name='object',
        form_class=None):
    
    if extra_context is None:
        extra_context = {}
    if login_required and not request.user.is_authenticated:
        return redirect_to_login(request.path)

    model, form_class = get_model_and_form_class(model, form_class)
    obj = lookup_object(model, object_id, slug, slug_field)

    if request.method == 'POST':
        form = form_class(request.POST, request.FILES, instance=obj)
        if form.is_valid():
            obj = form.save()
            msg = ugettext("The %(verbose_name)s was updated successfully.") %\
                {"verbose_name": model._meta.verbose_name}
            messages.success(request, msg, fail_silently=True)
            return redirect(post_save_redirect, obj)
    else:
        form = form_class(instance=obj)

    if not template_name:
        template_name = "%s/%s_form.html" % (
            model._meta.app_label, model._meta.object_name.lower())
    t = template_loader.get_template(template_name)
    c = {
        'form': form,
        template_object_name: obj,
    }
    apply_extra_context(extra_context, c)
    response = HttpResponse(t.render(context=c, request=request))
    return response
646241
def create(self, project, slug, content, **attrs):
        
        attrs.update({'project': project, 'slug': slug, 'content': content})
        return self._new_resource(payload=attrs)
704438
def update_slugs(apps, schema_editor):
    

    # Get model managers
    Representative = apps.get_model("representatives", "Representative")

    for rep in Representative.objects.all():
        rep.slug = '%s-%s' % (rep.slug, rep.birth_date)
        rep.save()
786924
def new(relation, new_options = EMPTY_HASH)
      self.class.new(relation, new_options.empty? ? options : options.merge(new_options))
    end
1131449
def contents
      RelationProxy.new(self, Content, [Operations::Create, Operations::Find]) do
        def create(mime_type, file, client = self.client)
          response = client.post(url, {}, {
            'Upload-Content-Type' => mime_type,
            'Upload-Content-Length' => file.size
          })

          attributes = response.merge('size' => file.size, 'mime_type' => mime_type)

          Content.from_response(attributes, client).tap do |content|
            content.upload(file)
          end
        end
      end
    end
377442
def mirror(self, axes='x'):
        
        polygon = np.array([[0,0], [0,1], [1,1]])
        space = Space(Place(polygon, polyhedra=self))
        return space.mirror(axes, inplace=False)[0].polyhedra[0]
802935
def update_or_create(self, *args, **kwargs):
        
        obj, created = super().update_or_create(*args, **kwargs)

        if not created:
            return self.with_signal(result=(obj, created))

        return obj, created
812310
def create_mirror(self, resource_name, target_name, mirror_type,
                      slave_resource_name, rpo=None, remote_rpo=None,
                      schedule=None, remote_schedule=None,
                      activate_mirror='no'):
        ''''''

        return self._create_mirror('cg', resource_name, target_name,
                                   mirror_type, slave_resource_name, rpo=rpo,
                                   remote_rpo=remote_rpo, schedule=schedule,
                                   remote_schedule=remote_schedule,
                                   activate_mirror=activate_mirror)
8073
function mirrorProperties(src, dest = {}) {
  function set(name) { dest[name] = src[name]; }
  function _delete(name) { delete dest[name]; }

  var handlers = { add: set, update: set, delete: _delete};

  assign(dest, src);
  observe(src, makeObserver(handlers));
  return dest;
}
1369617
def assure_relation(cls, cms_page):
        
        try:
            cms_page.cascadepage
        except cls.DoesNotExist:
            cls.objects.create(extended_object=cms_page)
475147
def replicate(source, model, cache=None):
    ''''''
    target = replicate_no_merge(source, model, cache=cache)
    if target is not None:
        db = object_session(source)
        target = db.merge(target)
    return target
42352
def validate_unique_slug(self):
        
        clashes_qs = type(self).objects.filter(slug=self.slug)
        if self.pk:
            clashes_qs = clashes_qs.exclude(pk=self.pk)
        if isinstance(self, PublishingModel):
            clashes_qs = clashes_qs.filter(
                publishing_is_draft=self.publishing_is_draft)
        if clashes_qs:
            raise ValidationError(
                "Slug '%s' clashes with other items: %s"
                % (self.slug, clashes_qs))