# Path: pattern/widgets.py
# class CodeEditor(forms.Textarea):
#     def __init__(self, *args, **kwargs):
#         super(CodeEditor, self).__init__(*args, **kwargs)
#         self.attrs['class'] = 'code-editor'
# 
#     class Media:
#         css = {
#             'all': (
#                 staticfiles_storage.url('codemirror-5.22.0/lib/codemirror.css'),
#                 staticfiles_storage.url('css/edit.css'),
#                 staticfiles_storage.url('livewriting/livewriting.css'),
#             )
#         }
#         js = (
#             staticfiles_storage.url('codemirror-5.22.0/lib/codemirror.js'),
#             staticfiles_storage.url('livewriting/index.js'),
#             staticfiles_storage.url('js/livewriting-init.js'),
#         )
# 
# Path: pattern/models.py
# class Pattern(models.Model):
#     name = models.CharField(max_length=200)
#     loves = models.ManyToManyField(Identity, related_name="pattern_loves")
#     author = models.ForeignKey('Identity')
#     language = models.ForeignKey('Language')
#     parent = models.ForeignKey('self', null=True)
#     code = models.TextField()
#     json = models.TextField(null=True)
#     history = models.TextField(null=True)
#     audiourl = models.URLField(blank=True, null=True)
#     errorMessage = models.TextField(blank=True, null=True)
#     editNumber = models.IntegerField(default=0)
#     status = models.CharField(
#         max_length=10,
#         choices = (
#             ('error', 'Error'),
#             ('editing', 'Editing'),
#             ('pending', 'Pending'),
#             ('rendering', 'Rendering'),
#             ('live', 'Live'),
#         )
#     )
#     
#     def __str__(self):
#         return self.name
# 
#     def lovePattern(self, user):
#         self.loves.add(user)
# 
#     def unlovePattern(self, user):
#         print("unlove")
#         self.loves.remove(user)
# 
#     def typecheck(self):
#         result = ws.sendrecv("/typecheck " + self.code)
#         print(result[1])
#         if (result[1] == 'ok'):
#             self.status = 'pending'
#             return(True)
#         else:
#             if (result[1] == 'nok'):
#                 self.status = 'error'
#                 print("static: "  + result[1])
#                 print("error: "  + result[2])
#                 self.errorMessage = result[2]
#                 return(False)
# 
#     def getJSON(self):
#         result = ws.sendrecv("/renderJSON " + self.code)
#         print(result[1])
#         if (result[1] == 'ok'):
#             self.json = result[2]
#             return(True)
#         else:
#             if (result[1] == 'nok'):
#                 self.status = 'error'
#                 print("static: "  + result[1])
#                 print("error: "  + result[2])
#                 self.errorMessage = result[2]
#                 return(False)
#             
#     def render(self):
#         filename = str(self.id)
#         filepath_tmp = os.path.join(settings.PATTERN_QUEUEDIR, filename + ".tmp")
#         filepath = os.path.join(settings.PATTERN_QUEUEDIR, filename + ".tidal")
#         f = open(filepath_tmp, "w")
#         f.write(self.code)
#         f.close()
#         os.rename(filepath_tmp, filepath)
#         self.audiourl = os.path.join(settings.PATTERN_AUDIOURL,
#                                      settings.PATTERN_GENPATH(filename),
#                                      filename + ".mp3"
#                                     )
#         self.status = 'rendering'
# 
#     def is_live(self):
#         result = False
#         if self.status == 'live':
#             result = True
#         elif self.status == 'rendering':
#             filepath = os.path.join(settings.PATTERN_AUDIODIR, self.audiourl)
#             print "test " + filepath
#             if os.path.isfile(filepath):
#                 result = True
#                 self.status = 'live'
#                 self.save()
#         return result


from django import forms
from .widgets import CodeEditor
from .models import Pattern


class PatternForm(forms.ModelForm):
    class Meta:
        model = Pattern
        fields = ('code', 'name','history')

# Path: pattern/widgets.py
# class CodeEditor(forms.Textarea):
#     def __init__(self, *args, **kwargs):
#         super(CodeEditor, self).__init__(*args, **kwargs)
#         self.attrs['class'] = 'code-editor'
# 
#     class Media:
#         css = {
#             'all': (
#                 staticfiles_storage.url('codemirror-5.22.0/lib/codemirror.css'),
#                 staticfiles_storage.url('css/edit.css'),
#                 staticfiles_storage.url('livewriting/livewriting.css'),
#             )
#         }
#         js = (
#             staticfiles_storage.url('codemirror-5.22.0/lib/codemirror.js'),
#             staticfiles_storage.url('livewriting/index.js'),
#             staticfiles_storage.url('js/livewriting-init.js'),
#         )
# 
# Path: pattern/models.py
# class Pattern(models.Model):
#     name = models.CharField(max_length=200)
#     loves = models.ManyToManyField(Identity, related_name="pattern_loves")
#     author = models.ForeignKey('Identity')
#     language = models.ForeignKey('Language')
#     parent = models.ForeignKey('self', null=True)
#     code = models.TextField()
#     json = models.TextField(null=True)
#     history = models.TextField(null=True)
#     audiourl = models.URLField(blank=True, null=True)
#     errorMessage = models.TextField(blank=True, null=True)
#     editNumber = models.IntegerField(default=0)
#     status = models.CharField(
#         max_length=10,
#         choices = (
#             ('error', 'Error'),
#             ('editing', 'Editing'),
#             ('pending', 'Pending'),
#             ('rendering', 'Rendering'),
#             ('live', 'Live'),
#         )
#     )
#     
#     def __str__(self):
#         return self.name
# 
#     def lovePattern(self, user):
#         self.loves.add(user)
# 
#     def unlovePattern(self, user):
#         print("unlove")
#         self.loves.remove(user)
# 
#     def typecheck(self):
#         result = ws.sendrecv("/typecheck " + self.code)
#         print(result[1])
#         if (result[1] == 'ok'):
#             self.status = 'pending'
#             return(True)
#         else:
#             if (result[1] == 'nok'):
#                 self.status = 'error'
#                 print("static: "  + result[1])
#                 print("error: "  + result[2])
#                 self.errorMessage = result[2]
#                 return(False)
# 
#     def getJSON(self):
#         result = ws.sendrecv("/renderJSON " + self.code)
#         print(result[1])
#         if (result[1] == 'ok'):
#             self.json = result[2]
#             return(True)
#         else:
#             if (result[1] == 'nok'):
#                 self.status = 'error'
#                 print("static: "  + result[1])
#                 print("error: "  + result[2])
#                 self.errorMessage = result[2]
#                 return(False)
#             
#     def render(self):
#         filename = str(self.id)
#         filepath_tmp = os.path.join(settings.PATTERN_QUEUEDIR, filename + ".tmp")
#         filepath = os.path.join(settings.PATTERN_QUEUEDIR, filename + ".tidal")
#         f = open(filepath_tmp, "w")
#         f.write(self.code)
#         f.close()
#         os.rename(filepath_tmp, filepath)
#         self.audiourl = os.path.join(settings.PATTERN_AUDIOURL,
#                                      settings.PATTERN_GENPATH(filename),
#                                      filename + ".mp3"
#                                     )
#         self.status = 'rendering'
# 
#     def is_live(self):
#         result = False
#         if self.status == 'live':
#             result = True
#         elif self.status == 'rendering':
#             filepath = os.path.join(settings.PATTERN_AUDIODIR, self.audiourl)
#             print "test " + filepath
#             if os.path.isfile(filepath):
#                 result = True
#                 self.status = 'live'
#                 self.save()
#         return result


# Path: pattern/forms.py
from django import forms
from .widgets import CodeEditor
from .models import Pattern



class PatternForm(forms.ModelForm):
    class Meta:
        model = Pattern
        fields = ('code', 'name','history')

        widgets = {