# 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

def pattern_index(request):
    rendering = Pattern.objects.filter(status__in=['rendering'])
    for pattern in rendering:
        pattern.is_live()
        
    latest = Pattern.objects.filter(status__in=['live']).order_by('-id')[:32]
    context = {'latest': latest}
    return render(request, 'pattern/index.html', context)

def pattern_all(request):
    all = Pattern.objects.filter(status__in=['live']).order_by('-id')
    context = {'all': all}
    return render(request, 'pattern/all.html', context)

def pattern_about(request):
    latest = Pattern.objects.filter(status__in=['live']).order_by('-id')[:16]
    context = {'latest': latest}
    return render(request, 'pattern/about.html', context)

def pattern_help(request):
    latest = Pattern.objects.filter(status__in=['live']).order_by('-id')[:16]
    context = {'latest': latest}
    return render(request, 'pattern/help.html', context)

@login_required
def pattern_love(request, pk):
    if request.method == "POST":
        i , created = Identity.objects.get_or_create(
            user = request.user,

# 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


def pattern_index(request):
    rendering = Pattern.objects.filter(status__in=['rendering'])
    for pattern in rendering:
        pattern.is_live()
        
    latest = Pattern.objects.filter(status__in=['live']).order_by('-id')[:32]
    context = {'latest': latest}
    return render(request, 'pattern/index.html', context)

def pattern_all(request):
    all = Pattern.objects.filter(status__in=['live']).order_by('-id')
    context = {'all': all}
    return render(request, 'pattern/all.html', context)

def pattern_about(request):
    latest = Pattern.objects.filter(status__in=['live']).order_by('-id')[:16]
    context = {'latest': latest}
    return render(request, 'pattern/about.html', context)

def pattern_help(request):
    latest = Pattern.objects.filter(status__in=['live']).order_by('-id')[:16]
    context = {'latest': latest}
    return render(request, 'pattern/help.html', context)

@login_required
def pattern_love(request, pk):
    if request.method == "POST":
        i , created = Identity.objects.get_or_create(
            user = request.user,

            service = None,