Python django.db.DataError() Examples

The following are 18 code examples of django.db.DataError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db , or try the search function .
Example #1
Source File: court_processor.py    From oldp with MIT License 6 votes vote down vote up
def process_content(self):
        for content in self.pre_processed_content:  # type: Court
            try:
                # First save (some processing steps require ids)
                content.full_clean()  # Validate model
                content.save()

                self.call_processing_steps(content)

                # Save again
                content.save()

                logger.debug('Completed: %s' % content)

                self.doc_counter += 1
                self.processed_content.append(content)

            except (ValidationError, DataError, OperationalError, IntegrityError, ProcessingError) as e:
                logger.error('Cannot process court: %s; %s' % (content, e))
                self.processing_errors.append(e)
                self.doc_failed_counter += 1 
Example #2
Source File: case_processor.py    From oldp with MIT License 6 votes vote down vote up
def process_content_item(self, content: Case) -> Case:
        try:
            # First save (some processing steps require ids)
            # content.full_clean()  # Validate model
            content.save()

            self.call_processing_steps(content)

            # Save again
            content.save()

            logger.debug('Completed: %s' % content)

            self.doc_counter += 1
            self.processed_content.append(content)

        except (ValidationError, DataError, OperationalError, IntegrityError, ProcessingError) as e:
            logger.error('Cannot process case: %s; %s' % (content, e))
            self.processing_errors.append(e)
            self.doc_failed_counter += 1

        return content 
Example #3
Source File: reference_processor.py    From oldp with MIT License 6 votes vote down vote up
def process_content(self):
        for content in self.pre_processed_content:  # type: Reference
            try:
                # First save (some processing steps require ids)
                content.full_clean()  # Validate model
                content.save()

                self.call_processing_steps(content)

                # Save again
                content.save()

                logger.debug('Completed: %s' % content)

                self.doc_counter += 1
                self.processed_content.append(content)

            except (ValidationError, DataError, OperationalError, IntegrityError, ProcessingError) as e:
                logger.error('Cannot process: %s; %s' % (content, e))
                self.processing_errors.append(e)
                self.doc_failed_counter += 1 
Example #4
Source File: operation.py    From HttpRunnerManager with MIT License 6 votes vote down vote up
def add_register_data(**kwargs):
    """
    用户注册信息逻辑判断及落地
    :param kwargs: dict
    :return: ok or tips
    """
    user_info = UserInfo.objects
    try:
        username = kwargs.pop('account')
        password = kwargs.pop('password')
        email = kwargs.pop('email')

        if user_info.filter(username__exact=username).filter(status=1).count() > 0:
            logger.debug('{username} 已被其他用户注册'.format(username=username))
            return '该用户名已被注册,请更换用户名'
        if user_info.filter(email__exact=email).filter(status=1).count() > 0:
            logger.debug('{email} 昵称已被其他用户注册'.format(email=email))
            return '邮箱已被其他用户注册,请更换邮箱'
        user_info.create(username=username, password=password, email=email)
        logger.info('新增用户:{user_info}'.format(user_info=user_info))
        return 'ok'
    except DataError:
        logger.error('信息输入有误:{user_info}'.format(user_info=user_info))
        return '字段长度超长,请重新编辑' 
Example #5
Source File: test_filters.py    From karrot-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_fails_if_date_max_less_than_date_min(self):
        now = timezone.now()
        qs = Activity.objects.filter(place=self.place)
        with self.assertRaises(DataError):
            list(ActivitiesFilter(data={
                'date_min': now,
                'date_max': now - timedelta(hours=1),
            }, queryset=qs).qs) 
Example #6
Source File: test_models.py    From oldp with MIT License 5 votes vote down vote up
def test_field_validation(self):

        try:
            # max_length
            t = 'A' * 201
            c = Case(title=t, file_number=t)
            c.save()

            raise ValueError('DataError should have been raised.')
        except DataError:
            pass 
Example #7
Source File: validators.py    From Dailyfresh-B2C with Apache License 2.0 5 votes vote down vote up
def qs_filter(queryset, **kwargs):
    try:
        return queryset.filter(**kwargs)
    except (TypeError, ValueError, DataError):
        return queryset.none() 
Example #8
Source File: validators.py    From Dailyfresh-B2C with Apache License 2.0 5 votes vote down vote up
def qs_exists(queryset):
    try:
        return queryset.exists()
    except (TypeError, ValueError, DataError):
        return False 
Example #9
Source File: operation.py    From HttpRunnerManager with MIT License 5 votes vote down vote up
def add_config_data(type, **kwargs):
    """
    配置信息落地
    :param type: boolean: true: 添加新配置, fasle: 更新配置
    :param kwargs: dict
    :return: ok or tips
    """
    case_opt = TestCaseInfo.objects
    config_info = kwargs.get('config').get('config_info')
    name = kwargs.get('config').get('name')
    module = config_info.get('module')
    project = config_info.get('project')
    belong_module = ModuleInfo.objects.get_module_name(module, type=False)

    try:
        if type:
            if case_opt.get_case_name(name, module, project) < 1:
                case_opt.insert_config(belong_module, **kwargs)
                logger.info('{name}配置添加成功: {kwargs}'.format(name=name, kwargs=kwargs))
            else:
                return '用例或配置已存在,请重新编辑'
        else:
            index = config_info.get('test_index')
            if name != case_opt.get_case_by_id(index, type=False) \
                    and case_opt.get_case_name(name, module, project) > 0:
                return '用例或配置已在该模块中存在,请重新命名'
            case_opt.update_config(belong_module, **kwargs)
            logger.info('{name}配置更新成功: {kwargs}'.format(name=name, kwargs=kwargs))
    except DataError:
        logger.error('{name}配置信息过长:{kwargs}'.format(name=name, kwargs=kwargs))
        return '字段长度超长,请重新编辑'
    return 'ok' 
Example #10
Source File: operation.py    From HttpRunnerManager with MIT License 5 votes vote down vote up
def add_case_data(type, **kwargs):
    """
    用例信息落地
    :param type: boolean: true: 添加新用例, false: 更新用例
    :param kwargs: dict
    :return: ok or tips
    """
    case_info = kwargs.get('test').get('case_info')
    case_opt = TestCaseInfo.objects
    name = kwargs.get('test').get('name')
    module = case_info.get('module')
    project = case_info.get('project')
    belong_module = ModuleInfo.objects.get_module_name(module, type=False)
    config = case_info.get('config', '')
    if config != '':
        case_info.get('include')[0] = eval(config)

    try:
        if type:

            if case_opt.get_case_name(name, module, project) < 1:
                case_opt.insert_case(belong_module, **kwargs)
                logger.info('{name}用例添加成功: {kwargs}'.format(name=name, kwargs=kwargs))
            else:
                return '用例或配置已存在,请重新编辑'
        else:
            index = case_info.get('test_index')
            if name != case_opt.get_case_by_id(index, type=False) \
                    and case_opt.get_case_name(name, module, project) > 0:
                return '用例或配置已在该模块中存在,请重新命名'
            case_opt.update_case(belong_module, **kwargs)
            logger.info('{name}用例更新成功: {kwargs}'.format(name=name, kwargs=kwargs))

    except DataError:
        logger.error('用例信息:{kwargs}过长!!'.format(kwargs=kwargs))
        return '字段长度超长,请重新编辑'
    return 'ok' 
Example #11
Source File: operation.py    From HttpRunnerManager with MIT License 5 votes vote down vote up
def add_project_data(type, **kwargs):
    """
    项目信息落地 新建时必须默认添加debugtalk.py
    :param type: true: 新增, false: 更新
    :param kwargs: dict
    :return: ok or tips
    """
    project_opt = ProjectInfo.objects
    project_name = kwargs.get('project_name')
    if type:
        if project_opt.get_pro_name(project_name) < 1:
            try:
                project_opt.insert_project(**kwargs)
                belong_project = project_opt.get(project_name=project_name)
                DebugTalk.objects.create(belong_project=belong_project, debugtalk='# debugtalk.py')
            except DataError:
                return '项目信息过长'
            except Exception:
                logging.error('项目添加异常:{kwargs}'.format(kwargs=kwargs))
                return '添加失败,请重试'
            logger.info('项目添加成功:{kwargs}'.format(kwargs=kwargs))
        else:
            return '该项目已存在,请重新编辑'
    else:
        if project_name != project_opt.get_pro_name('', type=False, id=kwargs.get(
                'index')) and project_opt.get_pro_name(project_name) > 0:
            return '该项目已存在, 请重新命名'
        try:
            project_opt.update_project(kwargs.pop('index'), **kwargs)  # testcaseinfo的belong_project也得更新,这个字段设计的有点坑了
        except DataError:
            return '项目信息过长'
        except Exception:
            logging.error('更新失败:{kwargs}'.format(kwargs=kwargs))
            return '更新失败,请重试'
        logger.info('项目更新成功:{kwargs}'.format(kwargs=kwargs))

    return 'ok' 
Example #12
Source File: models.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def from_bare(cls, bare_obj):
        """
        Saves a paper to the database if it is not already present.
        The clustering algorithm is run to decide what authors should be
        attributed to the paper.

        :returns: the :class:`Paper` instance created from the bare paper supplied.
        """
        paper = bare_obj
        try:
            # Look up the fingerprint
            fp = paper.fingerprint
            matches = Paper.find_by_fingerprint(fp)

            p = None
            if matches:  # We have found a paper matching the fingerprint
                p = matches[0]
                if paper.visible and not p.visible:
                    p.visible = True
                p.update_authors(
                        paper.authors,
                        save_now=False)
                for record in paper.oairecords:
                    p.add_oairecord(record)
                p.update_availability()  # in Paper, this saves to the db
            else:  # Otherwise we create a new paper
                # this already saves the paper in the db
                p = super(Paper, cls).from_bare(paper)

            return p

        except DataError as e:
            raise ValueError(
                'Invalid paper, does not fit in the database schema:\n'+str(e))

    ### Other methods, specific to this non-bare subclass ### 
Example #13
Source File: test_models.py    From karrot-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_create_fails_if_name_too_long(self):
        with self.assertRaises(DataError):
            Place.objects.create(name='a' * 81, group=self.group) 
Example #14
Source File: test_model.py    From karrot-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_create_fails_if_name_too_long(self):
        with self.assertRaises(DataError):
            too_long = self.exampleuser
            too_long['display_name'] = 'a' * 81
            get_user_model().objects.create_user(**too_long) 
Example #15
Source File: test_models.py    From karrot-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_create_fails_if_comment_too_long(self):
        with self.assertRaises(DataError):
            Feedback.objects.create(comment='a' * 100001, about=self.activity, given_by=self.user, weight=1) 
Example #16
Source File: test_model.py    From karrot-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_create_fails_if_name_too_long(self):
        with self.assertRaises(DataError):
            Group.objects.create(name='a' * 81) 
Example #17
Source File: operation.py    From HttpRunnerManager with MIT License 4 votes vote down vote up
def add_module_data(type, **kwargs):
    """
    模块信息落地
    :param type: boolean: true: 新增, false: 更新
    :param kwargs: dict
    :return: ok or tips
    """
    module_opt = ModuleInfo.objects
    belong_project = kwargs.pop('belong_project')
    module_name = kwargs.get('module_name')
    if type:
        if module_opt.filter(belong_project__project_name__exact=belong_project) \
                .filter(module_name__exact=module_name).count() < 1:
            try:
                belong_project = ProjectInfo.objects.get_pro_name(belong_project, type=False)
            except ObjectDoesNotExist:
                logging.error('项目信息读取失败:{belong_project}'.format(belong_project=belong_project))
                return '项目信息读取失败,请重试'
            kwargs['belong_project'] = belong_project
            try:
                module_opt.insert_module(**kwargs)
            except DataError:
                return '模块信息过长'
            except Exception:
                logging.error('模块添加异常:{kwargs}'.format(kwargs=kwargs))
                return '添加失败,请重试'
            logger.info('模块添加成功:{kwargs}'.format(kwargs=kwargs))
        else:
            return '该模块已在项目中存在,请重新编辑'
    else:
        if module_name != module_opt.get_module_name('', type=False, id=kwargs.get('index')) \
                and module_opt.filter(belong_project__project_name__exact=belong_project) \
                .filter(module_name__exact=module_name).count() > 0:
            return '该模块已存在,请重新命名'
        try:
            module_opt.update_module(kwargs.pop('index'), **kwargs)
        except DataError:
            return '模块信息过长'
        except Exception:
            logging.error('更新失败:{kwargs}'.format(kwargs=kwargs))
            return '更新失败,请重试'
        logger.info('模块更新成功:{kwargs}'.format(kwargs=kwargs))
    return 'ok' 
Example #18
Source File: models.py    From dissemin with GNU Affero General Public License v3.0 4 votes vote down vote up
def create(cls, dct):
        """
        Given a dictionary representing an institution,
        as returned by OrcidProfile.institution,
        save the institution in the DB (or return
        an existing duplicate).

        The dictionary should contain 'name' and 'country'
        values, and possibly an identifier (such as a Ringgold one).

        Returns None if the institution is invalid
        (invalid country code, name too long…)
        """
        # cleanup arguments
        dct['name'] = dct['name'].strip()
        dct['country'] = dct['country'].strip()
        # django_countries does not validate the countries
        # against the list it contains (!?)
        if dct['country'] not in dict(countries):
            return

        # create a list of identifiers for this institution
        identifiers = []
        # add the 'fingerprint' for that institution
        identifiers.append(
            dct['country']+':'+iunaccent(dct['name']))
        # maybe add a disambiguated identifier
        if dct.get('identifier'):
            identifiers.append(
                dct.get('identifier'))

        # this uses our GIN index on identitfiers
        matches = Institution.objects.filter(
            identifiers__overlap=identifiers)

        try:
            if not matches:
                i = cls(
                    name=dct['name'],
                    country=dct['country'],
                    identifiers=identifiers)
                i.save()
                return i
            else:
                # TODO case where there are multiple matches : merge
                i = matches[0]
                old_identifiers = set(i.identifiers or [])
                new_identifiers = old_identifiers | set(identifiers)
                if old_identifiers != new_identifiers:
                    i.identifiers = list(new_identifiers)

                    # shorter names are better
                    if len(dct['name']) < len(i.name):
                        i.name = dct['name']
                    i.save()
                return i
        except DataError: # did not fit in the DB
            pass