Example #1
0
class BookForm(forms.ModelForm):
    '''form for represent model.Book in admin change veiw'''
    class Meta:
        '''defines model for this form '''
        model = Book

    author = forms.CharField(widget=AuthorWidget(rel=ManyToManyRel(to=Author)),
                             label='Authors')  #TODO help_text
    language = forms.ModelChoiceField(
        queryset=Language.objects.all(),
        widget=LanguageWidget(choices=Language.objects.exclude(
            book__isnull=True)[1:]))
    credit = forms.IntegerField(widget=forms.RadioSelect(
        choices=CREDIT_CHOICES))

    annotation = forms.CharField(widget=AnnotationWidget(rel=ManyToManyRel(
        to=Annotation)))

    def save(self, commit=True):
        """
        Saves this form's data into model using save_book function
        Returns ''instance''.
        """
        if self.instance.pk is None:
            fail_message = 'created'
        else:
            fail_message = 'changed'
        return save_book(self,
                         self.instance,
                         self._meta.fields,
                         fail_message,
                         commit,
                         exclude=self._meta.exclude)
Example #2
0
 def __init__(self, *args, **kwargs):                         
   super(PlayerForm, self).__init__(*args, **kwargs)
   if self.instance.pk:
     #if this is not a new object, we load related games                                       
     self.initial['games'] = self.instance.games.values_list('pk', flat=True)
     rel = ManyToManyRel(Game)
     self.fields['games'].widget = RelatedFieldWidgetWrapper(self.fields['games'].widget, rel, admin.site)   
Example #3
0
    def __init__(
        self,
        verbose_name=_("Tags"),
        help_text=_("A comma-separated list of tags."),
        through=None,
        blank=False,
        related_name=None,
        to=None,
        ordering=None,
        manager=_TaggableManager,
    ):
        self.through = through or TaggedItem

        rel = ManyToManyRel(self,
                            to,
                            related_name=related_name,
                            through=self.through)

        super().__init__(
            verbose_name=verbose_name,
            help_text=help_text,
            blank=blank,
            null=True,
            serialize=False,
            rel=rel,
        )

        self.ordering = ordering
        self.swappable = False
        self.manager = manager
Example #4
0
 def __init__(self,
              to,
              db_constraint=True,
              swappable=True,
              related_name=None,
              related_query_name=None,
              limit_choices_to=None,
              symmetrical=None,
              through=None,
              through_fields=None,
              db_table=None,
              **kwargs):
     try:
         to._meta
     except AttributeError:
         to = str(to)
     kwargs['rel'] = ManyToManyRel(
         self,
         to,
         related_name=related_name,
         related_query_name=related_query_name,
         limit_choices_to=limit_choices_to,
         symmetrical=symmetrical if symmetrical is not None else
         (to == RECURSIVE_RELATIONSHIP_CONSTANT),
         through=through,
         through_fields=through_fields,
         db_constraint=db_constraint,
     )
     self.swappable = swappable
     self.db_table = db_table
     if kwargs['rel'].through is not None:
         assert self.db_table is None, "Cannot specify a db_table if an intermediary model is used."
     super().__init__(**kwargs)
Example #5
0
    def __init__(self, *args, **kwargs):
        super(SmsCampaignAdminForm, self).__init__(*args, **kwargs)

        rel = ManyToManyRel(Campaign, 'pk')
        self.base_fields['mailing_list'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
        self.base_fields['negative_mailing_list'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
        self.base_fields['campaign_type'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
Example #6
0
 def __init__(self, to, db_constraint=True, swappable=True, **kwargs):
     try:
         to._meta
     except AttributeError:
         to = str(to)
     kwargs['rel'] = ManyToManyRel(
         self, to,
         related_name=kwargs.pop('related_name', None),
         related_query_name=kwargs.pop('related_query_name', None),
         limit_choices_to=kwargs.pop('limit_choices_to', None),
         symmetrical=kwargs.pop('symmetrical', to == RECURSIVE_RELATIONSHIP_CONSTANT),
         through=kwargs.pop('through', None),
         through_fields=kwargs.pop('through_fields', None),
         db_constraint=db_constraint,
     )
     self.swappable = swappable
     self.db_table = kwargs.pop('db_table', None)
     if kwargs['rel'].through is not None:
         assert self.db_table is None, "Cannot specify a db_table if an intermediary model is used."
     super(CustomManyToManyField, self).__init__(**kwargs)
Example #7
0
    def __init__(self, *args, **kwargs):
        super(CampaignAdminForm, self).__init__(*args, **kwargs)

        rel = ManyToManyRel(Campaign, 'id')

        # FIXME: django docs:
        # The base_fields class attribute is the *class-wide* definition of
        # fields. Because a particular *instance* of the class might want to
        # alter self.fields, we create self.fields here by copying base_fields.
        # Instances should always modify self.fields; they should not modify
        # self.base_fields.
        # Don't think using base_fields here is necessary.
        self.base_fields['mailing_list'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
        self.base_fields['negative_mailing_list'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
        self.base_fields['campaign_type'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
        self.base_fields['previous_campaigns'].widget = admin.widgets.RelatedFieldWidgetWrapper(
            SelectMultiple(attrs={'size': '6'}), rel, admin.site,)
Example #8
0
    def __init__(self, to, field_name, **kwargs):
        is_migration = kwargs.pop('is_migration', False)
        kwargs['verbose_name'] = kwargs.get('verbose_name', None)
        m2m_rel_kwargs = {
            'related_name': None,
            'symmetrical': True,
            'limit_choices_to': kwargs.pop('limit_choices_to', None),
            'through': None,
        }

        if is_migration:
            m2m_rel_kwargs['through'] = None
            self.many_to_many = False
        else:
            m2m_rel_kwargs['through'] = FalseThrough()

        kwargs['rel'] = ManyToManyRel(self, to, **m2m_rel_kwargs)

        self.field_name = field_name

        kwargs['blank'] = True
        kwargs['editable'] = True
        kwargs['serialize'] = False
        Field.__init__(self, **kwargs)
Example #9
0
        def __get__(self, instance, instance_type=None):
            qn = connection.ops.quote_name
            manager = RelatedManager(
                model=User,
                core_filters={'related_to__pk': instance._get_pk_val()},
                instance=instance,
                symmetrical=False,
                join_table=qn('relationships_relationship'),
                source_col_name=qn('from_user_id'),
                target_col_name=qn('to_user_id'),
            )
            return manager

elif django.VERSION > (1, 2) and django.VERSION < (1, 4):

    fake_rel = ManyToManyRel(to=User, through=Relationship)

    RelatedManager = create_many_related_manager(RelationshipManager, fake_rel)

    class RelationshipsDescriptor(object):
        def __get__(self, instance, instance_type=None):
            manager = RelatedManager(
                model=User,
                core_filters={'related_to__pk': instance._get_pk_val()},
                instance=instance,
                symmetrical=False,
                source_field_name='from_user',
                target_field_name='to_user')
            return manager

else:
Example #10
0
        def __get__(self, instance, instance_type=None):
            qn = connection.ops.quote_name
            manager = RelatedManager(
                model=get_user_model(),
                core_filters={'related_to__pk': instance._get_pk_val()},
                instance=instance,
                symmetrical=False,
                join_table=qn('relationships_relationship'),
                source_col_name=qn('from_user_id'),
                target_col_name=qn('to_user_id'),
            )
            return manager

elif django.VERSION > (1, 2) and django.VERSION < (1, 4):

    fake_rel = ManyToManyRel(to=AUTH_USER_MODEL, through=Relationship)

    RelatedManager = create_many_related_manager(RelationshipManager, fake_rel)

    class RelationshipsDescriptor(object):
        def __get__(self, instance, instance_type=None):
            manager = RelatedManager(
                model=get_user_model(),
                core_filters={'related_to__pk': instance._get_pk_val()},
                instance=instance,
                symmetrical=False,
                source_field_name='from_user',
                target_field_name='to_user')
            return manager

else:
Example #11
0
            qn = connection.ops.quote_name
            manager = RelatedManager(
                model=User,
                core_filters={'related_to__pk': instance._get_pk_val()},
                instance=instance,
                symmetrical=False,
                join_table=qn('followers_followers'),
                source_col_name=qn('from_user_id'),
                target_col_name=qn('to_user_id'),
            )
            return manager

else:

    fake_rel = ManyToManyRel(
        to=User,
        through=Followers)

    RelatedManager = create_many_related_manager(FollowingManager, fake_rel)

    class FollowersDescriptor(object):
        def __get__(self, instance, instance_type=None):
            manager = RelatedManager(
                model=User,
                core_filters={'related_to__pk': instance._get_pk_val()},
                instance=instance,
                symmetrical=False,
                source_field_name='from_user',
                target_field_name='to_user'
            )
            return manager
Example #12
0
    def followers(self):
        return self.get_related_to(RelationshipStatus.objects.following())

    def blocking(self):
        return self.get_relationships(RelationshipStatus.objects.blocking())

    def blockers(self):
        return self.get_related_to(RelationshipStatus.objects.blocking())

    def friends(self):
        return self.get_relationships(RelationshipStatus.objects.following(), True)


fake_rel = ManyToManyRel(
    field=None,
    to=User,
    through=Relationship
)

from relationships.compat import create_many_related_manager

RelatedManager = create_many_related_manager(RelationshipManager, fake_rel)


class RelationshipsDescriptor(object):
    def __get__(self, instance, instance_type=None):
        manager = RelatedManager(
            model=User,
            query_field_name='related_to',
            instance=instance,
            symmetrical=False,