예제 #1
0
파일: sql.py 프로젝트: hjalves/ines
def create_like_filter(column, value):
    value = maybe_unicode(value)
    if value:
        words = value.split()
        if words:
            like_str = u'%%%s%%' % u'%'.join(words)
            return column.like(like_str)
예제 #2
0
파일: sql.py 프로젝트: hjalves/ines
def create_rlike_filter(column, value):
    value = maybe_unicode(value)
    if value:
        words = value.split()
        if words:
            rlike_str = u'(%s)' % u'|'.join(words)
            return column.op('rlike')(rlike_str)
예제 #3
0
def validate_code(key, value, length, reverse=False, startswith=None):
    value = maybe_unicode(value)
    if not value:
        raise Error(key, _('Required'))

    number = value.strip().lower()
    if not number.isnumeric():
        raise Error(key, _('Need to be a number'))
    elif len(number) != length:
        raise Error(key, _('Need to have ${length} digits', mapping={'length': length}))

    if startswith and str(number[0]) not in startswith:
        startswith_str = '"%s"' % '", "'.join(startswith)
        raise Error(key, _('Need to start with ${chars}', mapping={'chars': startswith_str}))

    last_number = int(number[-1])
    number = number[:-1]

    if reverse:
        number_list = reversed(number)
    else:
        number_list = list(number)

    prime = find_next_prime(length)
    check_digit = prime - (sum(i * int(d) for i, d in enumerate(number_list, 2)) % prime)
    if last_number != check_digit:
        raise Error(key, _('Invalid number'))
    else:
        return value
예제 #4
0
파일: sql.py 프로젝트: hjalves/ines
def create_rlike_filter(column, value):
    value = maybe_unicode(value)
    if value:
        words = value.split()
        if words:
            rlike_str = u'(%s)' % u'|'.join(words)
            return column.op('rlike')(rlike_str)
예제 #5
0
파일: sql.py 프로젝트: hjalves/ines
def create_like_filter(column, value):
    value = maybe_unicode(value)
    if value:
        words = value.split()
        if words:
            like_str = u'%%%s%%' % u'%'.join(words)
            return column.like(like_str)
예제 #6
0
def create_rlike_filter(column, value):
    value = maybe_unicode(value)
    if value:
        words = value.split()
        if words:
            rlike_str = u('(%s)') % unicode_join('|', words)
            column = postgresql_non_ascii_and_lower(column)
            return column.op('rlike')(rlike_str.lower())
예제 #7
0
def create_ilike_filter(column, value):
    value = maybe_unicode(value)
    if value:
        words = value.split()
        if words:
            like_str = u('%%%s%%') % '%'.join(clean_unicode(w) for w in words)
            column = postgresql_non_ascii_and_lower(column)
            return column.ilike(like_str.lower())
예제 #8
0
def normalize_full_name(value):
    value = maybe_unicode(value)
    if value:
        words = []
        for word in clean_unicode(value, replace_case='_').lower().split():
            if word not in IGNORE_FULL_NAME_WORDS:
                words.append(word)
        if words:
            return unicode_join(' ', words)

    return u('')
예제 #9
0
def validate_url(key, value, required=False):
    url = maybe_unicode(value)
    if required and (not url or url in URL_PROTOCOLS):
        raise Error(key, 'Obrigatório')

    elif url and url not in URL_PROTOCOLS:
        for protocol in URL_PROTOCOLS:
            if url.startswith(protocol):
                if len(url[len(protocol):]) < 2:
                    raise Error(key, 'É necessário indicar um url')
                return url

        raise Error(key, 'O url tem de começar por "%s"' % '" ou "'.join(URL_PROTOCOLS))
예제 #10
0
    def save_file(
            self,
            binary,
            application_code,
            code_key=None,
            type_key=None,
            data=None,
            filename=None,
            title=None,
            parent_id=None):

        file_path = self.save_file_path(binary, filename)

        session_id = session_type = None
        if self.request.authenticated:
            session_id = self.request.authenticated.session_id
            session_type = self.request.authenticated.session_type

        # Add applications file relation
        new = File(
            file_id=file_path.id,
            parent_id=parent_id,
            key=make_unique_hash(70),
            application_code=to_unicode(application_code),
            code_key=maybe_unicode(code_key),
            type_key=maybe_unicode(type_key),
            data=maybe_unicode(data),
            filename=maybe_unicode(filename),
            title=maybe_unicode(title),
            session_type=session_type,
            session_id=session_id)
        self.session.add(new)
        self.session.flush()

        # Add some attributes
        new.size = file_path.size
        new.mimetype = file_path.mimetype
        return new
예제 #11
0
    def save_file_path(self, binary, filename=None, compressed=False):
        # Generate a unique code using SHA256
        if isinstance(binary, StorageFile):
            filename = filename or binary.name
            binary = binary.read()
        if isinstance(binary, (binary_type, string_types)):
            unique_code = string_unique_code(binary)
        else:
            unique_code = file_unique_code(binary)
            if not filename:
                filename = basename(binary.name)

        lock_key = 'storage save %s' % unique_code
        self.cache.lock(lock_key)
        try:
            file_path = (
                self.session
                .query(*FilePath.__table__.c.values())
                .filter(FilePath.code == unique_code)
                .first())

            # Save file if dont exists
            if not file_path:
                # Create a new filepath
                filename = maybe_string(filename)
                mimetype = find_mimetype(filename=filename, header_or_file=binary)
                file_path = FilePath(code=unique_code, mimetype=maybe_unicode(mimetype), compressed=compressed)

                to_add = []
                file_size = 0

                # Save blocks
                blocks = self.save_blocks(binary)

                for order, (block_id, block_size) in enumerate(blocks):
                    to_add.append(FileBlock(file_id_block=block_id, order=order))
                    file_size += block_size

                # Add file path to DB
                file_path.size = file_size
                file_path.id = self.direct_insert(file_path).inserted_primary_key[0]

                # Relate blocks and file path
                for block_relation in to_add:
                    block_relation.file_id_path = file_path.id
                    self.direct_insert(block_relation)

            return file_path
        finally:
            self.cache.unlock(lock_key)
예제 #12
0
def get_url_info(*args, **kwargs):
    headers = get_url_headers(*args, **kwargs)
    return {
        'size': maybe_integer(headers.get('Content-Length')) or 0,
        'type': maybe_unicode(headers.get('Content-Type')),
        'updated': guess_datetime(headers.get('Last-Modified'))}
예제 #13
0
파일: utils.py 프로젝트: hjalves/ines
from colander import Invalid
from pyramid.httpexceptions import HTTPError
from translationstring import TranslationString

from ines.convert import camelcase
from ines.convert import force_string
from ines.convert import force_unicode
from ines.convert import make_sha256
from ines.convert import maybe_unicode
from ines.i18n import translate_factory


NOW_DATE = datetime.datetime.now
PROCESS_ID = getpid()
DOMAIN_NAME = maybe_unicode(getfqdn())

# See: http://www.regular-expressions.info/email.html
EMAIL_REGEX = regex_compile(
    "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"
    "@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")


class WarningDict(dict):
    def __init__(self, message='Duplicate item "{key}" with value "{value}"'):
        self.message = message

    def __setitem__(self, key, value):
        if key in self:
            warnings.warn(
                self.message.format(key=key, value=value),