aboutsummaryrefslogtreecommitdiff
path: root/apps/utils/views.py
blob: c752706a5f7ff5f425e2a5790a5eb0f5bd090136 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from itertools import chain
import json
from django.http import Http404, HttpResponse, JsonResponse
from django.apps import apps
from django.views.generic import ListView
from django.views.generic.base import View, RedirectView
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
#from photos.models import LuxImage, LuxVideo
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.template import RequestContext
from taggit.models import Tag
#from dal import autocomplete


class PaginatedListView(ListView):
    """
    handles my own pagination system
    """
    context_object_name = 'object_list'

    def dispatch(self, request, *args, **kwargs):
        path = request.path.split('/')[1:-1]
        if path[-1] == self.kwargs['page']:
            path = "/".join(t for t in path[:-1])
            request.page_url = "/" + path + '/%d/'
        else:
            request.page_url = request.path + '%d/'
        print(request.page_url)
        request.page = int(self.kwargs['page'])
        request.base_path = path
        return super(PaginatedListView, self).dispatch(request, *args, **kwargs)


@method_decorator(login_required, name='dispatch')
class LoggedInViewWithUser(View):

    def get_form_kwargs(self, **kwargs):
        kwargs = super().get_form_kwargs(**kwargs)
        kwargs.update({'user': self.request.user})
        return kwargs



class AjaxableResponseMixin:
    """
    Mixin to add AJAX support to a form.
    Must be used with an object-based FormView (e.g. CreateView)
    """
    def form_invalid(self, form):
        response = super().form_invalid(form)
        if self.request.is_ajax():
            return JsonResponse(form.errors, status=400)
        else:
            return response

    def form_valid(self, form):
        # We make sure to call the parent's form_valid() method because
        # it might do some processing (in the case of CreateView, it will
        # call form.save() for example).
        print(self.request.META)
        print('x request header', self.request.META['HTTP_X_REQUESTED_WITH'])
        response = super().form_valid(form)
        if self.request.is_ajax():
            data = {
                'pk': self.object.pk,
            }
            return JsonResponse(data)
        else:
            return response
'''
class TagAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        if not self.request.user.is_authenticated:
            return Tag.objects.none()

        qs = Tag.objects.all()

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs


def nav_json(request, app, model, pk):
    model = apps.get_model(app_label=app, model_name=model)
    p = model.objects.get(pk=pk)
    data = {}
    data['prev'] = p.get_previous_admin_url
    data['next'] = p.get_next_admin_url
    data = json.dumps(data)
    return HttpResponse(data)


def insert_image(request):
    """
    The view that handles the admin insert image/video feature
    """
    images = LuxImage.objects.all()[:80]
    videos = LuxVideo.objects.all()[:10]
    object_list = sorted(
        chain(images, videos),
        key=lambda instance: instance.pub_date,
        reverse=True
    )
    return render(request, 'admin/insert_images.html', {'object_list': object_list, 'textarea_id': request.GET['textarea']})
'''