/
views.py
97 lines (81 loc) · 3.14 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.template import Context, loader
from django.core.urlresolvers import reverse
from core.models import Patch
import random
import settings
from diffviewer.patchutils import PatchToHtml
def index(request):
"""
The Frontpage
"""
template = loader.get_template('core/index.html')
context = Context({
'url_root': settings.DOMAIN,
'static_path':settings.DOMAIN + '/static'
})
return HttpResponse(template.render(context))
def submit(request):
"""
Test the patch and save to database
"""
errorMessage = ""
newPatch = Patch()
if(request.method == "POST"):
if("patchText" in request.POST):
newPatch.diffText = request.POST["patchText"]
sampleChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
key = ''.join(random.sample(sampleChars, 6))
newPatch.urlCode = key
if(PatchToHtml(newPatch, newPatch.diffText)):
pass
else:
errorMessage = """Parsing patch failed :(. Unified Diff format
only please. Thanks! If you feel that this is an error, please
email anirudh@anirudhsanjeev.org with the patch attached"""
# TODO: Convert to HTML
else:
errorMessage = "Expecting patchText POST parameter"
if("patchDesc" in request.POST):
newPatch.patchDesc = request.POST['patchDesc']
newPatch.patchDesc.replace('\n\n', '<br><br>')
if("authorName" in request.POST):
newPatch.authorName = request.POST['authorName']
if("emailAddress" in request.POST):
newPatch.emailAddress = request.POST['emailAddress']
else:
errorMessage = "POST Requests only, please :)"
if(errorMessage == ""): # No error
# Generate a primary key
# Save to database
newPatch.save()
# Redirect
#return HttpResponseRedirect(reverse('patchbin.diffviewer.views.showpatch', args=[key]))
return HttpResponseRedirect(settings.DOMAIN + '/' + key)
return HttpResponse(errorMessage)
def about(request):
"""Renders the about page"""
template = loader.get_template('core/about.html')
context = Context({
'url_root': settings.DOMAIN,
'static_path':settings.DOMAIN + '/static'
})
return HttpResponse(template.render(context))
def contribute(request):
"""Renders the /contribute/ page"""
template = loader.get_template('core/contrib.html')
context = Context({
'url_root': settings.DOMAIN,
'static_path':settings.DOMAIN + '/static'
})
return HttpResponse(template.render(context))
def sponsor(request):
"""Renders the /sponsor/ page"""
template = loader.get_template('core/sponsor.html')
context = Context({
'url_root': settings.DOMAIN,
'static_path':settings.DOMAIN + '/static'
})
return HttpResponse(template.render(context))