Main source

  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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python2
# Copyright (c) 2012, Kit Barnes
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import inspect
import logging

import web
import markdown

from google.appengine.ext import db
from google.appengine.api import users

import mdx_mathjax
from Form import Form, CombinedForm

def md(text):
    return markdown.markdown(text, ['codehilite(force_linenos=True)', 'fenced_code', 'wikilinks(end_url=)', 'mathjax', 'toc'])

def getLoginURL():
    return users.create_login_url(web.ctx.get('path', '/'))

def ensureLogin():
    user = users.get_current_user()
    if user:
        return user
    else:
        raise web.seeother(getLoginURL())

def ensureAdmin():
    user = ensureLogin()
    if users.is_current_user_admin():
        return user
    else:
        raise web.seeother("/")

class Mess(db.Model):
    outString = db.TextProperty()

class Link(db.Model):
    desc = db.StringProperty(required=True)
    href = db.StringProperty(required=True)

    @staticmethod
    def getAll():
        q = db.Query(Link)
        return q

def getForm():
    form = Form( web.form.Textarea( name='outString', description="Mess:", rows="30" )
                        , web.form.Button( 'Save Mess', class_="btn btn-mini btn-primary" )
                        )
    return form

render = web.template.render('templates/', base='base', globals={'getAllLinks':Link.getAll})

class index:
    def GET(self):
        return render.mess("index", None, "Add /something to the url")

class mess:
    form = getForm()
    def GET(self, name):
        form = self.form()
        name = name.lower().rstrip('/')
        m = db.get(db.Key.from_path('Mess', name))
        outString = md(m.outString) if m else ''
        return render.mess(name, form, outString)

    def POST(self, name):
        form = self.form()
        name = name.lower().rstrip('/')
        if not form.validates():
            return "No validate"
        m = Mess( key_name=name
                , urlString=name
                , outString=form.d.outString
                )
        m.put()
        raise web.seeother("/%s" % name)

class edit:
    form = getForm()
    def GET(self, name):
        ensureAdmin() # You're damn right *I* can edit them
        form = self.form()
        name = name.lower()
        m = db.get(db.Key.from_path('Mess', name))
        outString = m.outString if m else ''
        form.fill( outString=outString )
        return render.mess(name, form, '')

    def POST(self, name):
        ensureAdmin()
        form = self.form()
        name = name.lower()
        if not form.validates():
            return "No validate"
        m = Mess( key_name=name
                , urlString=name
                , outString=form.d.outString
                )
        m.put()
        raise web.seeother("/%s/edit" % name)

class code:
    def GET(self):
        t = inspect.getsource(inspect.getmodule(self.__class__))
        t = "## Main source\n~~~~{.python}\n"+t+"\n~~~~"
        t += "\nFind the full source on [Bitbucket](https://bitbucket.org/KitB/messypaste)"
        return render.mess("code", None, md(t))

class admin:
    nlf = Form( web.form.Textbox(description="Description", name="desc"), web.form.Textbox("href"), web.form.Button("Add Link") )

    def GET(self):
        user = ensureAdmin()
        newLinkForm = CombinedForm( [self.nlf()], action="", method="post" )
        return render.admin(newLinkForm)

    def POST(self):
        user = ensureAdmin()
        f = self.nlf()
        if not f.validates():
            return "Form didn't validate"
        l = Link(**f.d)
        l.put()
        web.seeother("/admin")

class renderMD:
    def POST(self):
        data = web.input()
        markdown = data.markdown
        web.header("Cache-Control", "no-cache")
        return md(markdown)

urls = ( '/', index
       , '/code', code
       , '/source', code
       , '/admin', admin
       , '/(.*)/edit', edit
       , '/render', renderMD
       , '/(.*)', mess
       )

app = web.application(urls, globals())
application = app.wsgifunc()

Find the full source on Bitbucket