ndarville / pony-forum

A modern alternative to ancient forum CMSes like vBulletin and PHPBB in Python on Django. (Alpha stage.) (NB: dotCloud have since removed their free Sandbox tier.)
http://pony-forum.com
26 stars 7 forks source link

Rewrite `except: pass` antipatterns #113

Open ndarville opened 11 years ago

ndarville commented 11 years ago



Examples

views.py

create()

user      = request.user
now       = datetime.datetime.now()  # UTC?
text_html = sanitized_smartdown(text_plain)
try:
    t = Thread.objects.create(
        title_plain=title_plain, title_html=title_html,
        author=user, category=category,
        creation_date=now, latest_reply_date=now)
    Post.objects.create(
        thread=t, creation_date=now, author=user,
        content_plain=text_plain, content_html=text_html)
    t.subscriber.add(user)
except:
    pass
else:  # After successful submission
    category.thread_count += 1
    category.post_count += 1
    t.post_count += 1
    request.user.get_profile().thread_count += 1
    request.user.get_profile().post_count += 1
    category.save()
    t.save()
    request.user.get_profile().save()
    return HttpResponseRedirect(reverse('forum.views.thread', args=(t.id,)))

add()

try:
    thread.title_plain = request.POST['title']
    thread.title_html  = prettify_title(thread.title_plain)
    thread.save()
except:
    pass
else:
    return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))

report()

user = request.user
now  = datetime.datetime.now()  # UTC?
try:
    r = Report.objects.create(
        creation_date=now, author=user,
        reason_short=title, thread=thread)
    if "content" in request.POST:
        r.reason_long_plain = text_plain
        r.reason_long_html  = text_html
    if object_type == "post":
        r.post = obj
    r.save()
except:
    pass
else:
    # After successful submission
    return HttpResponseRedirect(reverse('forum.views.thread',
                                        args=(thread.id,)))

settings.py

#ADMINS = (
#    try:
#        # ('Your Name', 'your_email@example.com'),
#        (env['ADMIN_NAME'], env['ADMIN_EMAIL']),
#    except NameError, KeyError:
#        pass
#)
#
#MANAGERS = ADMINS

https://github.com/ndarville/pony-forum/blob/master/ponyforum/settings.py#L294-300

try:
    LOCAL_SETTINGS
except NameError:
    try:
        from local_settings import *
    except ImportError:
        pass