general cleanup; move binding_filler helper

This commit is contained in:
voussoir 2016-12-20 21:33:14 -08:00
parent af40f24dd8
commit 69443d7a8c
8 changed files with 53 additions and 40 deletions

View file

@ -182,10 +182,10 @@ def post_login():
username = request.form['username'] username = request.form['username']
password = request.form['password'] password = request.form['password']
user = P.get_user(username=username)
try: try:
user = P.get_user(username=username)
user = P.login(user.id, password) user = P.login(user.id, password)
except exceptions.WrongLogin: except (exceptions.NoSuchUser, exceptions.WrongLogin):
flask.abort(422, 'Wrong login.') flask.abort(422, 'Wrong login.')
session = sessions.Session(request, user) session = sessions.Session(request, user)
session_manager.add(session) session_manager.add(session)

View file

@ -33,6 +33,9 @@ class UserExists(Exception):
# TAG ERRORS # TAG ERRORS
class CantSynonymSelf(Exception):
pass
class RecursiveGrouping(Exception): class RecursiveGrouping(Exception):
pass pass
@ -42,9 +45,6 @@ class TagTooLong(Exception):
class TagTooShort(Exception): class TagTooShort(Exception):
pass pass
class CantSynonymSelf(Exception):
pass
# USER ERRORS # USER ERRORS
class InvalidUsernameChars(Exception): class InvalidUsernameChars(Exception):

View file

@ -50,6 +50,25 @@ def album_zip_filenames(album, recursive=True):
return arcnames return arcnames
def binding_filler(column_names, values, require_all=True):
'''
Manually aligning question marks and bindings is annoying.
Given the table's column names and a dictionary of {column: value},
return the question marks and the list of bindings in the right order.
'''
values = values.copy()
for column in column_names:
if column in values:
continue
if require_all:
raise ValueError('Missing column "%s"' % column)
else:
values.setdefault(column, None)
qmarks = '?' * len(column_names)
qmarks = ', '.join(qmarks)
bindings = [values[column] for column in column_names]
return (qmarks, bindings)
def chunk_sequence(sequence, chunk_length, allow_incomplete=True): def chunk_sequence(sequence, chunk_length, allow_incomplete=True):
''' '''
Given a sequence, divide it into sequences of length `chunk_length`. Given a sequence, divide it into sequences of length `chunk_length`.

View file

@ -281,23 +281,24 @@ class Photo(ObjectBase):
if isinstance(row_tuple, (list, tuple)): if isinstance(row_tuple, (list, tuple)):
row_tuple = {constants.SQL_PHOTO_COLUMNS[index]: value for (index, value) in enumerate(row_tuple)} row_tuple = {constants.SQL_PHOTO_COLUMNS[index]: value for (index, value) in enumerate(row_tuple)}
self.id = row_tuple['id'] self.real_filepath = helpers.normalize_filepath(row_tuple['filepath'], allowed=':\\/')
self.real_filepath = row_tuple['filepath']
self.real_filepath = helpers.normalize_filepath(self.real_filepath, allowed=':\\/')
self.real_path = pathclass.Path(self.real_filepath) self.real_path = pathclass.Path(self.real_filepath)
self.filepath = row_tuple['override_filename'] or self.real_filepath
self.basename = row_tuple['override_filename'] or os.path.basename(self.real_filepath) self.id = row_tuple['id']
self.created = row_tuple['created']
self.author_id = row_tuple['author_id']
self.filepath = row_tuple['override_filename'] or self.real_path.absolute_path
self.basename = row_tuple['override_filename'] or self.real_path.basename
self.extension = row_tuple['extension'] self.extension = row_tuple['extension']
self.width = row_tuple['width'] self.tagged_at = row_tuple['tagged_at']
self.height = row_tuple['height']
self.ratio = row_tuple['ratio']
self.area = row_tuple['area'] self.area = row_tuple['area']
self.bytes = row_tuple['bytes'] self.bytes = row_tuple['bytes']
self.duration = row_tuple['duration'] self.duration = row_tuple['duration']
self.created = row_tuple['created'] self.width = row_tuple['width']
self.height = row_tuple['height']
self.ratio = row_tuple['ratio']
self.thumbnail = row_tuple['thumbnail'] self.thumbnail = row_tuple['thumbnail']
self.tagged_at = row_tuple['tagged_at']
self.author_id = row_tuple['author_id']
def __reinit__(self): def __reinit__(self):
''' '''

View file

@ -124,25 +124,6 @@ def _helper_filenamefilter(subject, terms):
basename = subject.lower() basename = subject.lower()
return all(term in basename for term in terms) return all(term in basename for term in terms)
def binding_filler(column_names, values, require_all=True):
'''
Manually aligning question marks and bindings is annoying.
Given the table's column names and a dictionary of {column: value},
return the question marks and the list of bindings in the right order.
'''
values = values.copy()
for column in column_names:
if column in values:
continue
if require_all:
raise ValueError('Missing column "%s"' % column)
else:
values.setdefault(column, None)
qmarks = '?' * len(column_names)
qmarks = ', '.join(qmarks)
bindings = [values[column] for column in column_names]
return (qmarks, bindings)
def operate(operand_stack, operator_stack): def operate(operand_stack, operator_stack):
#print('before:', operand_stack, operator_stack) #print('before:', operand_stack, operator_stack)
operator = operator_stack.pop() operator = operator_stack.pop()
@ -379,7 +360,7 @@ class PDBAlbumMixin:
'associated_directory': associated_directory, 'associated_directory': associated_directory,
} }
(qmarks, bindings) = binding_filler(constants.SQL_ALBUM_COLUMNS, data) (qmarks, bindings) = helpers.binding_filler(constants.SQL_ALBUM_COLUMNS, data)
query = 'INSERT INTO albums VALUES(%s)' % qmarks query = 'INSERT INTO albums VALUES(%s)' % qmarks
self.cur.execute(query, bindings) self.cur.execute(query, bindings)
@ -496,7 +477,7 @@ class PDBPhotoMixin:
'thumbnail': None, 'thumbnail': None,
} }
(qmarks, bindings) = binding_filler(constants.SQL_PHOTO_COLUMNS, data) (qmarks, bindings) = helpers.binding_filler(constants.SQL_PHOTO_COLUMNS, data)
query = 'INSERT INTO photos VALUES(%s)' % qmarks query = 'INSERT INTO photos VALUES(%s)' % qmarks
self.cur.execute(query, bindings) self.cur.execute(query, bindings)
photo = objects.Photo(self, data) photo = objects.Photo(self, data)
@ -937,7 +918,7 @@ class PDBUserMixin:
'created': created, 'created': created,
} }
(qmarks, bindings) = binding_filler(constants.SQL_USER_COLUMNS, data) (qmarks, bindings) = helpers.binding_filler(constants.SQL_USER_COLUMNS, data)
query = 'INSERT INTO users VALUES(%s)' % qmarks query = 'INSERT INTO users VALUES(%s)' % qmarks
self.cur.execute(query, bindings) self.cur.execute(query, bindings)

View file

@ -53,7 +53,7 @@ button
<div id="login_register_box"> <div id="login_register_box">
<form id="login_form" action="/login" method="post"> <form id="login_form" action="/login" method="post">
<span>Log in</span> <span>Log in</span>
<input type="text" name="username" placeholder="username"> <input type="text" name="username" placeholder="username" autofocus>
<input type="password" name="password" placeholder="password"> <input type="password" name="password" placeholder="password">
<button type="submit">Log in</button> <button type="submit">Log in</button>
</form> </form>

View file

@ -215,22 +215,34 @@ function move_hoverzoom(event)
{ {
var x; var x;
var y; var y;
// Adding 5% to perceived position gives us a bit of padding around the image,
// so you don't need to navigate a 1px line to see the edge.
// We first subtract half of the image dimensions so that the 5% is applied
// to both left and right. Otherwise 105% of 0 is still 0 which doesn't
// apply padding on the left.
var mouse_x = event.offsetX; var mouse_x = event.offsetX;
mouse_x -= (photo_img_holder.offsetWidth / 2); mouse_x -= (photo_img_holder.offsetWidth / 2);
mouse_x *= 1.05; mouse_x *= 1.05;
mouse_x += (photo_img_holder.offsetWidth / 2); mouse_x += (photo_img_holder.offsetWidth / 2);
var mouse_y = event.offsetY; var mouse_y = event.offsetY;
mouse_y -= (photo_img_holder.offsetHeight / 2); mouse_y -= (photo_img_holder.offsetHeight / 2);
mouse_y *= 1.05; mouse_y *= 1.05;
mouse_y += (photo_img_holder.offsetHeight / 2); mouse_y += (photo_img_holder.offsetHeight / 2);
if (photo_img.naturalWidth < photo_img_holder.offsetWidth) if (photo_img.naturalWidth < photo_img_holder.offsetWidth)
{ {
// If the image is smaller than the frame, just center it
x = (photo_img.naturalWidth - photo_img_holder.offsetWidth) / 2; x = (photo_img.naturalWidth - photo_img_holder.offsetWidth) / 2;
} }
else else
{ {
// Take the amount of movement necessary (frame width - image width)
// times our distance across the image as a percentage.
x = (photo_img.naturalWidth - photo_img_holder.offsetWidth) * (mouse_x / photo_img_holder.offsetWidth); x = (photo_img.naturalWidth - photo_img_holder.offsetWidth) * (mouse_x / photo_img_holder.offsetWidth);
} }
if (photo_img.naturalHeight < photo_img_holder.offsetHeight) if (photo_img.naturalHeight < photo_img_holder.offsetHeight)
{ {
y = (photo_img.naturalHeight - photo_img_holder.offsetHeight) / 2; y = (photo_img.naturalHeight - photo_img_holder.offsetHeight) / 2;

View file

@ -37,7 +37,7 @@
</a> </a>
</div> </div>
<div class="photo_card_grid_info"> <div class="photo_card_grid_info">
<a target="_blank" href="/photo/{{photo.id}}">{{photo.basename}}</a> <a target="_blank" href="/photo/{{photo.id}}" style="word-break:break-all">{{photo.basename}}</a>
<span class="photo_card_grid_file_metadata"> <span class="photo_card_grid_file_metadata">
{% if photo.width %} {% if photo.width %}
{{photo.width}}x{{photo.height}}, {{photo.width}}x{{photo.height}},