# Path: herring/puzzles/models.py
# class Round(models.Model,JSONMixin):
#     # shell model for defining rounds
#     number = models.IntegerField(default=1)
#     name = models.CharField(max_length=200)
#     hunt_url = models.CharField(max_length=1000, default='', **optional)
# 
#     def __str__(self):
#         return 'R' + str(self.number)
# 
#     class Meta:
#         ordering = ['number', 'id']
#     class Json:
#         include_fields = ['id', 'name', 'number', 'puzzle_set', 'hunt_url']
# 
# class Puzzle(models.Model,JSONMixin):
#     # class for all puzzles, including metas
#     parent = models.ForeignKey(Round)
#     name = models.CharField(max_length=200)
#     slug = AutoSlugField(
#         populate_from=puzzle_to_slug,
#         unique=True,
#         db_index=True
#     )
#     number = models.IntegerField(**optional)
#     answer = models.CharField(max_length=200, default='', **optional)
#     note = models.CharField(max_length=200, default='', **optional)
#     tags = models.CharField(max_length=200, default='', **optional)
#     is_meta = models.BooleanField(default=False)
#     url = models.CharField(max_length=1000, default='', **optional)
#     hunt_url = models.CharField(max_length=1000, default='', **optional)
# 
#     tracker = FieldTracker()
# 
#     class Meta:
#         ordering = ['parent', '-is_meta', 'number', 'id']
# 
#     class Json:
#         include_fields = ['id', 'name', 'number', 'answer', 'note', 'tags', 'is_meta', 'url', 'hunt_url', 'slug']
# 
#     def identifier(self):
#         child_type = 'P'
#         if self.is_meta:
#             child_type = 'M'
#         num = str(self.number) if self.number is not None else 'x'
#         return str(self.parent) + child_type + num
#     
#     def __str__(self):
#         return '#%s %s' % (self.slug, self.name)
# 
#     def is_answered(self):
#         return bool(self.answer)
# 
# def to_json_value(field):
#     if isinstance(field, str):
#         return field
#     if isinstance(field, int):
#         return field
#     if isinstance(field, dict):
#         return { key: to_json_value(value) for key, value in field.items() }
#     if isinstance(field, (list, models.query.QuerySet)):
#         return [to_json_value(item) for item in field]
#     if isinstance(field, models.manager.Manager):
#         return [to_json_value(item) for item in field.all()]
#     if isinstance(field, JSONMixin):
#         return field.to_json()


import json
import re
import django.contrib.auth
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, JsonResponse
from django.contrib.auth.decorators import login_required
from .models import Round, Puzzle, to_json_value



@login_required
def logout(request):
    if request.method == "POST":
        django.contrib.auth.logout(request)
    return redirect('/')

@login_required
def index(request):
    admins = django.contrib.auth.models.User.objects.filter(is_staff=True)
    context = {
        'username': request.user.username,
        'rounds': Round.objects.all(),
        'channel': 'general_chat',
        'admins': admins,
    }
    return render(request, 'puzzles/index.html', context)


@login_required
def get_resources(request):
    return render(request, 'puzzles/resources.html', {})


@login_required
def get_puzzles(request):
    data = {

# Path: herring/puzzles/models.py
# class Round(models.Model,JSONMixin):
#     # shell model for defining rounds
#     number = models.IntegerField(default=1)
#     name = models.CharField(max_length=200)
#     hunt_url = models.CharField(max_length=1000, default='', **optional)
# 
#     def __str__(self):
#         return 'R' + str(self.number)
# 
#     class Meta:
#         ordering = ['number', 'id']
#     class Json:
#         include_fields = ['id', 'name', 'number', 'puzzle_set', 'hunt_url']
# 
# class Puzzle(models.Model,JSONMixin):
#     # class for all puzzles, including metas
#     parent = models.ForeignKey(Round)
#     name = models.CharField(max_length=200)
#     slug = AutoSlugField(
#         populate_from=puzzle_to_slug,
#         unique=True,
#         db_index=True
#     )
#     number = models.IntegerField(**optional)
#     answer = models.CharField(max_length=200, default='', **optional)
#     note = models.CharField(max_length=200, default='', **optional)
#     tags = models.CharField(max_length=200, default='', **optional)
#     is_meta = models.BooleanField(default=False)
#     url = models.CharField(max_length=1000, default='', **optional)
#     hunt_url = models.CharField(max_length=1000, default='', **optional)
# 
#     tracker = FieldTracker()
# 
#     class Meta:
#         ordering = ['parent', '-is_meta', 'number', 'id']
# 
#     class Json:
#         include_fields = ['id', 'name', 'number', 'answer', 'note', 'tags', 'is_meta', 'url', 'hunt_url', 'slug']
# 
#     def identifier(self):
#         child_type = 'P'
#         if self.is_meta:
#             child_type = 'M'
#         num = str(self.number) if self.number is not None else 'x'
#         return str(self.parent) + child_type + num
#     
#     def __str__(self):
#         return '#%s %s' % (self.slug, self.name)
# 
#     def is_answered(self):
#         return bool(self.answer)
# 
# def to_json_value(field):
#     if isinstance(field, str):
#         return field
#     if isinstance(field, int):
#         return field
#     if isinstance(field, dict):
#         return { key: to_json_value(value) for key, value in field.items() }
#     if isinstance(field, (list, models.query.QuerySet)):
#         return [to_json_value(item) for item in field]
#     if isinstance(field, models.manager.Manager):
#         return [to_json_value(item) for item in field.all()]
#     if isinstance(field, JSONMixin):
#         return field.to_json()


# Path: herring/puzzles/views.py
import json
import re
import django.contrib.auth
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, JsonResponse
from django.contrib.auth.decorators import login_required
from .models import Round, Puzzle, to_json_value




@login_required
def logout(request):
    if request.method == "POST":
        django.contrib.auth.logout(request)
    return redirect('/')

@login_required
def index(request):
    admins = django.contrib.auth.models.User.objects.filter(is_staff=True)
    context = {
        'username': request.user.username,
        'rounds': Round.objects.all(),
        'channel': 'general_chat',
        'admins': admins,
    }
    return render(request, 'puzzles/index.html', context)


@login_required
def get_resources(request):
    return render(request, 'puzzles/resources.html', {})


@login_required
def get_puzzles(request):
    data = {

        'rounds': Round.objects.all()