Skip to content

Instantly share code, notes, and snippets.

@whosaysni
whosaysni / gist:7685212
Last active December 29, 2015 14:39
POSTing CSRF-protected actions in Catalyst::Test
# common logic for *session_request.
sub _session_request {
my ($res, $path) = @_;
my $req = Catalyst::Utils::request($path);
$req->header('Cookie'=>$res->headers->{'set-cookie'});
return $req;
}
# build request with session, using given response.
sub session_request {
@whosaysni
whosaysni / gist:7816978
Last active December 30, 2015 10:39
IDとパスワードを本文から抜き出す
# coding: utf-8
import re
# いろいろなパターンを作るためのフォーマット
text_fmt = u"""
...
お客様の%(id_label)s/%(pw_label)sをご入力ください。
...
%(id_label_prefix)s%(id_label)s%(label_suffix)s%(sep)s%(value_prefix)s%(id)s
%(pw_label_prefix)s%(pw_label)s%(label_suffix)s%(sep)s%(value_prefix)s%(pw)s
@whosaysni
whosaysni / JsonSerializedDict
Created March 28, 2014 13:30
Dictionary with JSON-serialized internal buffer. Placed in the Public Domain.
from json import dumps, loads
from UserDict import DictMixin
class JsonSerializedDict(DictMixin):
"""
>>> JSD = JsonSerializedDict
>>> d = JSD()
>>> print d
{}
@whosaysni
whosaysni / get_secret_key.py
Created April 17, 2014 00:05
Generating SECRET_KEY on first run
import os, binascii
def get_secret_key(key_filename):
key_path = os.path.join(BASE_DIR, 'conf', key_filename)
if not os.path.exists(key_path):
try:
umask_saved = os.umask(0277)
with open(key_path, 'wb') as key_file:
key_file.write(binascii.b2a_base64(os.urandom(64)).rstrip('='))
finally:
os.umask(umask_saved)
@whosaysni
whosaysni / form.html
Created April 17, 2014 13:11
Confirmation/submit dialog with MetroUI css
<form id="delete_form"
method="POST" action="{% url 'delete_something' %}">
{% csrf_token %}
<div>
<input type="hidden" name="delete" value="">
<button id="delete_button" type="button">Delete</button>
<script>
$('#delete_button').on('click',
form_confirmation_factory('delete_form',
'Delete', 'Existing something will be removed.', 'OK', 'Cancel'))
@whosaysni
whosaysni / mytemplate.html
Created April 17, 2014 23:21
Padding blank row to last page with Django pagination
<table>
{% for obj in page %}
<tr><td>{{ obj.blah }}</td></tr>
{% endfor %}
{% ifequal page.number page.paginator.num_pages %}
{% for padding in page.paginator.paddngs %}
<tr><td> </td></tr>
{% endfor %}
{% endifequal %}
</table>
@whosaysni
whosaysni / add_user.html
Last active August 29, 2015 14:00
Template-only MetroUI-widgets-in-Django
<div class="container padding10">
<h1>Add user</h1>
<div class="padding10">
<form method="POST" action="{% url 'user_add' %}">
{% csrf_token %}
<fieldset>
{% with field=form.is_superuser type='switch' %}{% include "share/form_field.html" %}{% endwith %}
{% with field=form.is_active type='switch' %}{% include "share/form_field.html" %}{% endwith %}
{% with field=form.username type='text' %}{% include "share/form_field.html" %}{% endwith %}
{% with field=form.password type='password' %}{% include "share/form_field.html" %}{% endwith %}
@whosaysni
whosaysni / dbms_model.py
Last active August 29, 2015 14:00
Modelizing auxiliary DBMS using Django's multi-db framework
# coding: utf-8
"""外部DBMS
"""
import os
from functools import wraps
from json import loads
from logging import getLogger
from threading import local
@whosaysni
whosaysni / datatables.py
Last active August 29, 2015 14:01
Djangoで dataTables.js を使うためのクエリパーザ
# coding: utf-8
"""dataTables.js 用のクエリパーザ
"""
import re
class DataTablesQueryParser(object):
"""dataTables.js 用のクエリパーザ
"""
def __init__(self, request, column_map=dict(), method='POST'):
@whosaysni
whosaysni / matchcontext.py
Last active August 29, 2015 14:01
re の MatchObject を with のコンテキストにする
# coding: utf-8
class MatchContext(object):
"""
>>> import re
>>> with MatchContext(re.match(r'(.)', 'a')) as context:
... print context.groups()
('a',)
>>> import re
>>> with MatchContext(re.match(r'(.)', '')) as context: