diff --git a/TODO b/TODO index 162ec36..de39776 100644 --- a/TODO +++ b/TODO @@ -4,3 +4,4 @@ * Improve i18n support * Improve LodgeIt Interface (null-interface + star) * use udiff module instead of pygments-diff-highlighter for diff highlighning + * use flatland for forms \ No newline at end of file diff --git a/lodgeit/controllers/pastes.py b/lodgeit/controllers/pastes.py index 6c18c0f..fb104cb 100644 --- a/lodgeit/controllers/pastes.py +++ b/lodgeit/controllers/pastes.py @@ -15,7 +15,7 @@ from lodgeit.lib import antispam from lodgeit.i18n import list_languages as i18n_list_languages, _ from lodgeit.utils import render_to_response, url_for from lodgeit.models import Paste -from lodgeit.database import db +from lodgeit.database import session from lodgeit.lib.highlighting import list_languages, STYLES, get_style from lodgeit.lib.pagination import generate_pagination from lodgeit.lib.captcha import check_hashed_solution, Captcha @@ -24,6 +24,7 @@ from lodgeit.lib.captcha import check_hashed_solution, Captcha class PasteController(object): """Provides all the handler callback for paste related stuff.""" + #XXX:dc: using language here clashes with internationalization terms def new_paste(self, language=None): """The 'create a new paste' view.""" language = local.request.args.get('language', language) @@ -32,19 +33,16 @@ class PasteController(object): code = error = '' show_captcha = private = False - parent = None + parent_id = None req = local.request getform = req.form.get if local.request.method == 'POST': code = getform('code', u'') language = getform('language') - parent_id = getform('parent') - if parent_id is not None: - parent = Paste.get(parent_id) - spam = getform('webpage') or antispam.is_spam(code) + if spam: error = _('your paste contains spam') captcha = getform('captcha') @@ -55,11 +53,12 @@ class PasteController(object): error = _('your paste contains spam and the ' 'CAPTCHA solution was incorrect') show_captcha = True + if code and language and not error: - paste = Paste(code, language, parent, req.user_hash, + paste = Paste(code, language, parent_id, req.user_hash, 'private' in req.form) - db.session.add(paste) - db.session.commit() + session.add(paste) + session.commit() local.request.session['language'] = language if paste.private: identifier = paste.private_id @@ -78,7 +77,7 @@ class PasteController(object): private = parent.private return render_to_response('new_paste.html', languages=list_languages(), - parent=parent, + parent=parent_id, code=code, language=language, error=error, @@ -165,7 +164,7 @@ class PasteController(object): old = Paste.get(old_id) new = Paste.get(new_id) - if old is None or new is None: + if not (old or new): raise NotFound() return Response(old.compare_to(new), mimetype='text/plain') @@ -175,7 +174,9 @@ class PasteController(object): back to the page the user is coming from. """ style_name = local.request.form.get('style') - resp = redirect(local.request.environ.get('HTTP_REFERER') or '/') + resp = redirect(local.request.headers.get('referer') or + url_for('pastes/new_paste')) + #XXX:dc: use some sort of form element validation instead if style_name in STYLES: resp.set_cookie('style', style_name) return resp diff --git a/lodgeit/models.py b/lodgeit/models.py index b224cb3..7f89251 100644 --- a/lodgeit/models.py +++ b/lodgeit/models.py @@ -37,16 +37,15 @@ class Paste(db.Model): primaryjoin=parent_id == paste_id, backref=db.backref('parent', remote_side=[paste_id])) - def __init__(self, code, language, parent=None, user_hash=None, + def __init__(self, code, language, parent_id=None, user_hash=None, private=False): if language not in LANGUAGES: language = 'text' self.code = u'\n'.join(code.splitlines()) self.language = language - if isinstance(parent, Paste): - self.parent = parent - elif parent is not None: - self.parent_id = parent + #XXX:dc: set these a bit more sanely, allowing two types is bad + if parent_id: + self.parent_id = parent_id self.pub_date = datetime.now() self.handled = False self.user_hash = user_hash @@ -58,23 +57,23 @@ class Paste(db.Model): with their unique hash and public with the paste id. """ if isinstance(identifier, basestring) and not identifier.isdigit(): - return Paste.query.filter(Paste.private_id == identifier).first() - return Paste.query.filter(db.and_( - Paste.paste_id == int(identifier), - Paste.private_id == None)).first() + query = Paste.query.filter_by(private_id=identifier) + else: + query = Paste.query.filter_by(paste_id=int(identifier)) + return query.first() @staticmethod def find_all(): """Return a query for all public pastes ordered by the id in reverse order. """ - return Paste.query.filter(Paste.private_id == None) \ - .order_by(Paste.paste_id.desc()) + return Paste.query.filter_by( + private_id=None).order_by(Paste.paste_id.desc()) @staticmethod def count(): """Count all pastes.""" - return Paste.query(Paste.paste_id).count() + return Paste.query.count() @staticmethod def resolve_root(identifier): @@ -91,18 +90,17 @@ class Paste(db.Model): """Get the new replies for the ower of a request and flag them as handled. """ - ids = db.session.query(Paste.paste_id) \ - .filter(Paste.user_hash == local.request.user_hash) - - paste_list = db.session.query(Paste.paste_id).filter(db.and_( + ids = [x.paste_id for x in Paste.query.filter_by( + user_hash=local.request.user_hash).all()] + paste_list = Paste.query.filter(db.and_( Paste.parent_id.in_(ids), Paste.handled == False, Paste.user_hash != local.request.user_hash, )).order_by(Paste.paste_id.desc()).all() to_mark = [p.paste_id for p in paste_list] - Paste.query.filter(Paste.paste_id.in_(to_mark)) \ - .update(values={'handled': True}) + Paste.query.filter(Paste.paste_id.in_(to_mark) + ).update(values={'handled': True}) db.session.commit() return paste_list @@ -112,14 +110,9 @@ class Paste(db.Model): def _set_private(self, value): if not value: self.private_id = None - return - if self.private_id is None: - while 1: - self.private_id = generate_paste_hash() - paste = Paste.query.filter(Paste.private_id == - self.private_id).first() - if paste is None: - break + elif self.private_id is None: + self.private_id = generate_paste_hash() + private = property(_get_private, _set_private, doc=''' The private status of the paste. If the paste is private it gets a unique hash as identifier, otherwise an integer. diff --git a/lodgeit/utils.py b/lodgeit/utils.py index 583a929..9a5a95c 100644 --- a/lodgeit/utils.py +++ b/lodgeit/utils.py @@ -55,7 +55,7 @@ def generate_paste_hash(): """Generates a more or less unique-truncated SHA1 hash.""" while 1: digest = sha1('%s|%s' % (random(), time.time())).digest() - val = _word_only(digest.encode('base64').strip().splitlines()[0])[:20] + val = _word_only(digest.encode('base64').strip())[:20] # sanity check. number only not allowed (though unlikely) if not val.isdigit(): return val diff --git a/lodgeit/views/layout.html b/lodgeit/views/layout.html index 721cf19..95e6cf4 100644 --- a/lodgeit/views/layout.html +++ b/lodgeit/views/layout.html @@ -1,10 +1,10 @@ +"http://www.w3.org/TR/html4/loose.dtd"> {{ page_title|e }} | LodgeIt! - - + + @@ -17,23 +17,27 @@
- + {# #} @@ -47,9 +51,9 @@ paste=paste.paste_id, paste_url=paste.url|e, parent_url=paste.parent.url|e %} on {{ date }} someone replied to your paste #{{ parent }}, - in paste #{{ paste }}. Click here to - compare - those two pastes.{% endtrans %} + in paste #{{ paste }}. Click here to {% endtrans %} + + {%- trans %}compare those two pastes{% endtrans %}.

{% endfor %}

{% trans %}hide this notification{% endtrans %}

diff --git a/lodgeit/views/not_found.html b/lodgeit/views/not_found.html index c7d2877..e29dffb 100644 --- a/lodgeit/views/not_found.html +++ b/lodgeit/views/not_found.html @@ -8,9 +8,12 @@ We've recently updated this pastebin. While it is out goal for nothing to get lost, you may have found a page that was mis-placed. Check your URL to ensure you have gone where you intended. If everything looks OK and you still see - this error page, please consider {% endtrans %}{% trans %} contacting us{% endtrans %}. + this error page, please consider {% endtrans -%} + {% trans %}contacting us{% endtrans %}.

- {% trans %}Click{% endtrans %} {% trans %}here{% endtrans %}{% trans %} to add a new paste.{% endtrans %} + {% trans %}Click {% endtrans -%} + {% trans %}here{% endtrans %} + {%- trans %} to add a new paste.{% endtrans %}

{% endblock %}