def create_default_entity(sender, app, **kwargs): """ Load default entities if none exist or create an entity with id=1 if not exist. """ def get_site_display_name(): setting_table_exists = Setting._meta.db_table in \ connection.introspection.table_names() if setting_table_exists: return get_setting("site", "global", "sitedisplayname") return '' if app == "entities": site_name = get_site_display_name().strip() if not Entity.objects.all(): call_command("loaddata", "default_entities.json") if site_name: # update the entity_name of the first default entity entity = Entity.objects.get(pk=1) entity.entity_name = site_name entity.save() else: if not Entity.objects.filter(pk=1): if not site_name: site_name = "Default" entity = Entity() entity.allow_anonymous_view = False entity.entity_name = site_name entity.id = 1 entity.save()
def create_default_entity(sender, app, **kwargs): """ Auto-create an entity with id=1 if none exist. """ if app == "entities": if not Entity.objects.filter(pk=1): site_name = "Default" table_exists = Setting._meta.db_table in \ connection.introspection.table_names() if table_exists and get_setting("site", "global", "sitedisplayname"): site_name = get_setting("site", "global", "sitedisplayname") entity = Entity() entity.allow_anonymous_view = False entity.entity_name = site_name entity.id = 1 entity.save()
def handle(self, *args, **options): from tendenci.apps.entities.models import Entity from tendenci.apps.user_groups.models import Group from tendenci.apps.site_settings.utils import get_setting from tendenci.apps.perms.models import TendenciBaseModel verbosity = int(options['verbosity']) [entity] = Entity.objects.filter(pk=1)[:1] or [None] [user] = User.objects.filter(pk=1)[:1] or [None] site_display_name = get_setting('site', 'global', 'sitedisplayname') if not site_display_name: site_display_name = 'Default' site_contact_name = get_setting('site', 'global', 'sitecontactname') site_contact_email = get_setting('site', 'global', 'sitecontactemail') site_phone_number = get_setting('site', 'global', 'sitephonenumber') site_url = get_setting('site', 'global', 'siteurl') # if there is no entity, create one. if not entity: params = { 'id': 1, 'entity_name': site_display_name, 'entity_type': '', 'contact_name': site_contact_name, 'phone': site_phone_number, 'email': site_contact_email, 'fax': '', 'website': site_url, 'summary': '', 'notes': '', 'admin_notes': 'system auto created', 'allow_anonymous_view': True, 'status': True, 'status_detail': 'active' } if user: params.update({ 'creator': user, 'creator_username': user.username, 'owner': user, 'owner_username': user.username }) else: params.update({'creator_username': '', 'owner_username': ''}) entity = Entity(**params) entity.save() print('entity created: ', entity.entity_name) # loop through all the tables and populate # the entity field only if it's null. models = apps.get_models() # exclude legacy tables tables_excluded = [ 'corporate_memberships_corporatemembership', 'corporate_memberships_corporatemembershiparchive' ] table_updated = [] for model in models: if TendenciBaseModel in model.__bases__: if hasattr(model, 'entity'): table_name = model._meta.db_table if table_name in tables_excluded: continue for row in model.objects.all(): if not row.entity: row.entity = entity row.save(update_fields=['entity']) table_updated.append(table_name) if verbosity >= 2: print() print('List of tables updated: ') print('\n'.join(table_updated)) print() # GROUP - check if we have a group associated with group_exists = Group.objects.filter(entity=entity).exists() if not group_exists: params = { 'name': site_display_name, 'entity': entity, 'type': 'distribution', 'email_recipient': site_contact_email, 'allow_anonymous_view': True, 'status': True, 'status_detail': 'active' } if user: params.update({ 'creator': user, 'creator_username': user.username, 'owner': user, 'owner_username': user.username }) else: params.update({'creator_username': '', 'owner_username': ''}) group = Group(**params) try: group.save() print('Group created: ', group.name) except Exception as e: print(e) print('All done.')
def handle(self, *args, **options): from tendenci.apps.entities.models import Entity from tendenci.apps.user_groups.models import Group from tendenci.core.site_settings.utils import get_setting from tendenci.core.perms.models import TendenciBaseModel verbosity = int(options['verbosity']) [entity] = Entity.objects.filter(pk=1)[:1] or [None] user = User.objects.get(pk=1) site_display_name = get_setting('site', 'global', 'sitedisplayname') if not site_display_name: site_display_name = 'Default' site_contact_name = get_setting('site', 'global', 'sitecontactname') site_contact_email = get_setting('site', 'global', 'sitecontactemail') site_phone_number = get_setting('site', 'global', 'sitephonenumber') site_url = get_setting('site', 'global', 'siteurl') # if there is no entity, create one. if not entity: entity = Entity(entity_name=site_display_name, entity_type='', contact_name=site_contact_name, phone=site_phone_number, email=site_contact_email, fax='', website=site_url, summary='', notes='', admin_notes='system auto created', allow_anonymous_view=True, creator=user, creator_username=user.username, owner=user, owner_username=user.username, status=True, status_detail='active', id=1) entity.save() print 'entity created: ', entity.entity_name # loop through all the tables and populate # the entity field only if it's null. models = get_models() table_updated = [] for model in models: if TendenciBaseModel in model.__bases__: if hasattr(model, 'entity'): table_name = model._meta.db_table for row in model.objects.all(): if not row.entity: row.entity = entity row.save() table_updated.append(table_name) if verbosity >= 2: print print 'List of tables updated: ' print '\n'.join(table_updated) print # GROUP - check if we have a group associated with group_exists = Group.objects.filter( name=site_display_name).exists() if not group_exists: group = Group(name=site_display_name, entity=entity, type='distribution', email_recipient=site_contact_email, allow_anonymous_view=True, creator=user, creator_username=user.username, owner=user, owner_username=user.username, status=True, status_detail='active') group.save() print 'Group created: ', group.name print 'All done.'
def handle(self, *args, **options): from tendenci.apps.entities.models import Entity from tendenci.apps.user_groups.models import Group from tendenci.apps.site_settings.utils import get_setting from tendenci.apps.perms.models import TendenciBaseModel verbosity = int(options['verbosity']) [entity] = Entity.objects.filter(pk=1)[:1] or [None] [user] = User.objects.filter(pk=1)[:1] or [None] site_display_name = get_setting('site', 'global', 'sitedisplayname') if not site_display_name: site_display_name = 'Default' site_contact_name = get_setting('site', 'global', 'sitecontactname') site_contact_email = get_setting('site', 'global', 'sitecontactemail') site_phone_number = get_setting('site', 'global', 'sitephonenumber') site_url = get_setting('site', 'global', 'siteurl') # if there is no entity, create one. if not entity: params = {'id': 1, 'entity_name': site_display_name, 'entity_type': '', 'contact_name': site_contact_name, 'phone': site_phone_number, 'email': site_contact_email, 'fax': '', 'website': site_url, 'summary': '', 'notes': '', 'admin_notes': 'system auto created', 'allow_anonymous_view': True, 'status': True, 'status_detail': 'active' } if user: params.update({'creator': user, 'creator_username': user.username, 'owner': user, 'owner_username': user.username }) else: params.update({'creator_username': '', 'owner_username': '' }) entity = Entity(**params) entity.save() print('entity created: ', entity.entity_name) # loop through all the tables and populate # the entity field only if it's null. models = apps.get_models() # exclude legacy tables tables_excluded = ['corporate_memberships_corporatemembership', 'corporate_memberships_corporatemembershiparchive'] table_updated = [] for model in models: if TendenciBaseModel in model.__bases__: if hasattr(model, 'entity'): table_name = model._meta.db_table if table_name in tables_excluded: continue for row in model.objects.all(): if not row.entity: row.entity = entity row.save(update_fields=['entity']) table_updated.append(table_name) if verbosity >= 2: print() print('List of tables updated: ') print('\n'.join(table_updated)) print() # GROUP - check if we have a group associated with group_exists = Group.objects.filter(entity=entity).exists() if not group_exists: params = {'name': site_display_name, 'entity': entity, 'type': 'distribution', 'email_recipient': site_contact_email, 'allow_anonymous_view': True, 'status': True, 'status_detail': 'active' } if user: params.update({'creator': user, 'creator_username': user.username, 'owner': user, 'owner_username': user.username }) else: params.update({'creator_username': '', 'owner_username': '' }) group = Group(**params) try: group.save() print('Group created: ', group.name) except Exception as e: print(e) print('All done.')