예제 #1
0
 def test_current_user_field(self):
     user = User.objects.get(username="******")
     CuserMiddleware.set_user(user)
     TestModel.objects.create(name="TEST")
     CuserMiddleware.del_user()
     test_instance = TestModel.objects.get(name="TEST")
     self.assertEqual(test_instance.creator, user)
예제 #2
0
파일: tests.py 프로젝트: VoNov/django-cuser
 def test_current_user_field(self):
     user = User.objects.get(username="******")
     CuserMiddleware.set_user(user)
     TestModel.objects.create(name="TEST")
     CuserMiddleware.del_user()
     test_instance = TestModel.objects.get(name="TEST")
     self.assertEqual(test_instance.creator, user)
예제 #3
0
    def get_queryset(self, *args, **kwargs):
        self.app = app_from_object(self)
        self.model = class_from_string(self, self.kwargs['model'])

        # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
        CuserMiddleware.set_user(self.request.user)

        # If the URL has GET parameters (following a ?) then self.request.GET
        # will contain a dictionary of name: value pairs that FilterSet uses
        # construct a new filtered queryset.
        self.filterset = None
        self.format = get_list_display_format(self.request.GET)
        self.ordering = get_ordering(self)

        self.queryset = self.model.objects.all()
        if len(self.request.GET) > 0 or len(
                self.request.session.get("filter", {})) > 0:
            fs = get_filterset(self)

            # If there is a filter specified in the URL
            if not fs is None:
                self.filterset = fs
                self.queryset = fs.filter()

        if (self.ordering):
            self.queryset = self.queryset.order_by(*self.ordering)

        self.count = len(self.queryset)

        return self.queryset
예제 #4
0
 def setUpTestData(cls):
     cls.user = User.objects.create_user(
         username="******", email="", password="******"
     )
     cls.user_repr = repr(cls.user)
     CuserMiddleware.set_user(cls.user)
     cls.pet = Pet.objects.create(name="Catz", age=12)
     cls.human = Human.objects.create(name="George", age=42, height=175)
     cls.human_repr = repr(cls.human)
예제 #5
0
 def setUpTestData(cls):
     cls.user = User.objects.create_user(
         username="******", email="", password="******"
     )
     cls.user_repr = repr(cls.user)
     CuserMiddleware.set_user(cls.user)
     cls.pet = Pet.objects.create(name="Catz", age=12)
     cls.human = Human.objects.create(name="George", age=42, height=175)
     cls.human_repr = repr(cls.human)
예제 #6
0
 def setUp(self):
     self.user = User.objects.create_user(
         username="******", email="", password="******"
     )
     self.user_repr = repr(self.user)
     CuserMiddleware.set_user(self.user)
     self.pet = Pet.objects.create(name="Catz", age=12)
     self.pet2 = Pet.objects.create(name="Catzou", age=1)
     self.human = Human.objects.create(name="George", age=42, height=175)
     self.human.pets.add(self.pet)
     self.human_repr = repr(self.human)
예제 #7
0
 def setUp(self):
     self.user = User.objects.create_user(
         username="******", email="", password="******"
     )
     self.user_repr = repr(self.user)
     CuserMiddleware.set_user(self.user)
     self.pet = Pet.objects.create(name="Catz", age=12)
     self.pet2 = Pet.objects.create(name="Catzou", age=1)
     self.human = Human.objects.create(name="George", age=42, height=175)
     self.human.pets.add(self.pet)
     self.human_repr = repr(self.human)
예제 #8
0
파일: views.py 프로젝트: dreggs76/CoGs
    def get_queryset(self, *args, **kwargs):
        if settings.DEBUG:
            log.debug(
                f"Getting Queryset for List View. Process ID: {os.getpid()}.")
            if len(self.request.GET) > 0:
                log.debug(f"GET parameters:")
                for key, val in self.request.GET.items():
                    log.debug(f"\t{key}={val}")
            else:
                log.debug(f"No GET parameters!")

        self.app = app_from_object(self)
        self.model = class_from_string(self, self.kwargs['model'])

        # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
        CuserMiddleware.set_user(self.request.user)

        self.format = get_list_display_format(self.request.GET)

        self.ordering = get_ordering(self)

        self.filterset = None  # Default

        fs = None
        if len(self.request.GET) > 0 or len(
                self.request.session.get("filter", {})) > 0:
            # If the URL has GET parameters (following a ?) then self.request.GET
            # will contain a dictionary of name: value pairs that FilterSet uses
            # construct a new filtered queryset.
            fs = get_filterset(self)

        # If there is a filter specified in the URL
        if fs:
            self.filterset = fs
            self.queryset = fs.filter()
        else:
            self.queryset = self.model.objects.all()

        if (self.ordering):
            self.queryset = self.queryset.order_by(*self.ordering)

        if settings.DEBUG:
            log.debug(f"ordering  = {self.ordering}")
            log.debug(
                f"filterset = {self.filterset.get_specs() if self.filterset else None}"
            )

        self.count = len(self.queryset)

        return self.queryset
예제 #9
0
    def get_object(self, *args, **kwargs):
        self.app = app_from_object(self)
        self.model = class_from_string(self, self.kwargs['model'])

        # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
        CuserMiddleware.set_user(self.request.user)

        self.pk = self.kwargs['pk']
        self.obj = get_object_or_404(self.model, pk=self.kwargs['pk'])
        self.format = get_object_display_format(self.request.GET)
        self.success_url = reverse_lazy('list',
                                        kwargs={'model': self.kwargs['model']})

        collect_rich_object_fields(self)

        return self.obj
예제 #10
0
    def get_object(self, *args, **kwargs):
        self.model = class_from_string(self, self.kwargs['model'])
        self.pk = self.kwargs['pk']

        # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
        CuserMiddleware.set_user(self.request.user)

        # Get the ordering
        self.ordering = get_ordering(self)

        # Get Neighbour info for the object browser
        self.filterset = get_filterset(self)

        neighbours = get_neighbour_pks(self.model,
                                       self.pk,
                                       filterset=self.filterset,
                                       ordering=self.ordering)

        # Support for incoming next/prior requests via a GET
        if 'next' in self.request.GET or 'prior' in self.request.GET:
            self.ref = get_object_or_404(self.model, pk=self.pk)

            # If requesting the next or prior object look for that
            # FIXME: Totally fails for Ranks, the get dictionary fails when there are ties!
            #        Doesn't generalise well at all. Must find a general way to do this for
            #        arbitrary orders. Still should specify orders in models that create unique
            #        ordering not reliant on pk break ties.
            if neighbours:
                if 'next' in self.request.GET and not neighbours[1] is None:
                    self.pk = self.object_browser[1]
                elif 'prior' in self.request.GET and not neighbours[0] is None:
                    self.pk = self.object_browser[0]

            self.obj = get_object_or_404(self.model, pk=self.pk)
            self.kwargs["pk"] = self.pk
        else:
            self.obj = get_object_or_404(self.model, pk=self.pk)

        # Add this information to the view (so it's available in the context).
        self.object_browser = neighbours

        self.format = get_object_display_format(self.request.GET)

        collect_rich_object_fields(self)

        return self.obj
예제 #11
0
    def get_object(self, *args, **kwargs):
        '''Fetches the object to edit and augments the standard queryset by passing the model to the view so it can make model based decisions and access model attributes.'''
        self.pk = self.kwargs['pk']
        self.model = class_from_string(self, self.kwargs['model'])
        self.obj = get_object_or_404(self.model, pk=self.kwargs['pk'])

        if not hasattr(self, 'fields') or self.fields == None:
            self.fields = '__all__'

        if callable(getattr(self.obj, 'fields_for_model', None)):
            self.fields = self.obj.fields_for_model()
        else:
            self.fields = fields_for_model(self.model)

        self.success_url = reverse_lazy('view', kwargs=self.kwargs)

        # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
        CuserMiddleware.set_user(self.request.user)

        return self.obj
예제 #12
0
파일: views.py 프로젝트: dreggs76/CoGs
def get_context_data_generic(self, *args, **kwargs):
    '''
    Augments the standard context with model and related model information
    so that the template in well informed - and can do Javascript wizardry
    based on this information

    :param self: and instance of CreateView or UpdateView

    This is code shared by the two views so peeled out into a generic.
    '''
    if settings.DEBUG:
        log.debug("Preparing context data.")

    # We need to set self.model here
    self.app = app_from_object(self)
    self.model = class_from_string(self, self.kwargs['model'])
    if not hasattr(self, 'fields') or self.fields == None:
        self.fields = '__all__'

    # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
    CuserMiddleware.set_user(self.request.user)

    if isinstance(self, CreateView):
        # Note that the super.get_context_data initialises the form with get_initial
        context = super(CreateView, self).get_context_data(*args, **kwargs)
    elif isinstance(self, UpdateView):
        # Note that the super.get_context_data initialises the form with get_object
        context = super(UpdateView, self).get_context_data(*args, **kwargs)
    else:
        raise NotImplementedError(
            "Generic get_context_data only for use by CreateView or UpdateView derivatives."
        )

    # Now add some context extensions ....
    add_model_context(self, context, plural=False, title='New')
    add_timezone_context(self, context)
    add_debug_context(self, context)
    if callable(getattr(self, 'extra_context_provider', None)):
        context.update(self.extra_context_provider(context))

    return context
예제 #13
0
파일: views.py 프로젝트: plugger22/CoGs
    def get_context_data(self, *args, **kwargs):
        '''Augments the standard context with model and related model information so that the template in well informed - and can do Javascript wizardry based on this information'''

        # We need to set self.model here
        self.app = app_from_object(self)
        self.model = class_from_string(self, self.kwargs['model'])
        if not hasattr(self, 'fields') or self.fields == None:
            self.fields = '__all__'

        # Communicate the request user to the models (Django doesn't make this easy, need cuser middleware)
        CuserMiddleware.set_user(self.request.user)

        # Note that the super.get_context_data initialises the form with get_initial
        context = super().get_context_data(*args, **kwargs)

        # Now add some context extensions ....
        add_model_context(self, context, plural=False, title='New')
        add_timezone_context(self, context)
        add_debug_context(self, context)
        if callable(getattr(self, 'extra_context_provider', None)):
            context.update(self.extra_context_provider())
        return context
예제 #14
0
 def setUpTestData(cls):
     cls.user = User.objects.create_superuser("admin", "", "password")
     cls.c = Client()
     cls.c.login(username="******", password="******")
     CuserMiddleware.set_user(cls.user)
     cls.human = Human.objects.create(name="George", age=42, height=175)
예제 #15
0
 def setUp(self):
     self.user = User.objects.create_superuser('admin', '', 'password')
     self.c = Client()
     self.c.login(username="******", password="******")
     CuserMiddleware.set_user(self.user)
     self.human = Human.objects.create(name="George", age=42, height=175)