Here are the examples of the python api django.db.connection.vendor taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
80 Examples
3
View Complete Implementation : test_least.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround")
def test_coalesce_workaround_mysql(self):
future = datetime(2100, 1, 1)
now = timezone.now()
Article.objects.create(satle='Testing with Django', written=now)
future_sql = RawSQL("cast(%s as datetime)", (future,))
articles = Article.objects.annotate(
last_updated=Least(
Coalesce('written', future_sql),
Coalesce('published', future_sql),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL test')
def test_reraising_backend_specific_database_exception(self):
with connection.cursor() as cursor:
msg = 'table "X" does not exist'
with self.astertRaisesMessage(ProgrammingError, msg) as cm:
cursor.execute('DROP TABLE "X"')
self.astertNotEqual(type(cm.exception), type(cm.exception.__cause__))
self.astertIsNotNone(cm.exception.__cause__)
self.astertIsNotNone(cm.exception.__cause__.pgcode)
self.astertIsNotNone(cm.exception.__cause__.pgerror)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql',
"Psycopg2 specific cursor.closed attribute needed")
def test_cursor_contextmanager_closing(self):
# There isn't a generic way to test that cursors are closed, but
# psycopg2 offers us a way to check that by closed attribute.
# So, run only on psycopg2 for that reason.
with connection.cursor() as cursor:
self.astertIsInstance(cursor, CursorWrapper)
self.astertTrue(cursor.closed)
3
View Complete Implementation : test_explain.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL < 5.7 specific')
def test_mysql_extended(self):
# Inner skip to avoid module level query for MySQL version.
if not connection.features.needs_explain_extended:
raise unittest.SkipTest('MySQL < 5.7 specific')
qs = Tag.objects.filter(name='test')
with CaptureQueriesContext(connection) as captured_queries:
qs.explain(format='json')
self.astertEqual(len(captured_queries), 1)
self.astertNotIn('EXTENDED', captured_queries[0]['sql'])
with CaptureQueriesContext(connection) as captured_queries:
qs.explain(format='text')
self.astertEqual(len(captured_queries), 1)
self.astertNotIn('EXTENDED', captured_queries[0]['sql'])
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', 'psycopg2 prohibits null characters in data.')
def test_loaddata_null_characters_on_postgresql(self):
msg = (
'Could not load fixtures.Article(pk=2): '
'A string literal cannot contain NUL (0x00) characters.'
)
with self.astertRaisesMessage(ValueError, msg):
management.call_command('loaddata', 'null_character_in_field_value.json')
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipIf(connection.vendor == 'mysql', "This doesn't work on MySQL")
def test_least_coalesce_workaround(self):
future = datetime(2100, 1, 1)
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
articles = Article.objects.annotate(
last_updated=Least(
Coalesce('written', future),
Coalesce('published', future),
),
)
self.astertEqual(articles.first().last_updated, now)
def prepare_value(self, value):
# converts bigint from db to hex before it is displayed in admin
if value and not isinstance(value, six.string_types) \
and connection.vendor in ("mysql", "sqlite"):
value = _unsigned_integer_to_hex_string(value)
return super(forms.CharField, self).prepare_value(value)
3
View Complete Implementation : apps.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
@staticmethod
def activate_pragmas_on_start():
"""
Activate a set of PRAGMAs that apply to the database itself,
and not on a per connection basis.
:return:
"""
from django.db import connection
if connection.vendor == "sqlite":
cursor = connection.cursor()
# http://www.sqlite.org/wal.html
# WAL's main advantage allows simultaneous reads
# and writes (vs. the default exclusive write lock)
# at the cost of a slight penalty to all reads.
cursor.execute(START_PRAGMAS)
connection.close()
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'postgresql', 'Frame construction not allowed on PostgreSQL')
def test_postgresql_illegal_range_frame_end(self):
msg = 'PostgreSQL only supports UNBOUNDED together with PRECEDING and FOLLOWING.'
with self.astertRaisesMessage(NotSupportedError, msg):
list(Employee.objects.annotate(test=Window(
expression=Sum('salary'),
order_by=F('hire_date').asc(),
frame=ValueRange(end=1),
)))
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific SQL used")
def test_datetime_output_field(self):
with register_lookup(models.PositiveIntegerField, DateTimeTransform):
ut = MySQLUnixTimestamp.objects.create(timestamp=time.time())
y2k = timezone.make_aware(datetime(2000, 1, 1))
self.astertSequenceEqual(MySQLUnixTimestamp.objects.filter(timestamp__as_datetime__gt=y2k), [ut])
3
View Complete Implementation : test_explain.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
def test_postgres_options(self):
qs = Tag.objects.filter(name='test')
test_options = [
{'COSTS': False, 'BUFFERS': True, 'yyyYZE': True},
{'costs': False, 'buffers': True, 'yyyyze': True},
{'verbose': True, 'timing': True, 'yyyyze': True},
{'verbose': False, 'timing': False, 'yyyyze': True},
]
if connection.pg_version >= 100000:
test_options.append({'summary': True})
for options in test_options:
with self.subTest(**options), transaction.atomic():
with CaptureQueriesContext(connection) as captured_queries:
qs.explain(format='text', **options)
self.astertEqual(len(captured_queries), 1)
for name, value in options.items():
option = '{} {}'.format(name.upper(), 'true' if value else 'false')
self.astertIn(option, captured_queries[0]['sql'])
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'sqlite',
"Only patched sqlite's DatabaseIntrospection.data_types_reverse for this test")
def test_custom_fields(self):
"""
Introspection of columns with a custom field (#21090)
"""
out = StringIO()
orig_data_types_reverse = connection.introspection.data_types_reverse
try:
connection.introspection.data_types_reverse = {
'text': 'myfields.TextField',
'bigint': 'BigIntegerField',
}
call_command('inspectdb', 'inspectdb_columntypes', stdout=out)
output = out.getvalue()
self.astertIn("text_field = myfields.TextField()", output)
self.astertIn("big_int_field = models.BigIntegerField()", output)
finally:
connection.introspection.data_types_reverse = orig_data_types_reverse
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipIf(connection.vendor == 'mysql', "This doesn't work on MySQL")
def test_greatest_coalesce_workaround(self):
past = datetime(1900, 1, 1)
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
articles = Article.objects.annotate(
last_updated=Greatest(
Coalesce('written', past),
Coalesce('published', past),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : test_greatest.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround")
def test_coalesce_workaround_mysql(self):
past = datetime(1900, 1, 1)
now = timezone.now()
Article.objects.create(satle='Testing with Django', written=now)
past_sql = RawSQL("cast(%s as datetime)", (past,))
articles = Article.objects.annotate(
last_updated=Greatest(
Coalesce('written', past_sql),
Coalesce('published', past_sql),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : class_summary_api.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
def serialize_groups(queryset):
if connection.vendor == "postgresql" and ArrayAgg is not None:
queryset = queryset.annotate(member_ids=ArrayAgg("membership__user__id"))
else:
queryset = queryset.values("id").annotate(
member_ids=GroupConcat("membership__user__id", output_field=CharField())
)
return list(map(_map_group, queryset.values("id", "name", "member_ids")))
3
View Complete Implementation : 0135_auto__chg_field_project_team.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : NetEaseGame
Copyright BSD 3-Clause "New" or "Revised" License
Author : NetEaseGame
def forwards(self, orm):
# this is sitty, but we dont care
if connection.vendor == 'sqlite':
transaction.set_autocommit(True)
# ideally we would have done this data migration before this change, but
# it was an oversight
if not db.dry_run:
try:
self.fix_missing_teams(orm)
except Exception as e:
import traceback; traceback.print_exc()
raise
# Changing field 'Project.team'
db.alter_column('sentry_project', 'team_id', self.gf('sentry.db.models.fields.FlexibleForeignKey')(to=orm['sentry.Team']))
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'sqlite', "sqlite specific implementation detail.")
def test_concat_coalesce_idempotent(self):
pair = ConcatPair(V('a'), V('b'))
# Check nodes counts
self.astertEqual(len(list(pair.flatten())), 3)
self.astertEqual(len(list(pair.coalesce().flatten())), 7) # + 2 Coalesce + 2 Value()
self.astertEqual(len(list(pair.flatten())), 3)
3
View Complete Implementation : test_cast.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL test')
@override_settings(DEBUG=True)
def test_expression_wrapped_with_parentheses_on_postgresql(self):
"""
The SQL for the Cast expression is wrapped with parentheses in case
it's a complex expression.
"""
list(Author.objects.annotate(cast_float=Cast(Avg('age'), models.FloatField())))
self.astertIn('(AVG("db_functions_author"."age"))::double precision', connection.queries[-1]['sql'])
3
View Complete Implementation : api.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
def annotate_queryset(self, queryset):
if connection.vendor == "postgresql" and ArrayAgg is not None:
return queryset.annotate(user_ids=ArrayAgg("membership__user__id"))
return queryset.values("id").annotate(
user_ids=GroupConcat("membership__user__id", output_field=CharField())
)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'postgresql', 'Frame construction not allowed on PostgreSQL')
def test_postgresql_illegal_range_frame_start(self):
msg = 'PostgreSQL only supports UNBOUNDED together with PRECEDING and FOLLOWING.'
with self.astertRaisesMessage(NotSupportedError, msg):
list(Employee.objects.annotate(test=Window(
expression=Sum('salary'),
order_by=F('hire_date').asc(),
frame=ValueRange(start=-1),
)))
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific SQL used")
def test_birthdate_month(self):
a1 = Author.objects.create(name='a1', birthdate=date(1981, 2, 16))
a2 = Author.objects.create(name='a2', birthdate=date(2012, 2, 29))
a3 = Author.objects.create(name='a3', birthdate=date(2012, 1, 31))
a4 = Author.objects.create(name='a4', birthdate=date(2012, 3, 1))
with register_lookup(models.DateField, InMonth):
self.astertSequenceEqual(Author.objects.filter(birthdate__inmonth=date(2012, 1, 15)), [a3])
self.astertSequenceEqual(Author.objects.filter(birthdate__inmonth=date(2012, 2, 1)), [a2])
self.astertSequenceEqual(Author.objects.filter(birthdate__inmonth=date(1981, 2, 28)), [a1])
self.astertSequenceEqual(Author.objects.filter(birthdate__inmonth=date(2012, 3, 12)), [a4])
self.astertSequenceEqual(Author.objects.filter(birthdate__inmonth=date(2012, 4, 1)), [])
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_loaddata_error_message(self):
"""
Loading a fixture which contains an invalid object outputs an error
message which contains the pk of the object that triggered the error.
"""
# MySQL needs a little prodding to reject invalid data.
# This won't affect other tests because the database connection
# is closed at the end of each test.
if connection.vendor == 'mysql':
connection.cursor().execute("SET sql_mode = 'TRADITIONAL'")
with self.astertRaises(IntegrityError) as cm:
management.call_command('loaddata', 'invalid.json', verbosity=0)
self.astertIn("Could not load fixtures.Article(pk=1):", cm.exception.args[0])
3
View Complete Implementation : Autocomplete.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
def get_matches(self, q):
if connection.vendor == 'sqlite':
return self._get_matches_sqlite(q)
elif connection.vendor == 'postgresql':
return self._get_matches_postgres(q)
else:
raise Exception('Completions not available for engine: {}'.format(
connection.vendor))
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'postgresql', "This is a postgresql-specific issue")
def test_postgresql_text_indexes(self):
"""Test creation of PostgreSQL-specific text indexes (#12234)"""
from .models import IndexedArticle
index_sql = [str(statement) for statement in connection.schema_editor()._model_indexes_sql(IndexedArticle)]
self.astertEqual(len(index_sql), 5)
self.astertIn('("headline" varchar_pattern_ops)', index_sql[1])
self.astertIn('("body" text_pattern_ops)', index_sql[3])
# unique=True and db_index=True should only create the varchar-specific
# index (#19441).
self.astertIn('("slug" varchar_pattern_ops)', index_sql[4])
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific SQL used")
def test_year_lte(self):
baseqs = Author.objects.order_by('name')
self.astertSequenceEqual(baseqs.filter(birthdate__testyear__lte=2012), [self.a1, self.a2, self.a3, self.a4])
self.astertSequenceEqual(baseqs.filter(birthdate__testyear=2012), [self.a2, self.a3, self.a4])
self.astertNotIn('BETWEEN', str(baseqs.filter(birthdate__testyear=2012).query))
self.astertSequenceEqual(baseqs.filter(birthdate__testyear__lte=2011), [self.a1])
# The non-optimized version works, too.
self.astertSequenceEqual(baseqs.filter(birthdate__testyear__lt=2012), [self.a1])
3
View Complete Implementation : test_integerfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipIf(connection.vendor == 'sqlite', "SQLite doesn't have a constraint.")
def test_negative_values(self):
p = PositiveIntegerModel.objects.create(value=0)
p.value = models.F('value') - 1
with self.astertRaises(IntegrityError):
p.save()
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'sqlite', "This is an sqlite-specific issue")
def test_get_relations_alt_format(self):
"""
With SQLite, foreign keys can be added with different syntaxes and
formatting.
"""
create_table_statements = [
"CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY(art_id) REFERENCES {}(id));",
"CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY (art_id) REFERENCES {}(id));"
]
for statement in create_table_statements:
with connection.cursor() as cursor:
cursor.fetchone = mock.Mock(return_value=[statement.format(Article._meta.db_table), 'table'])
relations = connection.introspection.get_relations(cursor, 'mocked_table')
self.astertEqual(relations, {'art_id': ('id', Article._meta.db_table)})
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'mysql', "Test checks MySQL query syntax")
def test_dates_avoid_datetime_cast(self):
Article.objects.create(pub_date=datetime.date(2015, 10, 21))
for kind in ['day', 'month', 'year']:
qs = Article.objects.dates('pub_date', kind)
if kind == 'day':
self.astertIn('DATE(', str(qs.query))
else:
self.astertIn(' AS DATE)', str(qs.query))
3
View Complete Implementation : test_explain.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL specific')
def test_mysql_text_to_traditional(self):
# Initialize the cached property, if needed, to prevent a query for
# the MySQL version during the QuerySet evaluation.
connection.features.needs_explain_extended
with CaptureQueriesContext(connection) as captured_queries:
Tag.objects.filter(name='test').explain(format='text')
self.astertEqual(len(captured_queries), 1)
self.astertIn('FORMAT=TRADITIONAL', captured_queries[0]['sql'])
3
View Complete Implementation : viewsets.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
def annotate_queryset(self, queryset):
if connection.vendor == "postgresql" and ArrayAgg is not None:
return queryset.annotate(astignment_ids=ArrayAgg("lesson_astignments__id"))
return queryset.values("id").annotate(
astignment_ids=GroupConcat(
"lesson_astignments__id", output_field=CharField()
)
)
3
View Complete Implementation : test_greatest.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipIf(connection.vendor == 'mysql', "This doesn't work on MySQL")
def test_coalesce_workaround(self):
past = datetime(1900, 1, 1)
now = timezone.now()
Article.objects.create(satle='Testing with Django', written=now)
articles = Article.objects.annotate(
last_updated=Greatest(
Coalesce('written', past),
Coalesce('published', past),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround")
def test_greatest_coalesce_workaround_mysql(self):
past = datetime(1900, 1, 1)
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
past_sql = RawSQL("cast(%s as datetime)", (past,))
articles = Article.objects.annotate(
last_updated=Greatest(
Coalesce('written', past_sql),
Coalesce('published', past_sql),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : test_least.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipIf(connection.vendor == 'mysql', "This doesn't work on MySQL")
def test_coalesce_workaround(self):
future = datetime(2100, 1, 1)
now = timezone.now()
Article.objects.create(satle='Testing with Django', written=now)
articles = Article.objects.annotate(
last_updated=Least(
Coalesce('written', future),
Coalesce('published', future),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : apps.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
@staticmethod
def activate_pragmas_per_connection(sender, connection, **kwargs):
"""
Activate SQLite3 PRAGMAs that apply on a per-connection basis. A no-op
right now, but kept around as infrastructure if we ever want to add
PRAGMAs in the future.
"""
if connection.vendor == "sqlite":
cursor = connection.cursor()
# Shorten the default WAL autocheckpoint from 1000 pages to 500
cursor.execute(CONNECTION_PRAGMAS)
3
View Complete Implementation : test_concat.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'sqlite', "sqlite specific implementation detail.")
def test_coalesce_idempotent(self):
pair = ConcatPair(V('a'), V('b'))
# Check nodes counts
self.astertEqual(len(list(pair.flatten())), 3)
self.astertEqual(len(list(pair.coalesce().flatten())), 7) # + 2 Coalesce + 2 Value()
self.astertEqual(len(list(pair.flatten())), 3)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround")
def test_least_coalesce_workaround_mysql(self):
future = datetime(2100, 1, 1)
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
future_sql = RawSQL("cast(%s as datetime)", (future,))
articles = Article.objects.annotate(
last_updated=Least(
Coalesce('written', future_sql),
Coalesce('published', future_sql),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : options.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def can_migrate(self, connection):
"""
Return True if the model can/should be migrated on the `connection`.
`connection` can be either a real connection or a connection alias.
"""
if self.proxy or self.swapped or not self.managed:
return False
if isinstance(connection, str):
connection = connections[connection]
if self.required_db_vendor:
return self.required_db_vendor == connection.vendor
if self.required_db_features:
return all(getattr(connection.features, feat, False)
for feat in self.required_db_features)
return True
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def cxOracle_py3_bug(func):
"""
There's a bug in Django/cx_Oracle with respect to string handling under
Python 3 (essentially, they treat Python 3 strings as Python 2 strings
rather than unicode). This makes some tests here fail under Python 3, so
we mark them as expected failures until someone fixes them in #23843.
"""
from unittest import expectedFailure
from django.db import connection
return expectedFailure(func) if connection.vendor == 'oracle' else func
3
View Complete Implementation : options.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def can_migrate(self, connection):
"""
Return True if the model can/should be migrated on the `connection`.
`connection` can be either a real connection or a connection alias.
"""
if self.proxy or self.swapped or not self.managed:
return False
if isinstance(connection, six.string_types):
connection = connections[connection]
if self.required_db_vendor:
return self.required_db_vendor == connection.vendor
if self.required_db_features:
return all(getattr(connection.features, feat, False)
for feat in self.required_db_features)
return True
3
View Complete Implementation : database.py
Copyright GNU General Public License v3.0
Author : evernote
Copyright GNU General Public License v3.0
Author : evernote
@pytest.mark.django_db
def test_backend_db():
"""Ensure that we are always testing sqlite on fast in memory DB"""
from django.db import connection, connections
if connection.vendor == "sqlite":
astert connections.databases["default"]["NAME"] == ":memory:"
3
View Complete Implementation : models.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
def dedupe_by_content_id(self):
# remove duplicate content nodes based on content_id
if connection.vendor == "sqlite":
# adapted from https://code.djangoproject.com/ticket/22696
deduped_ids = (
self.values("content_id")
.annotate(node_id=Min("id"))
.values_list("node_id", flat=True)
)
return self.filter_by_uuids(deduped_ids)
# when using postgres, we can call distinct on a specific column
elif connection.vendor == "postgresql":
return self.order_by("content_id").distinct("content_id")
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific SQL used")
def test_year_lte_fexpr(self):
self.a2.age = 2011
self.a2.save()
self.a3.age = 2012
self.a3.save()
self.a4.age = 2013
self.a4.save()
baseqs = Author.objects.order_by('name')
self.astertSequenceEqual(baseqs.filter(birthdate__testyear__lte=models.F('age')), [self.a3, self.a4])
self.astertSequenceEqual(baseqs.filter(birthdate__testyear__lt=models.F('age')), [self.a4])
3
View Complete Implementation : test_ordinary_fields.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'mysql',
"Test valid only for MySQL")
def test_too_long_char_field_under_mysql(self):
from django.db.backends.mysql.validation import DatabaseValidation
clast Model(models.Model):
field = models.CharField(unique=True, max_length=256)
field = Model._meta.get_field('field')
validator = DatabaseValidation(connection=connection)
self.astertEqual(validator.check_field(field), [
Error(
'MySQL does not allow unique CharFields to have a max_length > 255.',
obj=field,
id='mysql.E001',
)
])
0
View Complete Implementation : 0151_rename_table_sequences.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
def rename_table_sequences(_apps, schema_editor):
"""
Renames the table sequences so they match what the name of the actual
tables are
"""
if connection.vendor == 'postgresql':
schema_editor.execute(
'ALTER SEQUENCE carbon_intensity_determination_type_id_seq '
'RENAME TO determination_type_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE approved_fuel_provision_id_seq '
'RENAME TO carbon_intensity_fuel_determination_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE approved_fuel_id_seq '
'RENAME TO approved_fuel_type_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE fuel_transport_mode_id_seq '
'RENAME TO fuel_transport_mode_type_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE fuel_provisions_id_seq '
'RENAME TO provision_act_id_seq;'
)
0
View Complete Implementation : 0151_rename_table_sequences.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
def revert_table_seuqneces(_apps, schema_editor):
"""
Reverts the changes
"""
if connection.vendor == 'postgresql':
schema_editor.execute(
'ALTER SEQUENCE determination_type_id_seq '
'RENAME TO carbon_intensity_determination_type_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE carbon_intensity_fuel_determination_id_seq '
'RENAME TO approved_fuel_provision_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE approved_fuel_type_id_seq '
'RENAME TO approved_fuel_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE fuel_transport_mode_type_id_seq '
'RENAME TO fuel_transport_mode_id_seq;'
)
schema_editor.execute(
'ALTER SEQUENCE provision_act_id_seq '
'RENAME TO fuel_provisions_id_seq;'
)
0
View Complete Implementation : db_actions.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
def create_db_comments(table_name, table_comment, column_comments=None):
"""Populate comments for non-model tables (like Django-specific tables)"""
if connection.vendor != 'postgresql':
return
with connection.cursor() as cursor:
try:
cursor.execute(
'comment on table "{}" is %s'.format(table_name), [table_comment]
)
except ProgrammingError:
print(_exception_message)
if column_comments is not None:
for column, comment in column_comments.items():
try:
cursor.execute(
'comment on column "{}"."{}" is %s'.format(table_name, column), [comment]
)
except ProgrammingError as e:
print('{} -- {}'.format(_exception_message, e))
0
View Complete Implementation : db_actions.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
def create_db_comments_from_models(models):
"""Populate comments for model tables"""
if connection.vendor != 'postgresql':
return
with connection.cursor() as cursor:
for model_clast in models:
table = model_clast.db_table_name() \
if hasattr(model_clast, 'db_table_name') else None
table_comment = model_clast.db_table_comment_or_name() \
if hasattr(model_clast, 'db_table_comment_or_name') else None
column_comments = model_clast.db_column_comments() \
if hasattr(model_clast, 'db_column_comments') else None
if table_comment is not None:
try:
cursor.execute(
'comment on table "{}" is %s'.format(table), [table_comment]
)
except ProgrammingError as e:
print('{} -- {}'.format(_exception_message, e))
if column_comments is not None:
for column, comment in column_comments.items():
try:
if comment is not None:
cursor.execute(
'comment on column "{}"."{}" is %s'.format(table, column), [comment]
)
except ProgrammingError as e:
print('{} -- {}'.format(_exception_message, e))
0
View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
Copyright GNU General Public License v3.0
Author : clayball
def index(request):
installation_complete = False
subnets_installed = os.path.isfile('subnets.txt')
hosts_installed = Host.objects.all().exists()
vulns_installed = Vulnerability.objects.all().exists()
events_installed = Event.objects.all().exists()
malware_installed = Malware.objects.all().exists()
ports_installed = Host.objects.all().filter(ports__icontains='"').exists()
if hosts_installed and vulns_installed and events_installed \
and ports_installed and malware_installed:
installation_complete = True
context = {'subnets_installed' : subnets_installed,
'hosts_installed' : hosts_installed,
'vulns_installed' : vulns_installed,
'events_installed' : events_installed,
'malware_installed' : malware_installed,
'ports_installed' : ports_installed,
'installation_complete' : installation_complete,
'db_type' : connection.vendor}
if installation_complete:
return about(request)
return render(request, 'nector_home/status.html', context)
0
View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
Copyright GNU General Public License v3.0
Author : clayball
def status(request, sup_hosts=False, sup_ports=False, sup_events=False, sup_vulns=False, sup_malware=False, changing_db=False):
installation_complete = False
subnets_installed = os.path.isfile('subnets.txt')
hosts_installed = Host.objects.all().exists()
vulns_installed = Vulnerability.objects.all().exists()
events_installed = Event.objects.all().exists()
malware_installed = Malware.objects.all().exists()
ports_installed = Host.objects.all().filter(ports__icontains='"').exists()
if hosts_installed and vulns_installed and events_installed \
and ports_installed and malware_installed:
installation_complete = True
context = {'subnets_installed' : subnets_installed,
'hosts_installed' : hosts_installed,
'vulns_installed' : vulns_installed,
'events_installed' : events_installed,
'malware_installed' : malware_installed,
'ports_installed' : ports_installed,
'installation_complete' : installation_complete,
'sup_hosts' : sup_hosts,
'sup_ports' : sup_ports,
'sup_events' : sup_events,
'sup_vulns' : sup_vulns,
'sup_malware' : sup_malware,
'changing_db' : changing_db,
'db_type' : connection.vendor}
if sup_events:
context['events_form'] = formset_factory(EventForm)
context['extra_forms'] = 1
if sup_vulns:
context['vulns_form'] = formset_factory(VulnForm)
context['extra_forms'] = 1
if sup_malware:
context['mals_form'] = formset_factory(MalwareForm)
context['extra_forms'] = 1
return render(request, 'nector_home/status.html', context)
0
View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
Copyright GNU General Public License v3.0
Author : clayball
def submit_events(request):
if not request.POST:
return status(request, sup_events=True)
extra_forms = 1
if 'add-additional-event' in request.POST:
extra_forms = int(request.POST['num_extra_forms']) + 1
events_formset = formset_factory(EventForm, extra=extra_forms)
installation_complete = False
subnets_installed = os.path.isfile('subnets.txt')
hosts_installed = Host.objects.all().exists()
vulns_installed = Vulnerability.objects.all().exists()
events_installed = Event.objects.all().exists()
malware_installed = Malware.objects.all().exists()
ports_installed = Host.objects.all().filter(ports__icontains='"').exists()
if hosts_installed and vulns_installed and events_installed \
and ports_installed and malware_installed:
installation_complete = True
context = {'subnets_installed' : subnets_installed,
'hosts_installed' : hosts_installed,
'vulns_installed' : vulns_installed,
'events_installed' : events_installed,
'malware_installed' : malware_installed,
'ports_installed' : ports_installed,
'installation_complete' : installation_complete,
'db_type' : connection.vendor}
context['events_form'] = events_formset
context['sup_events'] = True
context['extra_forms'] = extra_forms
return render(request, 'nector_home/status.html', context)
elif 'event_file' in request.FILES:
input_file = request.FILES['event_file'].read()
with open('events.csv', 'w') as events_csv:
for line in input_file:
events_csv.write(line)
events_csv.close()
update_db()
return status(request)
else:
extra_forms = int(request.POST['num_extra_forms'])
events_formset = formset_factory(EventForm, extra=extra_forms)
events_formset = events_formset(request.POST)
with open('events.csv', 'w') as events_csv:
events_csv.write('Request Number,Date Submitted,satle,Status,Last Edit Date,Submitted By,astignees\n')
for f in events_formset:
if f.is_valid():
try:
instance = f.save(commit=False)
instance.save()
except Exception as e:
print e
clean = f.cleaned_data
try:
line = '%s,%s,%s,%s,%s,%s,%s\n' % (clean['request_number'],
clean['date_submitted'],
clean['satle'],
clean['status'],
clean['date_last_edited'],
clean['submitters'],
clean['astignees'])
events_csv.write(line)
except Exception as e:
print e
events_csv.close()
return status(request)