diff options
Diffstat (limited to 'ishtar_common')
| -rw-r--r-- | ishtar_common/forms_common.py | 2 | ||||
| -rw-r--r-- | ishtar_common/models.py | 31 | ||||
| -rw-r--r-- | ishtar_common/views_item.py | 24 | 
3 files changed, 49 insertions, 8 deletions
| diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 4cc0ca024..e5246d9bb 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -1176,7 +1176,7 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):          for rel in models.Document.RELATED_MODELS:              if cleaned_data.get(rel, None):                  return cleaned_data -        raise forms.ValidationError(_(u"A document have to attached at least " +        raise forms.ValidationError(_(u"A document has to be attached at least "                                        u"to one item"))      def save(self, commit=True): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 1a0d80ac3..219b6b266 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3432,7 +3432,7 @@ class IshtarUser(FullSearch):          return self.person.full_label() -class Basket(FullSearch): +class Basket(FullSearch, OwnPerms):      """      Abstract class for a basket      Subclass must be defined with an "items" ManyToManyField @@ -3486,6 +3486,35 @@ class Basket(FullSearch):          return "{}-{}".format(datetime.date.today().strftime(              "%Y-%m-%d"), slugify(self.label)) +    @classmethod +    def get_query_owns(cls, ishtaruser): +        return Q(user=ishtaruser) | Q(shared_with=ishtaruser) | Q( +            shared_write_with=ishtaruser) + +    @classmethod +    def get_write_query_owns(cls, ishtaruser): +        return Q(user=ishtaruser) + +    def duplicate(self, ishtaruser=None): +        """ +        Duplicate the basket. Items in basket are copied but not shared users +        :param ishtaruser: if provided an alternate user is used +        :return: the new basket +        """ +        items = list(self.items.all()) +        new_item = self +        new_item.pk = None +        if ishtaruser: +            new_item.user = ishtaruser +        label = new_item.label +        while self.__class__.objects.filter( +                label=label, user=new_item.user).count(): +            label += unicode(_(u" - duplicate")) +        new_item.save() +        for item in items: +            new_item.add(item) +        return new_item +  class AuthorType(GeneralType):      order = models.IntegerField(_(u"Order"), default=1) diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index f34b2357f..2d8b6b828 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -65,17 +65,29 @@ CURRENT_ITEM_KEYS = (  CURRENT_ITEM_KEYS_DICT = dict(CURRENT_ITEM_KEYS) +def get_autocomplete_query(request, label_attributes, extra=None): +    q = request.GET.get('term') or "" +    if not label_attributes: +        return Q(pk__isnull=True) +    query = Q() +    if extra: +        query = Q(**extra) +    for q in q.split(' '): +        if not q: +            continue +        sub_q = Q(**{label_attributes[0] + "__icontains": q}) +        for other_label in label_attributes[1:]: +            sub_q = sub_q | Q(**{other_label + "__icontains": q}) +        query = query & sub_q +    return query + +  def get_autocomplete_item(model, extra=None):      if not extra:          extra = {}      def func(request, current_right=None): -        q = request.GET.get('term') or "" -        query = Q(**extra) -        for q in q.split(' '): -            if not q: -                continue -            query = query & Q(cached_label__icontains=q) +        query = get_autocomplete_query(request, ['cached_label'], extra=extra)          limit = 20          objects = model.objects.filter(query)[:limit]          data = json.dumps([{'id': obj.pk, 'value': obj.cached_label} | 
