# Path: pattern/forms.py
# class PatternForm(forms.ModelForm):
#     class Meta:
#         model = Pattern
#         fields = ('code', 'name','history')
#         widgets = {
#             #'code': CodeEditor(attrs={'style': 'width: 90%'}),
#             'history': forms.HiddenInput(),
#             'code': forms.HiddenInput()
#         }


from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
from .models import *
from .forms import PatternForm
import json
                service = None,
                defaults={'name': '',
                          'ident': username
                },
            )
            language = Language.objects.get_or_create(
                name = "TidalCycles"
            )
            pattern = Pattern(author = ident,
                              parent = parent,
                              language = language[0]
            )
        
        form = PatternForm(request.POST or None, instance=pattern)
        if form.is_valid():
            form.save(commit=False)
            
            if pattern.getJSON():
                # needs to save (and get an id) before rendering
                pattern.save()
                pattern.render()
                pattern.save()
                return redirect('pattern_detail', pk=pattern.pk)
    else:
        if pk:
            pattern = get_object_or_404(Pattern, pk=pk)
            form = PatternForm(instance=pattern)
            code = pattern.code
            mode = "edit"
        else:

# Path: pattern/forms.py
# class PatternForm(forms.ModelForm):
#     class Meta:
#         model = Pattern
#         fields = ('code', 'name','history')
#         widgets = {
#             #'code': CodeEditor(attrs={'style': 'width: 90%'}),
#             'history': forms.HiddenInput(),
#             'code': forms.HiddenInput()
#         }


# Path: pattern/views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
from .models import *
from .forms import PatternForm
import json

                service = None,
                defaults={'name': '',
                          'ident': username
                },
            )
            language = Language.objects.get_or_create(
                name = "TidalCycles"
            )
            pattern = Pattern(author = ident,
                              parent = parent,
                              language = language[0]
            )
        
        form = PatternForm(request.POST or None, instance=pattern)
        if form.is_valid():
            form.save(commit=False)
            
            if pattern.getJSON():
                # needs to save (and get an id) before rendering
                pattern.save()
                pattern.render()
                pattern.save()
                return redirect('pattern_detail', pk=pattern.pk)
    else:
        if pk:
            pattern = get_object_or_404(Pattern, pk=pk)
            form = PatternForm(instance=pattern)
            code = pattern.code
            mode = "edit"
        else:

            form = PatternForm()