SparkleView.get_context_data()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
import logging
22
23
from django.views.generic import ListView
24
from django.http.response import Http404
25
26
from sparkle.models import SparkleVersion
27
from sparkle.statistics import collect_statistics
28
from omaha.models import Application
29
30
logger = logging.getLogger(__name__)
31
32
33
class SparkleView(ListView):
34
    http_method_names = ['get']
35
    queryset = SparkleVersion.objects.filter_by_enabled()
36
    template_name = 'sparkle/appcast.xml'
37
38
    def get(self, request, *args, **kwargs):
39
        self.appname = self.kwargs.get('app_name')
40
        self.appid = None
41
        self.channel = self.kwargs.get('channel')
42
43
        try:
44
            app = Application.objects.get(name=self.kwargs.get('app_name'))
45
            self.appid = app.id
46
        except Application.DoesNotExist:
47
            raise Http404
48
49
        collect_statistics(request, self.appid, self.channel)   # It should be a celery task
50
        return super(SparkleView, self).get(self, request, *args, **kwargs)
51
52
    def get_queryset(self):
53
        qs = super(SparkleView, self).get_queryset()
54
        qs = qs.filter(channel__name=self.channel,
55
                  app__name=self.appname).order_by('short_version')
56
        cur_short_version = self.request.GET.get('appVersionShort')
57
        if cur_short_version:
58
            cur_version = '.'.join(cur_short_version.split('.')[-2:])
59
            upper_version = qs.filter(version__gt=cur_version).filter(is_critical=True).order_by('version').first() or \
60
                            qs.order_by('-version').first()
61
            return qs.filter(version__lte=upper_version.version, version__gte=cur_version)
62
        else:
63
            return qs
64
65
    def get_context_data(self, **kwargs):
66
        context = super(SparkleView, self).get_context_data(**kwargs)
67
        context['app_name'] = self.appname
68
        context['channel'] = self.channel
69
        return context
70
71
    def render_to_response(self, context, **response_kwargs):
72
        response = super(SparkleView, self).render_to_response(context, **response_kwargs)
73
        response['Content-Type'] = 'text/xml; charset=utf-8'
74
        return response
75