-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from StreamsTech/angularjs-analytics
Angularjs analytics
- Loading branch information
Showing
19 changed files
with
379 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from .db_connections import Database | ||
from django.db import models | ||
|
||
|
||
class ClassFactory(object): | ||
DJANGO_MODEL = { | ||
'bigint': models.BigIntegerField, | ||
'character varying': models.CharField, | ||
'integer': models.IntegerField, | ||
'boolean': models.BooleanField, | ||
'text': models.TextField, | ||
'smallint': models.SmallIntegerField | ||
} | ||
def __init__(self): | ||
super(ClassFactory, self).__init__() | ||
|
||
def create_model(self, name, fields=None, app_label='', module='', options=None, admin_opts=None, db='default'): | ||
""" | ||
Create specified model | ||
EX: model = create_model('NycRoad', app_label='front_end', options=dict(db_table='nyc_road')) | ||
""" | ||
class Meta: | ||
# Using type('Meta', ...) gives a dictproxy error during model creation | ||
pass | ||
if app_label: | ||
# app_label must be set using the Meta inner class | ||
setattr(Meta, 'app_label', app_label) | ||
# Update Meta with any options that were provided | ||
if options is not None: | ||
for key, value in options.iteritems(): | ||
setattr(Meta, key, value) | ||
|
||
# Set up a dictionary to simulate declarations within a class | ||
attrs = {'__module__': module, 'Meta': Meta, 'database':db} | ||
# Add in any fields that were provided | ||
if fields: | ||
attrs.update(fields) | ||
# Create the class, which automatically triggers ModelBase processing | ||
model = type(name, (models.Model,), attrs) | ||
# Create an Admin class if admin options were provided | ||
# if admin_opts is not None: | ||
# class Admin(admin.ModelAdmin): | ||
# pass | ||
# for key, value in admin_opts: | ||
# setattr(Admin, key, value) | ||
# admin.site.register(model, Admin) | ||
return model | ||
|
||
def get_model_field(self, data_type,column_name=None, blank=True, is_null=True, character_maximum_length=None, *args, **kwargs): | ||
if character_maximum_length is None: | ||
if column_name == 'fid': | ||
return self.DJANGO_MODEL[data_type](null=is_null, primary_key=True) | ||
else: | ||
return self.DJANGO_MODEL[data_type](null=is_null) | ||
else: | ||
return self.DJANGO_MODEL[data_type](null=is_null, max_length=character_maximum_length) | ||
|
||
def get_model(self, name, table_name, app_label='dynamic', db=None): | ||
schema_infos = Database(db_name=db).get_table_schema_info(table_name=table_name) | ||
fields = {f.column_name: self.get_model_field(**f) for f in schema_infos if f.data_type in self.DJANGO_MODEL} | ||
return self.create_model(name, app_label=app_label, fields=fields, options=dict(db_table=table_name), db=db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.db import connections | ||
from django.conf import settings | ||
|
||
|
||
class InformationSchemaColumns: | ||
_KEYS = ['column_name', 'is_nullable', 'data_type', 'character_maximum_length'] | ||
column_name = None | ||
is_nullable = None | ||
data_type = None | ||
character_maximum_length = None | ||
def __init__(self, *args, **kwargs): | ||
self.column_name = kwargs.get('column_name', '') | ||
self.is_nullable = kwargs.get('is_nullable', 'YES') | ||
self.data_type = kwargs.get('data_type', None) | ||
self.character_maximum_length = kwargs.get('character_maximum_length', None) | ||
|
||
@property | ||
def is_null(self): | ||
return True if self.is_nullable == 'YES' else False | ||
|
||
def keys(self): | ||
return self._KEYS | ||
|
||
def __getitem__(self, key): | ||
return getattr(self, key) | ||
|
||
|
||
class Database(object): | ||
''' | ||
This class will handle db connection | ||
''' | ||
TABLE_SCHEMA_INFO_QUERY = "SELECT %s FROM information_schema.columns WHERE TABLE_NAME = '%s'" | ||
|
||
def __init__(self, db_name=None): | ||
if db_name is None: | ||
k, v = settings.DATABASES.items()[0] | ||
self.cursor = connections[k] | ||
else: | ||
self.cursor = connections[db_name] | ||
|
||
def get_table_schema_info(self, table_name): | ||
columns_details = [] | ||
schema_columns = InformationSchemaColumns._KEYS | ||
with self.cursor.cursor() as cursor: | ||
sql = self.TABLE_SCHEMA_INFO_QUERY % (','.join(schema_columns), table_name) | ||
cursor.execute(sql) | ||
for row in cursor.fetchall(): | ||
columns_details.append(InformationSchemaColumns(**dict(zip(schema_columns, row)))) | ||
|
||
return columns_details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class DbRouter(object): | ||
""" | ||
A router to control all database operations on models in the | ||
auth application. | ||
""" | ||
def db_for_read(self, model, **hints): | ||
""" | ||
Attempts to read auth models go to auth_db. | ||
""" | ||
return getattr(model, 'database', None) | ||
|
||
def db_for_write(self, model, **hints): | ||
""" | ||
Attempts to write auth models go to auth_db. | ||
""" | ||
return getattr(model, 'database', None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('contenttypes', '0002_remove_content_type_name'), | ||
('layers', '0026_auto_20171004_1311'), | ||
('documents', '0026_auto_20171004_1243'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='DocumentLayers', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('content_type', models.ForeignKey(blank=True, to='contenttypes.ContentType', null=True)), | ||
], | ||
options={ | ||
'db_table': 'document_layers', | ||
}, | ||
), | ||
migrations.RemoveField( | ||
model_name='document', | ||
name='content_type', | ||
), | ||
migrations.RemoveField( | ||
model_name='document', | ||
name='object_id', | ||
), | ||
migrations.AddField( | ||
model_name='documentlayers', | ||
name='document', | ||
field=models.ForeignKey(to='documents.Document'), | ||
), | ||
migrations.AddField( | ||
model_name='documentlayers', | ||
name='layer', | ||
field=models.ForeignKey(blank=True, to='layers.Layer', null=True), | ||
), | ||
migrations.AddField( | ||
model_name='document', | ||
name='layers', | ||
field=models.ManyToManyField(to='layers.Layer', null=True, through='documents.DocumentLayers', blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.