lookups.py 2.93 KB
Newer Older
Skia's avatar
Skia committed
1
from django.core.exceptions import PermissionDenied
2
3
4
from ajax_select import register, LookupChannel

from core.views.site import search_user
Skia's avatar
Skia committed
5
from core.models import User, Group, SithFile
6
from club.models import Club
7
from counter.models import Product, Counter
Skia's avatar
Skia committed
8
from accounting.models import ClubAccount, Company
9

Sli's avatar
Sli committed
10
def check_token(request):
Sli's avatar
Sli committed
11
12
13
14
    return ('counter_token' in request.session.keys() and
        request.session['counter_token'] and
        Counter.objects.filter(token=request.session['counter_token']).exists())

Skia's avatar
Skia committed
15
16
class RightManagedLookupChannel(LookupChannel):
    def check_auth(self, request):
Sli's avatar
Sli committed
17
        if not request.user.subscribed and not check_token(request):
Skia's avatar
Skia committed
18
19
            raise PermissionDenied

20
@register('users')
Skia's avatar
Skia committed
21
class UsersLookup(RightManagedLookupChannel):
22
23
24
25
26
27
28
29
30
31
32
    model = User

    def get_query(self, q, request):
        return search_user(q)

    def format_match(self, obj):
        return obj.get_mini_item()

    def format_item_display(self, item):
        return item.get_display_name()

33
@register('groups')
Skia's avatar
Skia committed
34
class GroupsLookup(RightManagedLookupChannel):
35
36
37
38
39
40
41
42
43
44
45
46
    model = Group

    def get_query(self, q, request):
        return self.model.objects.filter(name__icontains=q)[:50]

    def format_match(self, obj):
        return obj.name

    def format_item_display(self, item):
        return item.name

@register('clubs')
Skia's avatar
Skia committed
47
class ClubLookup(RightManagedLookupChannel):
48
49
50
51
52
53
54
55
56
57
58
    model = Club

    def get_query(self, q, request):
        return self.model.objects.filter(name__icontains=q)[:50]

    def format_match(self, obj):
        return obj.name

    def format_item_display(self, item):
        return item.name

59
@register('counters')
Skia's avatar
Skia committed
60
class CountersLookup(RightManagedLookupChannel):
61
62
63
64
65
66
67
68
69
    model = Counter

    def get_query(self, q, request):
        return self.model.objects.filter(name__icontains=q)[:50]

    def format_item_display(self, item):
        return item.name

@register('products')
Skia's avatar
Skia committed
70
class ProductsLookup(RightManagedLookupChannel):
71
72
73
    model = Product

    def get_query(self, q, request):
Skia's avatar
Skia committed
74
75
        return (self.model.objects.filter(name__icontains=q) |
                self.model.objects.filter(code__icontains=q)).filter(archived=False)[:50]
76
77

    def format_item_display(self, item):
Skia's avatar
Skia committed
78
        return "%s (%s)" % (item.name, item.code)
Skia's avatar
Skia committed
79

Skia's avatar
Skia committed
80
81
82
83
84
85
86
@register('files')
class SithFileLookup(RightManagedLookupChannel):
    model = SithFile

    def get_query(self, q, request):
        return self.model.objects.filter(name__icontains=q)[:50]

Skia's avatar
Skia committed
87
@register('club_accounts')
Skia's avatar
Skia committed
88
class ClubAccountLookup(RightManagedLookupChannel):
Skia's avatar
Skia committed
89
90
91
92
93
94
95
96
97
    model = ClubAccount

    def get_query(self, q, request):
        return self.model.objects.filter(name__icontains=q)[:50]

    def format_item_display(self, item):
        return item.name

@register('companies')
Skia's avatar
Skia committed
98
class CompaniesLookup(RightManagedLookupChannel):
Skia's avatar
Skia committed
99
100
101
102
103
104
105
    model = Company

    def get_query(self, q, request):
        return self.model.objects.filter(name__icontains=q)[:50]

    def format_item_display(self, item):
        return item.name