Completed
Push — master ( 7648fa...b7eb8c )
by Egor
01:22
created

LatestVersionView.get()   A

Complexity

Conditions 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.2
cc 4
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2016 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
from collections import defaultdict
21
from django.conf import settings
22
23
from rest_framework import viewsets
24
from rest_framework import mixins
25
from rest_framework import pagination
26
from rest_framework.views import APIView
27
from rest_framework.response import Response
28
29
from omaha.api import BaseView
30
from omaha.models import Version
31
from sparkle.serializers import SparkleVersionSerializer
32
from sparkle.models import SparkleVersion
33
34
35
class LatestVersionView(APIView):
36
    """
37
    API returns an information about the latest versions of applications available on the server. The information contains supported platforms, provided channels, version numbers and download links.
38
    """
39
    permission_classes = []
40
41
    def get(self, request, format=None):
42
        win_versions = Version.objects.filter_by_enabled()\
43
            .select_related('app__name', 'channel__name')\
44
            .order_by('app__name', 'channel__name', '-version')\
45
            .distinct('app__name', 'channel__name')
46
        mac_versions = SparkleVersion.objects.filter_by_enabled()\
47
            .select_related('app__name', 'channel__name')\
48
            .order_by('app__name', 'channel__name', '-short_version')\
49
            .distinct('app__name', 'channel__name')
50
51
        recursive_dd = lambda: defaultdict(recursive_dd)
52
        data = recursive_dd()
53
        for v in win_versions:
54
            data[v.app.name]['win'][v.channel.name] = dict(version=str(v.version), url=v.file_absolute_url)
55
        for v in mac_versions:
56
            data[v.app.name]['mac'][v.channel.name] = dict(version=str(v.short_version), url=v.file_absolute_url)
57
        return Response(data)
58