from django.contrib.admin.views.decorators import staff_member_required
from django.core.paginator import Paginator
from django.shortcuts import render
from .common import *
from .constants import *
from .models import *


def article_directory(request, category="all", page_num=1):
    """ Returns page listing all articles sorted either by date or name """
    data = {"title": "Article Directory"}

    # Pull articles for page
    qs = Article.search(request.GET)

    if category != "all":
        qs = qs.filter(category=category)
        data["title"] = category.title() + " Directory"
        data["category"] = category.title()

    if request.GET.get("sort", "date") == "date":
        qs = qs.order_by("-date")
    elif request.GET.get("sort") == "title":
        qs = qs.order_by("title")
    elif request.GET.get("sort") == "author":
        qs = qs.order_by("author")
    elif request.GET.get("sort") == "category":
        qs = qs.order_by("category")

    data["available_views"] = ["detailed", "list", "gallery"]
    data["view"] = get_selected_view_format(request, data["available_views"])
    data = get_pagination_data(request, data, qs)
    data["sort_options"] = [
        {"text": "Date", "val": "date"},
        {"text": "Title", "val": "title"},
        {"text": "Author", "val": "author"},
        {"text": "Category", "val": "category"},
    ]

    return render(request, "museum_site/article_directory.html", data)


def article_view(request, id, page=0):
    """ Returns an article pulled from the database """
    a_id = int(id)
    if a_id == 1:
        uri = request.build_absolute_uri()
        if uri.lower().endswith(".zip"):
            return article(request, "1", uri.split("/")[-1])

    slug = request.path.split("/")[-1]
    page = int(page)
    data = {"id": a_id}
    data["custom_layout"] = "article"

    if request.GET.get("secret") is None:
        data["article"] = get_object_or_404(
            Article, pk=id, published=PUBLISHED_ARTICLE
        )
    elif request.GET.get("secret") == PASSWORD2DOLLARS:
        data["access"] = "early"
        data["article"] = get_object_or_404(
            Article,
            Q(published=PUBLISHED_ARTICLE) | Q(published=UPCOMING_ARTICLE),
            pk=a_id
        )
        data["private_disclaimer"] = True

    elif request.GET.get("secret") == PASSWORD5DOLLARS:
        data["access"] = "really_early"
        data["article"] = get_object_or_404(
            Article,
            Q(published=PUBLISHED_ARTICLE) |
            Q(published=UPCOMING_ARTICLE) |
            Q(published=UNPUBLISHED_ARTICLE),
            pk=a_id,
        )
        data["private_disclaimer"] = True
    data["page"] = page
    data["page_count"] = data["article"].content.count("<!--Page-->") + 1
    data["page_range"] = list(range(1, data["page_count"] + 1))
    data["next"] = None if page + 1 > data["page_count"] else page + 1
    data["prev"] = page - 1
    data["slug"] = str(slug)

    data["title"] = data["article"].title

    zgames = data["article"].file_set.all()
    if zgames:
        data["file"] = zgames[0]
        if len(zgames) > 1:
            data["multifile"] = True
            data["zgames"] = zgames

            if request.GET.get("alt_file"):
                data["file"] = get_object_or_404(
                    zgames, filename=request.GET["alt_file"]
                )

    # Split article to current page
    data["article"].content = data["article"].content.split(
        "<!--Page-->"
    )[data["page"]-1]
    return render(request, "museum_site/article_view.html", data)


def patron_articles(request):
    data = {}
    data["early"] = Article.objects.filter(published=UPCOMING_ARTICLE)
    data["really_early"] = Article.objects.filter(published=UNPUBLISHED_ARTICLE)

    if request.POST.get("secret") == PASSWORD2DOLLARS:
        data["access"] = "early"
    elif request.POST.get("secret") == PASSWORD5DOLLARS:
        data["access"] = "really_early"
    elif request.POST.get("secret") is not None:
        data["wrong_password"] = True

    return render(request, "museum_site/patreon_articles.html", data)

@@ -215,16 +215,65 @@
 port *%0A%0A
+from .file_views import file_articles  # Kludge%0A%0A
 %0Adef art
@@ -1513,16 +1513,24 @@
 equest, 
+article_
 id, page
@@ -1598,22 +1598,50 @@
     
-a_id = int(id)
+# Awful kludge to deal with a url conflict
 %0A   
@@ -1649,16 +1649,24 @@
 if a
+rticle
 _id == 
-1
+%221%22
 :%0A  
@@ -1718,45 +1718,38 @@
     
-if uri.lower().endsw
+filename = uri.spl
 it
-h
 (%22
-.zip%22):%0A    
+/%22)%5B-1%5D%0A
     
@@ -1759,23 +1759,29 @@
  return 
+file_
 article
+s
 (request
@@ -1786,31 +1786,28 @@
 st, 
-%221%22, uri.split(%22/%22)%5B-1%5D
+article_id, filename
 )%0A%0A 
@@ -1884,16 +1884,22 @@
 %7B%22id%22: a
+rticle
 _id%7D%0A   
@@ -2045,16 +2045,24 @@
 cle, pk=
+article_
 id, publ

Fix url overlap for files starting with non-alphabet characters