Example #1
0
    def update_problems(self, obj, delete=True, fix=False):
        """Update the problems of this checker and the specified object.

        When `delete` is False, the caller is responsible for deleting
        any existing objects.

        """
        Problem = rt.modules.plausibility.Problem
        if delete:
            gfk = Problem.owner
            qs = Problem.objects.filter(**gfk2lookup(gfk, obj, checker=self))
            qs.delete()

        done = []
        todo = []
        for fixable, msg in self.get_plausibility_problems(obj, fix):
            if fixable:
                msg = u"(\u2605) " + unicode(msg)
            if fixable and fix:
                done.append(msg)
            else:
                todo.append(msg)
        if len(todo):
            user = self.get_responsible_user(obj)
            if user is None:
                lang = dd.get_default_language()
            else:
                lang = user.language
            with translation.override(lang):
                msg = '\n'.join([unicode(s) for s in todo])
            prb = Problem(owner=obj, message=msg, checker=self, user=user)
            prb.full_clean()
            prb.save()
        return (todo, done)
Example #2
0
    def update_problems(self, obj, delete=True, fix=False):
        """Update the problems of this checker and the specified object.

        When `delete` is False, the caller is responsible for deleting
        any existing objects.

        """
        Problem = rt.modules.plausibility.Problem
        if delete:
            gfk = Problem.owner
            qs = Problem.objects.filter(**gfk2lookup(gfk, obj, checker=self))
            qs.delete()

        done = []
        todo = []
        for fixable, msg in self.get_plausibility_problems(obj, fix):
            if fixable:
                msg = u"(\u2605) " + unicode(msg)
            if fixable and fix:
                done.append(msg)
            else:
                todo.append(msg)
        if len(todo):
            user = self.get_responsible_user(obj)
            if user is None:
                lang = dd.get_default_language()
            else:
                lang = user.language
            with translation.override(lang):
                msg = '\n'.join([unicode(s) for s in todo])
            prb = Problem(owner=obj, message=msg, checker=self, user=user)
            prb.full_clean()
            prb.save()
        return (todo, done)
Example #3
0
 def get_request_queryset(cls, ar):
     mi = ar.master_instance
     if mi is None:
         return cls.model.objects.null()
     return cls.model.objects.filter(time__gte=mi.created,
                                     **gfk2lookup(
                                         cls.model.master, mi.owner))
Example #4
0
 def get_request_queryset(cls, ar):
     mi = ar.master_instance
     if mi is None:
         return cls.model.objects.null()
     return cls.model.objects.filter(
         time__gte=mi.created,
         **gfk2lookup(cls.model.master, mi.owner))
Example #5
0
 def run_from_ui(self, ar, fix=None):
     if fix is None:
         fix = self.fix_them
     Problem = rt.modules.plausibility.Problem
     gfk = Problem.owner
     checkers = get_checkable_models()[self.model]
     for obj in ar.selected_rows:
         assert isinstance(obj, self.model)
         qs = Problem.objects.filter(**gfk2lookup(gfk, obj))
         qs.delete()
         for chk in checkers:
             chk.update_problems(obj, False, fix)
     ar.set_response(refresh=True)
Example #6
0
 def run_from_ui(self, ar, fix=None):
     if fix is None:
         fix = self.fix_them
     Problem = rt.modules.plausibility.Problem
     gfk = Problem.owner
     checkers = get_checkable_models()[self.model]
     for obj in ar.selected_rows:
         assert isinstance(obj, self.model)
         qs = Problem.objects.filter(**gfk2lookup(gfk, obj))
         qs.delete()
         for chk in checkers:
             chk.update_problems(obj, False, fix)
     ar.set_response(refresh=True)
Example #7
0
    def notify(cls, ar, owner, user, message):
        """Create a notification unless that user has already been notified
        about that object.


        """
        fltkw = gfk2lookup(cls.owner, owner)
        qs = cls.objects.filter(user=user, seen__isnull=True, **fltkw)
        if not qs.exists():
            # create a notification object and send email
            obj = cls(user=user, owner=owner, message=message)
            obj.full_clean()
            obj.save()
            obj.send_email(ar)
Example #8
0
    def get_filter_kw(self, ar, **kw):
        """
        Return a dict with the "master keywords" for this table
        and a given `master_instance`.

        :class:`lino.modlib.tickets.models.EntriesBySession`
        Blog Entries are not directly linked to a Session, but in the
        Detail of a Session we want to display a table of related blog
        entries.

        :class:`lino.modlib.households.models.SiblingsByPerson`
        Household members are not directly linked to a Person, but
        usually a Person is member of exactly one household, and in
        the Detail of a Person we want to display the members of that
        household.

        """
        master_instance = ar.master_instance
        if self.master is None:
            pass
            # master_instance may be e.g. a lino.core.actions.EmptyTableRow
            # UsersWithClients as "slave" of the "table" Home
        elif self.master is models.Model:
            pass
        elif isinstance(self.master_field, GenericForeignKey):
            kw = gfk2lookup(self.master_field, master_instance, **kw)
        elif self.master_field is not None:
            if master_instance is None:
                if not self.master_field.null:
                    #~ logger.info('20120519 %s.get_filter_kw()--> None',self)
                    return  # cannot add rows to this table
            else:
                master_instance = master_instance.get_typed_instance(
                    self.master)
                if not isinstance(master_instance, self.master):
                    # e.g. a ByUser table descendant called by AnonymousUser
                    msg = "%r is not a %s (%s.master_key = '%s')" % (
                        master_instance.__class__,
                        self.master, self,
                        self.master_key)
                    logger.warning(msg)
                    # raise Exception(msg)
                    # raise PermissionDenied(msg)
                    # master_instance = None
                    return  # cannot add rows to this table
            kw[self.master_field.name] = master_instance
        # else:
        #     msg = "20150322 Cannot handle master {0}".format(master_instance)
        #     raise Exception(msg)
        return kw
Example #9
0
    def get_filter_kw(self, ar, **kw):
        """
        Return a dict with the "master keywords" for this table
        and a given `master_instance`.

        :class:`lino.modlib.tickets.models.EntriesBySession`
        Blog Entries are not directly linked to a Session, but in the
        Detail of a Session we want to display a table of related blog
        entries.

        :class:`lino.modlib.households.models.SiblingsByPerson`
        Household members are not directly linked to a Person, but
        usually a Person is member of exactly one household, and in
        the Detail of a Person we want to display the members of that
        household.

        """
        master_instance = ar.master_instance
        if self.master is None:
            pass
            # master_instance may be e.g. a lino.core.actions.EmptyTableRow
            # UsersWithClients as "slave" of the "table" Home
        elif self.master is models.Model:
            pass
        elif isinstance(self.master_field, GenericForeignKey):
            kw = gfk2lookup(self.master_field, master_instance, **kw)
        elif self.master_field is not None:
            if master_instance is None:
                if not self.master_field.null:
                    #~ logger.info('20120519 %s.get_filter_kw()--> None',self)
                    return  # cannot add rows to this table
            else:
                master_instance = master_instance.get_typed_instance(
                    self.master)
                if not isinstance(master_instance, self.master):
                    # e.g. a ByUser table descendant called by AnonymousUser
                    msg = "%r is not a %s (%s.master_key = '%s')" % (
                        master_instance.__class__, self.master, self,
                        self.master_key)
                    logger.warning(msg)
                    # raise Exception(msg)
                    # raise PermissionDenied(msg)
                    # master_instance = None
                    return  # cannot add rows to this table
            kw[self.master_field.name] = master_instance
        # else:
        #     msg = "20150322 Cannot handle master {0}".format(master_instance)
        #     raise Exception(msg)
        return kw
Example #10
0
    def notify(cls, ar, owner, user, message):
        """Create a notification unless that user has already been notified
        about that object.


        """
        fltkw = gfk2lookup(cls.owner, owner)
        qs = cls.objects.filter(
            user=user, seen__isnull=True, **fltkw)
        if not qs.exists():
            # create a notification object and send email
            obj = cls(user=user, owner=owner, message=message)
            obj.full_clean()
            obj.save()
            obj.send_email(ar)
Example #11
0
 def run_from_ui(self, ar, fix=None):
     if fix is None:
         fix = self.fix_them
     Problem = rt.modules.plausibility.Problem
     # print(20150327, ar.selected_rows)
     for obj in ar.selected_rows:
         assert isinstance(obj, Problem)
         chk = obj.checker
         owner = obj.owner
         # not tested: what happens if the following deletes
         # another obj from selected_rows?
         qs = Problem.objects.filter(
             **gfk2lookup(Problem.owner, owner, checker=chk))
         qs.delete()
         chk.update_problems(owner, False, fix)
     ar.set_response(refresh_all=True)
Example #12
0
 def run_from_ui(self, ar, fix=None):
     if fix is None:
         fix = self.fix_them
     Problem = rt.modules.plausibility.Problem
     # print(20150327, ar.selected_rows)
     for obj in ar.selected_rows:
         assert isinstance(obj, Problem)
         chk = obj.checker
         owner = obj.owner
         # not tested: what happens if the following deletes
         # another obj from selected_rows?
         qs = Problem.objects.filter(
             **gfk2lookup(Problem.owner, owner, checker=chk))
         qs.delete()
         chk.update_problems(owner, False, fix)
     ar.set_response(refresh_all=True)
Example #13
0
    def for_obj(cls, obj, **kwargs):
        """Return a queryset of :class:`Star` instances for the given database
        object.

        """
        return cls.objects.filter(**gfk2lookup(cls.owner, obj, **kwargs))
Example #14
0
 def assert_check(obj, expected):
     qs = Problem.objects.filter(**gfk2lookup(Problem.owner, obj))
     got = '\n'.join([p.message for p in qs])
     self.assertEqual(got, expected)