<commit_before># -*- coding: utf-8 -*-
#
# Newfies-Dialer License
# http://www.newfies-dialer.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#

from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from appointment.models.users import CalendarUser, CalendarUserProfile,\
    CalendarSetting
from appointment.models.calendars import Calendar
from appointment.function_def import get_calendar_user_id_list, \
    get_all_calendar_user_id_list
from apirest.calendar_user_serializers import CalendarUserSerializer
from user_profile.models import Manager


class CalendarUserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows calendar user to be viewed or edited.
    """
    queryset = CalendarUser.objects.filter(is_staff=False, is_superuser=False)
    serializer_class = CalendarUserSerializer
    authentication = (BasicAuthentication, SessionAuthentication)
    permissions = (IsAuthenticatedOrReadOnly, )


    def list(self, request, *args, **kwargs):
        """get list of all CalendarUser objects"""
        if self.request.user.is_superuser:
            calendar_user_list = get_all_calendar_user_id_list()
        else:
            calendar_user_list = get_calendar_user_id_list(request.user)

        snippets = CalendarUser.objects.filter(id__in=calendar_user_list).order_by('id')
        list_data = []
        for c_user in snippets:
            try:
                calendar_obj = Calendar.objects.get(user=c_user)
                calendar_dict = {
                    'name': calendar_obj.name,
                    'max_concurrent': calendar_obj.max_concurrent,
                }
            except:
                calendar_dict = {}
            data = {
                'id': c_user.id,
                'username': c_user.username,
                'password': <PASSWORD>,
                'last_name': c_user.last_name,
                'first_name': c_user.first_name,
                'email': c_user.email,
                #'groups': c_user.groups,
                'calendar': calendar_dict,
            }
            list_data.append(data)

        temp_data = ", ".join(str(e) for e in list_data)
        import ast
        final_data = ast.literal_eval(temp_data)
        #serializer = CalendarUserSerializer(snippets, many=True)
        return Response(final_data)

    def post_save(self, obj, created=False):
        """Create Calendar User object with default name & current Calendar User"""
        obj.set_password(self.request.DATA['password'])
        obj.save()

        CalendarUserProfile.objects.create(
            user=obj,
            manager=Manager.objects.get(username=self.request.user),
            calendar_setting=CalendarSetting.objects.filter(user=self.request.user)[0]
        )

        Calendar.objects.create(
            name='default',
            user=obj,
        )

    def retrieve(self, request, *args, **kwargs):
        """retrieve CalendarUser object"""
        self.object = self.get_object()
        data = dict()
        try:
            calendar_obj = Calendar.objects.get(user=self.object)
            calendar_dict = {
                'name': calendar_obj.name,
                'max_concurrent': calendar_obj.max_concurrent,
            }
        except:
            calendar_dict = {}

        data = {
            'id': self.object.id,
            'username': self.object.username,
            'password': self.object.password,
            'last_name': self.object.last_name,
            'first_name': self.object.first_name,
            'email': self.object.email,
            #'groups': c_user.groups,<commit_msg>update calendar_user rest api<commit_after># -*- coding: utf-8 -*-
#
# Newfies-Dialer License
# http://www.newfies-dialer.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#

from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from appointment.models.users import CalendarUser, CalendarUserProfile,\
    CalendarSetting
from appointment.models.calendars import Calendar
from appointment.function_def import get_calendar_user_id_list, \
    get_all_calendar_user_id_list
from apirest.calendar_user_serializers import CalendarUserSerializer
from user_profile.models import Manager


class CalendarUserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows calendar user to be viewed or edited.
    """
    queryset = CalendarUser.objects.filter(is_staff=False, is_superuser=False)
    serializer_class = CalendarUserSerializer
    authentication = (BasicAuthentication, SessionAuthentication)
    permissions = (IsAuthenticatedOrReadOnly, )


    def list(self, request, *args, **kwargs):
        """get list of all CalendarUser objects"""
        if self.request.user.is_superuser:
            calendar_user_list = get_all_calendar_user_id_list()
        else:
            calendar_user_list = get_calendar_user_id_list(request.user)

        snippets = CalendarUser.objects.filter(id__in=calendar_user_list).order_by('id')
        list_data = []
        for c_user in snippets:
            try:
                calendar_obj = Calendar.objects.get(user=c_user)
                calendar_dict = {
                    'name': calendar_obj.name,
                    'max_concurrent': calendar_obj.max_concurrent,
                }
            except:
                calendar_dict = {}
            data = {
                'id': c_user.id,
                'username': c_user.username,
                'password': <PASSWORD>,
                'last_name': c_user.last_name,
                'first_name': c_user.first_name,
                'email': c_user.email,
                #'groups': c_user.groups,
                'calendar': calendar_dict,
            }
            list_data.append(data)

        temp_data = ", ".join(str(e) for e in list_data)
        import ast
        final_data = ast.literal_eval(temp_data)
        #serializer = CalendarUserSerializer(snippets, many=True)
        return Response(final_data)

    def post_save(self, obj, created=False):
        """Create Calendar User object with default name & current Calendar User"""

        if created:
            obj.set_password(self.request.DATA['password'])
            obj.save()

            CalendarUserProfile.objects.create(
                user=obj,
                manager=Manager.objects.get(username=self.request.user),
                calendar_setting=CalendarSetting.objects.filter(user=self.request.user)[0]
            )

            Calendar.objects.create(
                name='default',
                user=obj,
            )


    def retrieve(self, request, *args, **kwargs):
        """retrieve CalendarUser object"""
        self.object = self.get_object()
        data = dict()
        try:
            calendar_obj = Calendar.objects.get(user=self.object)
            calendar_dict = {
                'name': calendar_obj.name,
                'max_concurrent': calendar_obj.max_concurrent,
            }
        except:
            calendar_dict = {}

        data = {
            'id': self.object.id,
            'username': self.object.username,
            'password': self.object.password,
            'last_name': self.object.last_name,
            'first_name': self.object.first_name,
            'email': self.object.email,
            #'groups': c_user.groups,
{'fraction_non_alphanumeric': 0.09478962818003914, 'fraction_numerical': 0.003669275929549902, 'mean_word_length': 2.3567323481116587, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5092153', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2188, 'n_words': 496}