from django.views import View
from Object.ResponseObject import ResponseObject
from Model.models import ProcessInfoLogsModel
from Object.TokenObject import TokenObject
from Object.LogsObject import LogsObject

class ProcessInfoView(View):
    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        request_dict = request.GET
        operation = kwargs.get('operation', None)
        return self.validate(request_dict, operation)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        request_dict = request.POST
        operation = kwargs.get('operation', None)
        return self.validate(request_dict, operation)

    def validate(self, request_dict, operation):
        response = LogsObject()
        # token = request_dict.get('token', None)

        if operation is None:
            return response.json(444, 'error path')
        elif operation == 'add_processinfo':
            return self.add_processinfo(request_dict, response)
        elif operation == 'delete_processinfo':
            return self.delete_processinfo(request_dict, response)

    def add_processinfo(self, request_dict, response):
        d = self.get_info(request_dict)
        if(d['userId'] == ''):
            return response.json(908)
        ProcessInfoLogsModel.objects.create(
            userId=d['userId'],
            devUid=d['devUid'],
            operatingcontent=d['operatingcontent'],
        )
        return response.json(0)

    def delete_processinfo(self, request_dict, response):
        userId = request_dict.get('userId', None)
        if userId:
            ProcessInfoLogsModel.objects.filter(userId=userId).delete()
            return response.json(0)
        else:
            return response.json(444, 'userId is not exist')

    def get_info(self, request_dict):
        devUid = request_dict.get('devUid', None)
        operatingcontent = request_dict.get('operatingcontent', None)
        operatingtime = request_dict.get('operatingtime', None)
        userId = request_dict.get('userId', None)
        return {
            'devUid': devUid,
            'operatingcontent': operatingcontent,
            'operatingtime': operatingtime,
            'userId': userId,
        }




