summaryrefslogtreecommitdiff
path: root/lib/grappelli/views/related.py
blob: 81622ab15f27afcbfcd14686e9973ebe54fe7c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# coding: utf-8

from django.http import HttpResponse
from django.db import models

def related_lookup(request):
    
    if request.method == 'GET':
        if request.GET.has_key('object_id') and request.GET.has_key('app_label') and request.GET.has_key('model_name'):
            object_id = request.GET.get('object_id')
            app_label = request.GET.get('app_label')
            model_name = request.GET.get('model_name')
            if object_id:
                try:
                    model = models.get_model(app_label, model_name)
                    obj = model.objects.get(pk=object_id)
                    obj_text = unicode(obj)
                except:
                    obj_text = "Not Found"
            else:
                obj_text = ""
        else:
            obj_text = "Error"
    else:
        obj_text = "Error"
    
    return HttpResponse(obj_text, mimetype='text/plain; charset=utf-8')
    

def m2m_lookup(request):
    obj_text = []
    if request.method == 'GET':
        if request.GET.has_key('object_id') and request.GET.has_key('app_label') and request.GET.has_key('model_name'):
            object_ids = request.GET.get('object_id').split(',')
            app_label = request.GET.get('app_label')
            model_name = request.GET.get('model_name')
            
            for obj_id in object_ids:
                try:
                    model = models.get_model(app_label, model_name)
                    obj = model.objects.get(pk=obj_id)
                    obj_text.append(unicode(obj))
                except:
                    obj_text.append("Not Found")
        else:
            obj_text.append("Error")
    else:
        obj_text.append("Error")
    obj_text = ", ".join(obj_text)
    
    return HttpResponse(obj_text, mimetype='text/plain; charset=utf-8')