Esempio n. 1
0
 def process_row(self,ar,obj,attrs):
     oldobj = obj
     watcher = dd.ChangeWatcher(obj)
     diffs = []
     for fldname,new in attrs.items():
         fld = get_field(self.client_model,fldname)
         old = getattr(obj,fldname)
         if old != new:
             diffs.append("%s : %s -> %s" % (unicode(fld.verbose_name),dd.obj2str(old),dd.obj2str(new)))
             setattr(obj,fld.name,new)
             
     if len(diffs) == 0:
         #~ return self.no_diffs_response(ar,obj)
         return self.goto_client_response(ar,obj,_("Client %s is up-to-date") % unicode(obj))
         
     msg = unicode(_("Click OK to apply the following changes for %s") % obj)
     msg += ' :<br/>'
     msg += '\n<br/>'.join(diffs)
     def yes(ar):
         obj.full_clean()
         obj.save()
         watcher.send_update(ar.request)
         #~ return self.saved_diffs_response(ar,obj)
         return self.goto_client_response(ar,obj,_("%s has been saved.") % dd.obj2unicode(obj))
     def no(ar):
         return self.goto_client_response(ar,oldobj)
     #~ print 20131108, msg
     cb = ar.add_callback(msg)
     cb.add_choice('yes',yes,_("Yes"))
     cb.add_choice('no',no,_("No"))
     ar.set_callback(cb)
Esempio n. 2
0
    def get_beid_diffs(obj, attrs):
        """Return two lists, one with the objects to save, and another with
        text lines to build a confirmation message explaining which
        changes are going to be applied after confirmation.

        The default implemantion is for the simple case where the
        holder is also a contacts.AddressLocation and the address is
        within the same database row.

        """
        raise Exception("not tested")
        diffs = []
        objects = []
        # model = holder_model()
        model = obj.__class__  # the holder
        for fldname, new in attrs.items():
            fld = get_field(model, fldname)
            old = getattr(obj, fldname)
            if old != new:
                diffs.append(
                    "%s : %s -> %s" % (
                        unicode(fld.verbose_name), dd.obj2str(old),
                        dd.obj2str(new)))
                setattr(obj, fld.name, new)
        return objects, diffs
Esempio n. 3
0
def add_gridfilters(qs, gridfilters):
    """
    Converts a `filter` request in the format used by :extux:`Ext.ux.grid.GridFilters` into a
    `Django field lookup <http://docs.djangoproject.com/en/1.2/ref/models/querysets/#field-lookups>`_
    on a :class:`django.db.models.query.QuerySet`.

    :param qs: the queryset to be modified.
    :param gridfilters: a list of dictionaries, each having 3 keys `field`, `type` and `value`.

    """
    if not isinstance(qs, QuerySet):
        raise NotImplementedError('TODO: filter also simple lists')
    q = models.Q()
    for flt in gridfilters:
        field = get_field(qs.model, flt['field'])
        flttype = flt['type']
        kw = {}
        if flttype == 'string':
            if isinstance(field, models.CharField):
                kw[field.name + "__icontains"] = flt['value']
                q = q & models.Q(**kw)
            elif isinstance(field, models.ForeignKey):
                q = q & quick_search_filter(
                    field.rel.to, flt['value'], field.name + "__")
                #~ rq = models.Q()
                #~ search_field = field.rel.to.grid_search_field
                #~ for search_field in field.rel.to.quick_search_fields:
                #~ search_field = getattr(field.rel.to,'grid_search_field',None)
                #~ if search_field is not None:
                    #~ rq = rq | models.Q(**{field.name+"__%s__icontains" % search_field : flt['value']})
                #~ q = q & rq
            else:
                raise NotImplementedError(repr(flt))
        elif flttype == 'numeric':
            cmp = str(flt['comparison'])
            if cmp == 'eq':
                cmp = 'exact'
            kw[field.name + "__" + cmp] = flt['value']
            q = q & models.Q(**kw)
        elif flttype == 'boolean':
            kw[field.name + "__equals"] = flt['value']
            q = q & models.Q(**kw)
        elif flttype == 'date':
            v = datetime.date(*settings.SITE.parse_date(flt['value']))
            #~ v = parse_js_date(flt['value'],field.name)
            cmp = str(flt['comparison'])
            if cmp == 'eq':
                cmp = 'exact'
            kw[field.name + "__" + cmp] = v
            q = q & models.Q(**kw)
            #~ print kw
        else:
            raise NotImplementedError(repr(flt))
    return qs.filter(q)