Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的API状态设计模式_Python_Django_Design Patterns - Fatal编程技术网

python中的API状态设计模式

python中的API状态设计模式,python,django,design-patterns,Python,Django,Design Patterns,据我所知,以下代码是否足以回答所附问题?请您帮助澄清缺失的点或部分,如果我添加一些异常捕获,如果编辑_title和编辑_description仅在新的类()或任务API类()中的摘要中,并在子类中重写它,我是否应该使用Django rest framework API,下面的“进行中”状态写为“链接两个任务,您应该能够使用任何id调用这两个任务”,这是什么意思 # States of a Task class TaskAPI(object): """ Abstract base

据我所知,以下代码是否足以回答所附问题?请您帮助澄清缺失的点或部分,如果我添加一些异常捕获,如果编辑_title编辑_description仅在新的类()或任务API类()中的摘要中,并在子类中重写它,我是否应该使用Django rest framework API,下面的“进行中”状态写为“链接两个任务,您应该能够使用任何id调用这两个任务”,这是什么意思

    # States of a Task

class TaskAPI(object):
    """ Abstract base class of state of a task """

    name = "state"
    allowed = []

    def switch(self, state):
        """ Switch to new state """
        if state.name in self.allowed:
            print('Current:', self, ' => switched to new state', state.name)
            self.__class__ = state
            # print( self.__class__)
        else:
            print('Current:', self, ' => switching to', state.name, 'not possible.')

    def __str__(self):
        return self.name



class New(TaskAPI):
    """ New State being in progress """

    name = "New"
    allowed = ['InProgress']

    def edit_title(self,new_title):
        self.title = new_title

    def edit_description(self,new_description):
        self.new_description = new_description

class InProgress(TaskAPI):
    """ State of being in progress after New state and need to switched to be done """

    name = "InProgress"
    allowed = ['Done']


class Done(TaskAPI):
    """ Done State of  task """

    name = "Done"
    allowed = []




class Task(object):
    """ A class representing a Task """

    def __init__(self, id,title,description):
        self.id = id
        self.title = title
        self.description = description
        # State of the computer - default is New.
        self.state = New()

    def change(self, state):
        """ Change state """

        self.state.switch(state)


if __name__ == "__main__":
    t1 = Task(1,'task1','test1')


    t1.change(InProgress)
    t1.change(Done)
    print(t1.state.__class__)
    t1.change(InProgress)
    print(t1.title)
    try:
        t1.state.edit_title
    except:
        print('Edit title process not allowed! for {}'.format(t1.state.name),'state')

Django Rest框架在api方面做得非常好,他们在文档中有很多示例。这个问题是否需要使用Django Rest框架?您不需要使用Rest框架,它只是让编写api变得更容易。您可以与我共享一个编写api的链接吗?这是一个很好的示例