diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-11-30 18:47:14 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-11-30 18:48:21 +0100 |
commit | eddd946150bcd9d4ddb05000806cc2cdfb9af60d (patch) | |
tree | e091e17882ca67be8e6040e938439fa5a5e6bc5a /ishtar_common | |
parent | afd7fb9c2be01a44a45f582eebdf02632a10be99 (diff) | |
download | Ishtar-eddd946150bcd9d4ddb05000806cc2cdfb9af60d.tar.bz2 Ishtar-eddd946150bcd9d4ddb05000806cc2cdfb9af60d.zip |
Manage basket to treatment file association
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} |