"""
Provides a JSON API for common components.
"""

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf.urls import url, include

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters, generics

import common.models
import common.serializers


class SettingsList(generics.ListAPIView):

    filter_backends = [
        DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter,
    ]

    ordering_fields = [
        'pk',
        'key',
        'name',
    ]

    search_fields = [
        'key',
    ]


class GlobalSettingsList(SettingsList):
    """
    API endpoint for accessing a list of global settings objects
    """

    queryset = common.models.InvenTreeSetting.objects.all()
    serializer_class = common.serializers.GlobalSettingsSerializer



class UserSettingsList(SettingsList):
    """
    API endpoint for accessing a list of user settings objects
    """

    queryset = common.models.InvenTreeUserSetting.objects.all()
    serializer_class = common.serializers.UserSettingsSerializer

    def filter_queryset(self, queryset):
        """
        Only list settings which apply to the current user        
        """

        try:
            user = self.request.user
        except AttributeError:
            return common.models.InvenTreeUserSetting.objects.none()

        queryset = super().filter_queryset(queryset)

        queryset = queryset.filter(user=user)

        return queryset


common_api_urls = [

    # User settings
    url(r'^user/', include([
        # User Settings Detail

        # User Settings List
        url(r'^.*$', UserSettingsList.as_view(), name='api-user-setting-list'),
    ])),

    # Global settings
    url(r'^global/', include([
        # Global Settings Detail

        # Global Settings List
        url(r'^.*$', GlobalSettingsList.as_view(), name='api-global-setting-list'),
    ]))

]

@@ -259,17 +259,30 @@
 generics
+, permissions
 %0A
-
 %0Aimport 
@@ -859,16 +859,866 @@
 alizer%0A%0A
+%0Aclass GlobalSettingsPermissions(permissions.BasePermission):%0A    %22%22%22%0A    Special permission class to determine if the user is %22staff%22%0A    %22%22%22%0A%0A    def has_permission(self, request, view):%0A        %22%22%22%0A        Check that the requesting user is 'admin'%0A        %22%22%22%0A%0A        print(%22User:%22, request.user, request.user.is_staff)%0A%0A        try:%0A            user = request.user%0A%0A            return user.is_staff%0A        except AttributeError:%0A            return False%0A%0A%0Aclass GlobalSettingsDetail(generics.RetrieveUpdateAPIView):%0A    %22%22%22%0A    Detail view for an individual %22global setting%22 object.%0A%0A    - User must have 'staff' status to view / edit%0A    %22%22%22%0A%0A    queryset = common.models.InvenTreeSetting.objects.all()%0A    serializer_class = common.serializers.GlobalSettingsSerializer%0A%0A    permission_classes = %5B%0A        GlobalSettingsPermissions,%0A    %5D%0A    
 %0A%0Aclass 
@@ -2636,32 +2636,32 @@
 al/', include(%5B%0A
-
         # Global
@@ -2668,32 +2668,129 @@
  Settings Detail
+%0A        url(r'%5E(?P%3Cpk%3E%5Cd+)/', GlobalSettingsDetail.as_view(), name='api-global-setting-detail'),
 %0A%0A        # Glob

Add "detail" view for global settings objects