# Path: QueueDB/models.py
# class Workspace(_OwnerModel):
#     name = models.CharField(max_length=255)
#     description = models.TextField(null=True, blank=True)
#     create_time = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return "%s - %s" % (self.name, self.user.username)
# 
# class VirtualEnvironment(_OwnerModel):
#     """Virtual Environment Table
#     Save VEs which can be activated by source command
#     """
#     name = models.CharField(max_length=50)
#     ve_type = models.CharField(choices=(("conda", "conda"),
#                                         ("venv", "venv")),
#                                max_length=10, default="conda")
#     value = models.TextField()
#     activation_command = models.TextField(null=True, blank=True)
# 
#     class Meta:
#         try:
#             constraints = [models.UniqueConstraint(fields=["name", "user"], name="unique_name")]
#         except:
#             unique_together = ("name", "user")
# 
#     def __str__(self):
#         return self.name
# 
# class Experiment(models.Model):
#     name = models.CharField(max_length=300)
#     required_fields = models.TextField()
#     file_support = models.CharField(null=True, blank=True, max_length=500)
# 
#     def __str__(self):
#         return self.name
# 
# class Step(_OwnerModel):
#     """Protocol Table
#     Save steps for protocol
#     """
#     software = models.CharField(max_length=50)
#     parameter = models.TextField()
#     specify_output = models.CharField(max_length=50, blank=True, null=True)
#     parent = models.ForeignKey("ProtocolList", on_delete=models.CASCADE)
#     hash = models.CharField(max_length=50)
#     step_order = models.SmallIntegerField(default=1)
#     env = models.ForeignKey("VirtualEnvironment", blank=True, null=True, on_delete=models.PROTECT)
#     force_local = models.SmallIntegerField(default=0, choices=YES_OR_NO)
#     cpu_prior = models.IntegerField(default=-1, blank=True)
#     mem_prior = models.IntegerField(default=-1, blank=True)
#     disk_prior = models.IntegerField(default=-1, blank=True)
# 
#     def __str__(self):
#         return self.software + ' ' + self.parameter
# 
#     def check_parent(self, parent):
#         if int(self.parent) == parent:
#             return 1
#         else:
#             return 0
# 
#     def update_order(self, new_order):
#         self.step_order = new_order
#         return 1
# 
#     def update_parameter(self, new_parameter):
#         import hashlib
#         m = hashlib.md5(str(self.software + ' ' + new_parameter.strip()).encode())
#         self.hash = m.hexdigest()
#         self.parameter = new_parameter
#         return 1
# 
# class ProtocolList(_OwnerModel):
#     """Protocol List Table
#     Save protocol names
#     """
#     name = models.CharField(max_length=500)
#     description = models.TextField(null=True)
#     ver = models.CharField(max_length=33, null=True)
# 
#     def __str__(self):
#         return self.name
# 
# class Reference(_OwnerModel):
#     """Reference Table
#     Save custom references
#     """
#     name = models.CharField(max_length=255)
#     path = models.CharField(max_length=500)
#     description = models.TextField()
# 
#     def __str__(self):
#         return self.name
# 
# class Profile(models.Model):
#     from uuid import uuid4
#     user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_related")
#     delegate = models.ForeignKey(User, on_delete=models.CASCADE, related_name="%(app_label)s_delegate_related")
#     api_key = models.CharField(default=uuid4, max_length=64, unique=True)
#     api_secret = models.CharField(default=uuid4, max_length=64)
#     upload_folder = models.CharField(default="", blank=True, max_length=1024)
#     archive_folder = models.CharField(default="", blank=True, max_length=1024)
#     notification_enabled = models.SmallIntegerField(choices=((0, "No"), (1, "Yes")), default=0)
# 
#     def __str__(self):
#         return self.user.username
# 
#     def check_owner(self, user, read_only=True):
#         """
#         Check whether the requesting user has the permission to read/change the record
# 
#         :param user: int or User
#             either or int (id of the user) or a User object
#         :param read_only: bool
#             If the ongoing operation only requires for read permission
#         :return:
#         has_perm : bool
#             True for having permission to access the record
#         """
#         if not isinstance(user, User):
#             if type(user) is int:
#                 user = User.objects.get(id=user)
#             else:
#                 raise AssertionError("user should be a User object or an integer")
#         if self.user is None:
#             if read_only:  # public
#                 return True
#             else:
#                 if user.is_staff:
#                     return True
#                 else:
#                     return False
#         elif int(self.user.id) == user.id:  # owner
#             return True
#         elif user.is_staff:  # administrators
#             return True
#         else:
#             return False


from django import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from django.forms import TextInput, Select, Textarea
from django.core.validators import RegexValidator
from QueueDB.models import Workspace, VirtualEnvironment, Experiment, Step, ProtocolList, Reference, Profile

class QueryJobLogForm(forms.Form):
    job = forms.IntegerField(
        required=True
    )
    std_out = forms.IntegerField(
        required=False
    )


class JobManipulateForm(forms.Form):
    job = forms.IntegerField(
        required=True
    )
    step = forms.IntegerField(
        required=False
    )


class JobUpdateForm(forms.Form):
    id = forms.IntegerField(
        required=True
    )
    parameter = forms.CharField(
        required=True
    )


class UpdateProtocolDescForm(forms.Form):
    id = forms.ModelChoiceField(

# Path: QueueDB/models.py
# class Workspace(_OwnerModel):
#     name = models.CharField(max_length=255)
#     description = models.TextField(null=True, blank=True)
#     create_time = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return "%s - %s" % (self.name, self.user.username)
# 
# class VirtualEnvironment(_OwnerModel):
#     """Virtual Environment Table
#     Save VEs which can be activated by source command
#     """
#     name = models.CharField(max_length=50)
#     ve_type = models.CharField(choices=(("conda", "conda"),
#                                         ("venv", "venv")),
#                                max_length=10, default="conda")
#     value = models.TextField()
#     activation_command = models.TextField(null=True, blank=True)
# 
#     class Meta:
#         try:
#             constraints = [models.UniqueConstraint(fields=["name", "user"], name="unique_name")]
#         except:
#             unique_together = ("name", "user")
# 
#     def __str__(self):
#         return self.name
# 
# class Experiment(models.Model):
#     name = models.CharField(max_length=300)
#     required_fields = models.TextField()
#     file_support = models.CharField(null=True, blank=True, max_length=500)
# 
#     def __str__(self):
#         return self.name
# 
# class Step(_OwnerModel):
#     """Protocol Table
#     Save steps for protocol
#     """
#     software = models.CharField(max_length=50)
#     parameter = models.TextField()
#     specify_output = models.CharField(max_length=50, blank=True, null=True)
#     parent = models.ForeignKey("ProtocolList", on_delete=models.CASCADE)
#     hash = models.CharField(max_length=50)
#     step_order = models.SmallIntegerField(default=1)
#     env = models.ForeignKey("VirtualEnvironment", blank=True, null=True, on_delete=models.PROTECT)
#     force_local = models.SmallIntegerField(default=0, choices=YES_OR_NO)
#     cpu_prior = models.IntegerField(default=-1, blank=True)
#     mem_prior = models.IntegerField(default=-1, blank=True)
#     disk_prior = models.IntegerField(default=-1, blank=True)
# 
#     def __str__(self):
#         return self.software + ' ' + self.parameter
# 
#     def check_parent(self, parent):
#         if int(self.parent) == parent:
#             return 1
#         else:
#             return 0
# 
#     def update_order(self, new_order):
#         self.step_order = new_order
#         return 1
# 
#     def update_parameter(self, new_parameter):
#         import hashlib
#         m = hashlib.md5(str(self.software + ' ' + new_parameter.strip()).encode())
#         self.hash = m.hexdigest()
#         self.parameter = new_parameter
#         return 1
# 
# class ProtocolList(_OwnerModel):
#     """Protocol List Table
#     Save protocol names
#     """
#     name = models.CharField(max_length=500)
#     description = models.TextField(null=True)
#     ver = models.CharField(max_length=33, null=True)
# 
#     def __str__(self):
#         return self.name
# 
# class Reference(_OwnerModel):
#     """Reference Table
#     Save custom references
#     """
#     name = models.CharField(max_length=255)
#     path = models.CharField(max_length=500)
#     description = models.TextField()
# 
#     def __str__(self):
#         return self.name
# 
# class Profile(models.Model):
#     from uuid import uuid4
#     user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_related")
#     delegate = models.ForeignKey(User, on_delete=models.CASCADE, related_name="%(app_label)s_delegate_related")
#     api_key = models.CharField(default=uuid4, max_length=64, unique=True)
#     api_secret = models.CharField(default=uuid4, max_length=64)
#     upload_folder = models.CharField(default="", blank=True, max_length=1024)
#     archive_folder = models.CharField(default="", blank=True, max_length=1024)
#     notification_enabled = models.SmallIntegerField(choices=((0, "No"), (1, "Yes")), default=0)
# 
#     def __str__(self):
#         return self.user.username
# 
#     def check_owner(self, user, read_only=True):
#         """
#         Check whether the requesting user has the permission to read/change the record
# 
#         :param user: int or User
#             either or int (id of the user) or a User object
#         :param read_only: bool
#             If the ongoing operation only requires for read permission
#         :return:
#         has_perm : bool
#             True for having permission to access the record
#         """
#         if not isinstance(user, User):
#             if type(user) is int:
#                 user = User.objects.get(id=user)
#             else:
#                 raise AssertionError("user should be a User object or an integer")
#         if self.user is None:
#             if read_only:  # public
#                 return True
#             else:
#                 if user.is_staff:
#                     return True
#                 else:
#                     return False
#         elif int(self.user.id) == user.id:  # owner
#             return True
#         elif user.is_staff:  # administrators
#             return True
#         else:
#             return False


# Path: ui/forms.py
from django import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from django.forms import TextInput, Select, Textarea
from django.core.validators import RegexValidator
from QueueDB.models import Workspace, VirtualEnvironment, Experiment, Step, ProtocolList, Reference, Profile


class QueryJobLogForm(forms.Form):
    job = forms.IntegerField(
        required=True
    )
    std_out = forms.IntegerField(
        required=False
    )


class JobManipulateForm(forms.Form):
    job = forms.IntegerField(
        required=True
    )
    step = forms.IntegerField(
        required=False
    )


class JobUpdateForm(forms.Form):
    id = forms.IntegerField(
        required=True
    )
    parameter = forms.CharField(
        required=True
    )


class UpdateProtocolDescForm(forms.Form):
    id = forms.ModelChoiceField(

        queryset=ProtocolList.objects.all(), required=True