from django.db import models
import os, errno

from api.utils import *
state = [
    ('init', 'init'),
    ('calculating', 'calculating'),
    ('pause', 'pause'),
    ('done', 'done'),
]

class MissionManager(models.Manager):
    def statistic(self, user):
      missions = user.own_missions.all()
      return {
          'amount': len(missions),
      }

class Mission(models.Model):
    name = models.CharField(default="未命名任务", max_length=64)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    nodeFile = models.ForeignKey(to="api.File", on_delete=models.CASCADE, related_name="own_missions_node")
    edgeFile = models.ForeignKey(to="api.File", on_delete=models.CASCADE, related_name="own_missions_edge")
    user = models.ForeignKey(to="api.User", on_delete=models.CASCADE, related_name="own_missions")
    
    objects = MissionManager()
    state = models.CharField(choices=state, default="init", max_length=32)

    class Meta:
        app_label = 'api'
