162 lines
5.3 KiB
Python
162 lines
5.3 KiB
Python
from django.shortcuts import render
|
|
from markdown2 import Markdown
|
|
from . import util
|
|
from django import forms
|
|
import random
|
|
from django.contrib import messages
|
|
from django.core.files.storage import default_storage
|
|
|
|
class CreateForm(forms.Form):
|
|
"""
|
|
Creates a form for creating a new entry with title and content.
|
|
"""
|
|
title = forms.CharField(label="Title ",max_length = 30)
|
|
textarea = forms.CharField(widget=forms.Textarea(attrs={'rows': 5,'cols': 40,'style': 'height: 23.5em;'}),label = 'TextArea')
|
|
|
|
class EditForm(forms.Form):
|
|
"""
|
|
Edits an existing form for with title and content.
|
|
"""
|
|
title = forms.CharField(label="Title",max_length = 30)
|
|
textarea = forms.CharField(widget=forms.Textarea(attrs={'rows': 5,'cols': 40,'style': 'height: 23.5em;'}),label = 'TextArea')
|
|
def page(request,title):
|
|
"""
|
|
Displays the content of an entry given it's title.
|
|
"""
|
|
markdown = Markdown()
|
|
wiki_content= markdown.convert(util.get_entry(title))
|
|
return render(request,"encyclopedia/wiki.html",{
|
|
|
|
"title": title,
|
|
"wiki_content":wiki_content
|
|
|
|
})
|
|
|
|
|
|
def index(request):
|
|
"""
|
|
index page and obtaining search result.
|
|
"""
|
|
if request.method == "GET":
|
|
return render(request, "encyclopedia/index.html", {
|
|
"entries": util.list_entries()
|
|
})
|
|
elif request.method == "POST":
|
|
search = request.POST.get("q", "")
|
|
entries = util.list_entries()
|
|
if search in entries:
|
|
return page(request,search)
|
|
else:
|
|
res = [i for i in entries if search in i]
|
|
if not res:
|
|
return error(request,error_heading = "Search not found!")
|
|
else:
|
|
return render(request,"encyclopedia/search.html",{
|
|
|
|
'res':res,
|
|
'search': search
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def create(request):
|
|
"""
|
|
Dislays the created form, obtains the details and adding it entries.
|
|
"""
|
|
if request.method == "POST":
|
|
form = CreateForm(request.POST)
|
|
if form.is_valid():
|
|
title = form.cleaned_data["title"]
|
|
content = form.cleaned_data["textarea"]
|
|
if title in util.list_entries():
|
|
return error(request, error_heading = "Already Page Exists",error_message = "Would you like to choose a different title?")
|
|
if content[0] == '#':
|
|
util.save_entry(title,content)
|
|
else:
|
|
content = "# " + title +"\n"+ content
|
|
util.save_entry(title,content)
|
|
messages.success(request,"Entry added successfully")
|
|
request.method = "GET"
|
|
return page(request,title)
|
|
else:
|
|
return render(request,"encyclopedia/create.html",{
|
|
|
|
"form": CreateForm()
|
|
|
|
|
|
})
|
|
def error(request,error_heading = "Page not found!",error_message = "Would you like to add a new page with this title?"):
|
|
"""
|
|
Dispays given heading and message on the error page
|
|
"""
|
|
return render(request,"encyclopedia/error.html",{
|
|
|
|
'error_heading':error_heading,
|
|
'error_message':error_message
|
|
|
|
|
|
})
|
|
def randomPage(request):
|
|
"""
|
|
Displays a random page from the given list of entries.
|
|
"""
|
|
entries = util.list_entries()
|
|
title = random.choice(entries)
|
|
return page(request,title)
|
|
|
|
def edit(request,title):
|
|
"""
|
|
Displays the edit form.
|
|
"""
|
|
return render(request,"encyclopedia/edit.html",
|
|
{
|
|
|
|
"form":EditForm(initial = {
|
|
"title": title,
|
|
"textarea": util.get_entry(title)
|
|
}),
|
|
|
|
"title":title
|
|
|
|
})
|
|
|
|
|
|
def editSubmit(request):
|
|
"""
|
|
Obtains the submitted edit form and returns to edited entry.
|
|
"""
|
|
if request.method == "POST":
|
|
title = request.POST.get("or_title", "")
|
|
form = EditForm(request.POST)
|
|
if form.is_valid():
|
|
content = form.cleaned_data["textarea"]
|
|
edit_title = form.cleaned_data["title"]
|
|
old_content = util.get_entry(title)
|
|
if edit_title == title and old_content == content:
|
|
messages.success(request,"No edits done")
|
|
elif edit_title == title and old_content != content:
|
|
util.save_entry(title,content)
|
|
messages.success(request,"Edits Successful")
|
|
|
|
elif edit_title != title:
|
|
entries = util.list_entries()
|
|
exclude_entries = [i for i in entries if i != title]
|
|
if edit_title in exclude_entries:
|
|
return error(request, error_heading = "Already Page Exists",error_message = "Would you like to choose a different title?")
|
|
else:
|
|
filename = f"entries/{edit_title}.md"
|
|
util.save_entry(edit_title, content)
|
|
messages.success(request,"Edit succesful")
|
|
request.method = "GET"
|
|
return page(request,edit_title)
|
|
|
|
else:
|
|
|
|
return render(request,"encyclopedia/edit.html",{
|
|
|
|
"form": EditForm()
|
|
|
|
|
|
}) |