def type_id(self):
     """
     Shortcut to retrieving the ContentType id of the model.
     """
     try:
         return ContentType.objects.get_for_model(self.model, for_concrete_model=False).id
     except DatabaseError as e:
         raise DatabaseError(
             "Unable to fetch ContentType object, is a plugin being registered before the initial syncdb? (original error: {})".format(
                 str(e)
             )
         )
示例#2
0
 def fetch_returned_insert_columns(self, cursor, returning_params):
     columns = []
     for param in returning_params:
         value = param.get_value()
         if value == []:
             raise DatabaseError(
                 "The database did not return a new row id. Probably "
                 '"ORA-1403: no data found" was raised internally but was '
                 "hidden by the Oracle OCI library (see "
                 "https://code.djangoproject.com/ticket/28859).")
         columns.append(value[0])
     return tuple(columns)
示例#3
0
文件: operations.py 项目: xg9/django
 def fetch_returned_insert_columns(self, cursor, returning_params):
     for param in returning_params:
         value = param.get_value()
         if value is None or value == []:
             # cx_Oracle < 6.3 returns None, >= 6.3 returns empty list.
             raise DatabaseError(
                 'The database did not return a new row id. Probably '
                 '"ORA-1403: no data found" was raised internally but was '
                 'hidden by the Oracle OCI library (see '
                 'https://code.djangoproject.com/ticket/28859).')
         # cx_Oracle < 7 returns value, >= 7 returns list with single value.
         yield value[0] if isinstance(value, list) else value
示例#4
0
def coerce_unicode(value):
    if isinstance(value, str):
        try:
            value = value.decode('utf-8')
        except UnicodeDecodeError:
            # This must be a Django databaseerror, because the exception happens too
            # early before Django's exception wrapping can take effect (e.g. it happens on SQL
            # construction, not on execution.
            raise DatabaseError("Bytestring is not encoded in utf-8")

    # The SDK raises BadValueError for unicode sub-classes like SafeText.
    return unicode(value)
示例#5
0
 def db_for_write(self, model, **hints):
     """
     Raises an error for attempts to write to the live Sierra DB;
     otherwise, routes TEST access on `base` models to the test
     Sierra DB and all others to the default DB.
     """
     if model._meta.app_label == 'base':
         if settings.TESTING:
             return 'sierra'
         raise DatabaseError('Attempted to write to Sierra database.')
     else:
         return 'default'
示例#6
0
 def handle(self, *args, **options):
     if settings.DATABASES['default'][
             'ENGINE'] != "zappa_django_utils.db.backends.s3sqlite":
         raise DatabaseError(
             'This command is only for the s3sqlite Django DB engine.')
     else:
         self.stdout.write(
             self.style.SUCCESS('Starting database VACUUM...'))
         cursor = connection.cursor()
         cursor.execute('VACUUM;')
         cursor.close()
         self.stdout.write(self.style.SUCCESS('VACUUM complete.'))
示例#7
0
 def delete_payment(self, id):
     if id is None:
         raise ValueError("Id is required")
     try:
         from product.models import PaymentHistory
         payment = PaymentHistory.objects.get(id=id)
         order = payment.order
         order.paid_total = order.paid_total - payment.amount
         order.save(using=self.db)
         payment.delete()
     except DatabaseError as e:
         raise DatabaseError("Technical problem to delete order")
     return True
示例#8
0
 def type_id(self):
     """
     Shortcut to retrieving the ContentType id of the model.
     """
     if self._type_id is None:
         try:
             self._type_id = ContentType.objects.get_for_model(
                 self.model).id
         except DatabaseError as e:
             raise DatabaseError(
                 "Unable to fetch ContentType object, is a plugin being registered before the initial syncdb? (original error: {0})"
                 .format(str(e)))
     return self._type_id
示例#9
0
 def test_clone_schema_exception(self):
     """
     Test that a DatabaseError is raised from within the call is logged and handled
     """
     tst_schema = "test_clone_schema_exception"
     expected = 'ERROR:api.iam.models:Exception DatabaseError cloning "{}" to "{}": Too Many Quatloos'.format(
         Tenant._TEMPLATE_SCHEMA, tst_schema)
     with patch("api.iam.models.Tenant._clone_schema",
                side_effect=DatabaseError("Too Many Quatloos")):
         with self.assertLogs("api.iam.models", level="INFO") as _logger:
             with self.assertRaises(DatabaseError):
                 Tenant(schema_name=tst_schema).create_schema()
                 self.assertIn(expected, _logger.output)
示例#10
0
    def test_check_size_with_database_error(self, m_mock):
        """
        Test if with 'broken scenario', all goes bad
        """
        m_mock.side_effect = DatabaseError('Boom')

        informer = DatabaseInformer()

        expected = (False, 'Oh no. Your database is out!')

        result = informer.check_availability()

        self.assertTupleEqual(expected, result)
 def type_id(self):
     """
     Returns the :class:`~django.contrib.contenttypes.models.ContentType` id of the model.
     """
     if self._type_id is None:
         try:
             self._type_id = ContentType.objects.get_for_model(
                 self.model).id
         except DatabaseError as e:
             raise DatabaseError(
                 "Unable to fetch ContentType object, is a plugin being registered before the initial syncdb? (original error: {0})"
                 .format(str(e)))
     return self._type_id
示例#12
0
def test_databases_exist():
    from django.db import DatabaseError

    connections = get_databases()
    try:
        for connection in connections:
            if connection.settings_dict['NAME'] == 'sqlite3':
                if not os.path.exists(connection.settings_dict['DATABASE_NAME']):
                    raise DatabaseError()
            connection.cursor()

        return True
    except DatabaseError, err:
        return False
示例#13
0
 def validate_thread_sharing(self):
     """
     Validate that the connection isn't accessed by another thread than the
     one which originally created it, unless the connection was explicitly
     authorized to be shared between threads (via the `inc_thread_sharing()`
     method). Raise an exception if the validation fails.
     """
     if not (self.allow_thread_sharing or self._thread_ident == _thread.get_ident()):
         raise DatabaseError(
             "DatabaseWrapper objects created in a "
             "thread can only be used in that same thread. The object "
             "with alias '%s' was created in thread id %s and this is "
             "thread id %s." % (self.alias, self._thread_ident, _thread.get_ident())
         )
示例#14
0
文件: common.py 项目: benwsapp/awx
def get_model_for_type(type):
    '''
    Return model class for a given type name.
    '''
    from django.contrib.contenttypes.models import ContentType
    for ct in ContentType.objects.filter(Q(app_label='main') | Q(app_label='auth', model='user')):
        ct_model = ct.model_class()
        if not ct_model:
            continue
        ct_type = get_type_for_model(ct_model)
        if type == ct_type:
            return ct_model
    else:
        raise DatabaseError('"{}" is not a valid AWX model.'.format(type))
示例#15
0
def test_assure_db(mocker):
    body = "{}"
    mock_message = mocker.Mock()
    conns = [mocker.Mock(), mocker.Mock(), mocker.Mock(), mocker.Mock()]
    conns[0].close_if_unusable_or_obsolete.side_effect = InterfaceError(
        'connection already closed')
    conns[1].close_if_unusable_or_obsolete.side_effect = DatabaseError(
        'pgbouncer cannot connect to server SSL connection has been closed unexpectedly'
    )

    mocker.patch('django.db.connections.all', return_value=conns)
    assure_db_connection(body, mock_message)

    conns[0].close_if_unusable_or_obsolete.assert_called_with()
    conns[1].close_if_unusable_or_obsolete.assert_called_with()
    conns[2].close_if_unusable_or_obsolete.assert_called_with()

    with pytest.raises(DatabaseError) as e_databaseerror:
        conns = [mocker.Mock()]
        conns[0].close_if_unusable_or_obsolete.side_effect = DatabaseError(
            'Not expected error')
        mocker.patch('django.db.connections.all', return_value=conns)
        assure_db_connection(body, mock_message)
示例#16
0
    def register(self):

        email = self.cleaned_data['email']
        password = self.cleaned_data['password']
        confirmpassword = self.cleaned_data['confirmpassword']

        if password == confirmpassword:
            try:
                User.objects.create_user(email=email, password=password)
                return True
            except ObjectDoesNotExist as odne:
                raise ObjectDoesNotExist(odne)
            except DatabaseError as de:
                raise DatabaseError(de)
示例#17
0
    def test_check_measures_with_database_error(self, m_mock):
        """
        Test if with 'broken scenario', all goes bad
        """
        m_mock.side_effect = DatabaseError('Boom')

        measures = ['size', 'commit', 'rollback', 'read', 'hit']

        for measure in measures:
            checker = getattr(self.informer, 'check_%s' % measure)

            expected = (0, 'We can not get the database %s (Boom).' % measure)

            self.assertTupleEqual(expected, checker())
示例#18
0
 def update_user(self, instance):
     try:
         updated_user = instance.save()
         updated_user.groups.clear()
         if updated_user.is_superuser:
             user_group = Group.objects.get(name='superuser')
         elif updated_user.is_admin:
             user_group = Group.objects.get(name='admin')
         elif updated_user.is_staff:
             user_group = Group.objects.get(name='staff')
         updated_user.groups.add(user_group)
     except DatabaseError as e:
         raise DatabaseError("Error occurred while updating")
     return updated_user
示例#19
0
    def test_database_error(self, create_mock):
        # Create a submission for the student and score it
        submission = sub_api.create_submission(self.STUDENT_ITEM,
                                               'test answer')
        sub_api.set_score(submission['uuid'], 1, 2)

        # Simulate a database error when creating the reset score
        create_mock.side_effect = DatabaseError("Test error")
        with self.assertRaises(sub_api.SubmissionInternalError):
            sub_api.reset_score(
                self.STUDENT_ITEM['student_id'],
                self.STUDENT_ITEM['course_id'],
                self.STUDENT_ITEM['item_id'],
            )
示例#20
0
    def test_update_pgreport_table(self):
        with patch('pgreport.views.ProgressModules') as pmock:
            update_pgreport_table(self.course.id)

        pmock.assert_any_call(
            count=6, display_name=self.problems[0].display_name,
            weight=None, standard_deviation=0.0, correct_map={}, median=0.0, due=None,
            submit_count=0, start=datetime.datetime(2030, 1, 1, 0, 0, tzinfo=UTC),
            location=self.problems[0].location, course_id=self.course.id, variance=0.0,
            student_answers={}, max_score=0.0, total_score=12.0, mean=0.0
        )

        with patch('pgreport.views.ProgressModules', side_effect=DatabaseError()):
            with self.assertRaises(DatabaseError):
                update_pgreport_table(self.course.id)
示例#21
0
    def test_database_create_error_handling(self, mock_create):
        mock_create.side_effect = DatabaseError("KABOOM!")

        # Try to create a staff assessment, handle database errors
        with self.assertRaises(StaffAssessmentInternalError) as context_manager:
            staff_api.create_assessment(
                "000000",
                "Dumbledore",
                OPTIONS_SELECTED_DICT['most']['options'], {}, "",
                RUBRIC,
            )
        self.assertEqual(
            str(context_manager.exception),
            "An error occurred while creating an assessment by the scorer with this ID: {}".format("Dumbledore")
        )
示例#22
0
    def test_determine_db_status(self, mock_query):
        """Health should not be ok if it cannot connect to the db"""

        mock_query.side_effect = DatabaseError()
        url = reverse('health-list')
        response = self.c.get(url)

        status = response.json().get("status", {})
        db_status = status.get('db')
        assert db_status == 'down', \
            'Expect DB to be down. Got: {}' . format (db_status)

        status = status.get('status')
        assert status == 'down', \
            'Expect status to be down. Got: {}' . format (status)
    def test_database_error(self):

        original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf")
        archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")
        Path(original).touch()
        Path(archive).touch()
        doc = Document(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_filename="0000001.pdf", archive_checksum="B")
        with mock.patch("documents.signals.handlers.Document.objects.filter") as m:
            m.side_effect = DatabaseError()
            doc.save()

        self.assertTrue(os.path.isfile(original))
        self.assertTrue(os.path.isfile(archive))
        self.assertTrue(os.path.isfile(doc.source_path))
        self.assertTrue(os.path.isfile(doc.archive_path))
示例#24
0
 def lock(flow_class, process_pk):
     for i in range(attempts):
         with transaction.atomic():
             try:
                 process = flow_class.process_class._default_manager.filter(pk=process_pk)
                 if not process.select_for_update(nowait=nowait).exists():
                     raise DatabaseError('Process not exists')
                 yield
                 break
             except DatabaseError:
                 if i != attempts - 1:
                     sleep_time = (((i + 1) * random.random()) + 2 ** i) / 2.5
                     time.sleep(sleep_time)
                 else:
                     raise FlowLockFailed('Lock failed for {}'.format(flow_class))
示例#25
0
 def create_investor(self, investor_data):
     try:
         new_investor = self.model(
             name=investor_data['name'],
             phone_no=investor_data['phone_no'],
             address=investor_data['address'],
             joining_date=investor_data['joining_date'])
         new_investor.save(using=self.db)
         from investor.models import InvestHistory
         invest = InvestHistory(share_holder=new_investor,
                                amount=investor_data['amount'])
         invest.save(using=self.db)
     except DatabaseError as e:
         raise DatabaseError("Technical problem to create investor")
     return new_investor
示例#26
0
 def shouldAddBlock(self):
     eb = ElectionBusiness()
     # While election is occurring, new blocks should be generated.
     if eb.isOccurring():
         return True
     if eb.isLocked() and eb.hadFinished():
         return True
     ec = eb.getCurrentElectionConfig()
     if not ec:
         raise DatabaseError('There is not Election Configuration for the current election')
     # Independently of election is occurring, if we have votes, a new block must be generated.
     if Voted.objects.filter(hash_val__isnull=True).count() > 0:
         return True
     # If election is not occurring and we do not have votes we do not need to generate a block.
     return False
示例#27
0
 def lookup_user(cls, email):
     if 'nees_users' in connections:
         cursor = connections['nees_users'].cursor()
         try:
             cursor.execute(cls._lookup_sql, [email])
             columns = [col[0] for col in cursor.description]
             return [
                 cls(**dict(zip(columns, row)))
                 for row in cursor.fetchall()
             ]
         finally:
             cursor.close()
     else:
         logger.error('Database connection for `nees_users` is not defined')
         raise DatabaseError('The NEES users database connection is unavailable.')
示例#28
0
 def test_registration_exception(self, create_inactive_user):
     """
     User is not created beforehand if an exception occurred at
     creating registration profile.
     """
     create_inactive_user.side_effect = DatabaseError()
     valid_data = {
         'username': '******',
         'email': '*****@*****.**',
         'password1': 'secret',
         'password2': 'secret'
     }
     with self.assertRaises(DatabaseError):
         self.client.post(reverse('registration_register'), data=valid_data)
     assert not UserModel().objects.filter(username='******').exists()
示例#29
0
文件: views.py 项目: saif409/Test
def add_survey(request):
    if request.user.is_authenticated:
        form = SurveyCreationForm()
        address_form = AddressCreationForm()
        if request.method == 'POST':
            form = SurveyCreationForm(request.POST)
            form.question_instances = QuestionFormset(request.POST)

            address = {
                'country': request.POST.get('country', None),
                'division': request.POST.get('state', None),
                'district': request.POST.get('district', None),
                'subdistrict': request.POST.get('subdistrict', None),
                'area_name': request.POST.get('area', None),
            }
            survey_address = AddressCreationForm(address)

            survey = None
            if form.is_valid():
                survey = Survey(**form.cleaned_data)
                survey.save()
            if form.question_instances.is_valid():
                if form.question_instances.cleaned_data is not None:

                    for item in form.question_instances.cleaned_data:
                        question = Question(**item)
                        question.save()
                        survey.question.add(question)

            if survey_address.is_valid():
                survey_address = Area(**survey_address.cleaned_data)
                try:
                    survey_address.save()
                except DatabaseError as e:
                    print(e)
                    raise DatabaseError(e)
                survey.area = survey_address
                survey.save()
                messages.success(request, 'Survey Created Successfully!')
        context = {
            'form': form,
            'address_form': address_form,
            'isact_createsurvey': 'active'
        }
        return render(request, 'sadmin_templates/survey/create_survey.html',
                      context)
    else:
        return redirect('admin_login')
示例#30
0
文件: tests.py 项目: trught007/django
 def column_classes(self, model):
     cursor = connection.cursor()
     columns = dict(
         (d[0], (connection.introspection.get_field_type(d[1], d), d))
         for d in connection.introspection.get_table_description(
             cursor,
             model._meta.db_table,
         ))
     # SQLite has a different format for field_type
     for name, (type, desc) in columns.items():
         if isinstance(type, tuple):
             columns[name] = (type[0], desc)
     # SQLite also doesn't error properly
     if not columns:
         raise DatabaseError("Table does not exist (empty pragma)")
     return columns