2016-04-02 12:13:26 +00:00
|
|
|
import datetime
|
|
|
|
import re
|
2016-04-24 14:34:06 +00:00
|
|
|
from sqlalchemy.inspection import inspect
|
2016-04-02 12:13:26 +00:00
|
|
|
from szurubooru.errors import ValidationError
|
2016-03-28 20:53:56 +00:00
|
|
|
|
2016-04-24 14:34:06 +00:00
|
|
|
def get_resource_info(entity):
|
|
|
|
serializers = {
|
|
|
|
'tag': lambda tag: tag.first_name,
|
|
|
|
'tag_category': lambda category: category.name,
|
|
|
|
'comment': lambda comment: comment.comment_id,
|
|
|
|
'post': lambda post: post.post_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
resource_type = entity.__table__.name
|
|
|
|
assert resource_type in serializers
|
|
|
|
|
|
|
|
primary_key = inspect(entity).identity
|
|
|
|
assert primary_key is not None
|
|
|
|
assert len(primary_key) == 1
|
|
|
|
|
|
|
|
resource_repr = serializers[resource_type](entity)
|
|
|
|
assert resource_repr
|
|
|
|
|
|
|
|
resource_id = primary_key[0]
|
|
|
|
assert resource_id
|
|
|
|
|
|
|
|
return (resource_type, resource_id, resource_repr)
|
|
|
|
|
2016-04-03 16:00:38 +00:00
|
|
|
def is_valid_email(email):
|
2016-04-03 20:03:58 +00:00
|
|
|
''' Return whether given email address is valid or empty. '''
|
2016-04-03 17:00:47 +00:00
|
|
|
return not email or re.match(r'^[^@]*@[^@]*\.[^@]*$', email)
|
2016-04-03 16:00:38 +00:00
|
|
|
|
2016-03-28 20:53:56 +00:00
|
|
|
class dotdict(dict): # pylint: disable=invalid-name
|
2016-04-03 16:00:38 +00:00
|
|
|
''' dot.notation access to dictionary attributes. '''
|
2016-03-28 20:53:56 +00:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return self.get(attr)
|
|
|
|
__setattr__ = dict.__setitem__
|
|
|
|
__delattr__ = dict.__delitem__
|
2016-04-02 12:13:26 +00:00
|
|
|
|
|
|
|
def parse_time_range(value, timezone=datetime.timezone(datetime.timedelta())):
|
2016-04-03 20:03:58 +00:00
|
|
|
''' Return tuple containing min/max time for given text representation. '''
|
2016-04-02 12:13:26 +00:00
|
|
|
one_day = datetime.timedelta(days=1)
|
|
|
|
one_second = datetime.timedelta(seconds=1)
|
|
|
|
|
|
|
|
value = value.lower()
|
|
|
|
if not value:
|
|
|
|
raise ValidationError('Empty date format.')
|
|
|
|
|
|
|
|
if value == 'today':
|
|
|
|
now = datetime.datetime.now(tz=timezone)
|
|
|
|
return (
|
|
|
|
datetime.datetime(now.year, now.month, now.day, 0, 0, 0),
|
2016-04-03 17:00:47 +00:00
|
|
|
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \
|
2016-04-02 12:13:26 +00:00
|
|
|
+ one_day - one_second)
|
|
|
|
|
|
|
|
if value == 'yesterday':
|
|
|
|
now = datetime.datetime.now(tz=timezone)
|
|
|
|
return (
|
|
|
|
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) - one_day,
|
2016-04-03 17:00:47 +00:00
|
|
|
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \
|
2016-04-02 12:13:26 +00:00
|
|
|
- one_second)
|
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
match = re.match(r'^(\d{4})$', value)
|
2016-04-02 12:13:26 +00:00
|
|
|
if match:
|
|
|
|
year = int(match.group(1))
|
|
|
|
return (
|
|
|
|
datetime.datetime(year, 1, 1),
|
|
|
|
datetime.datetime(year + 1, 1, 1) - one_second)
|
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
match = re.match(r'^(\d{4})-(\d{1,2})$', value)
|
2016-04-02 12:13:26 +00:00
|
|
|
if match:
|
|
|
|
year = int(match.group(1))
|
|
|
|
month = int(match.group(2))
|
|
|
|
return (
|
|
|
|
datetime.datetime(year, month, 1),
|
|
|
|
datetime.datetime(year, month + 1, 1) - one_second)
|
|
|
|
|
2016-04-03 17:00:47 +00:00
|
|
|
match = re.match(r'^(\d{4})-(\d{1,2})-(\d{1,2})$', value)
|
2016-04-02 12:13:26 +00:00
|
|
|
if match:
|
|
|
|
year = int(match.group(1))
|
|
|
|
month = int(match.group(2))
|
|
|
|
day = int(match.group(3))
|
|
|
|
return (
|
|
|
|
datetime.datetime(year, month, day),
|
|
|
|
datetime.datetime(year, month, day + 1) - one_second)
|
|
|
|
|
|
|
|
raise ValidationError('Invalid date format: %r.' % value)
|
2016-04-15 20:49:55 +00:00
|
|
|
|
|
|
|
def icase_unique(source):
|
|
|
|
target = []
|
|
|
|
target_low = []
|
|
|
|
for source_item in source:
|
|
|
|
if source_item.lower() not in target_low:
|
|
|
|
target.append(source_item)
|
|
|
|
target_low.append(source_item.lower())
|
|
|
|
return target
|
2016-04-16 13:07:33 +00:00
|
|
|
|
|
|
|
def value_exceeds_column_size(value, column):
|
|
|
|
return len(value) > column.property.columns[0].type.length
|