def create_project(self, project_path): """ Create Trionyx project in given path :param str path: path to create project in. :raises FileExistsError: """ shutil.copytree(self.project_path, project_path) self.update_file(project_path, 'requirements.txt', {'trionyx_version': trionyx.__version__}) self.update_file(project_path, 'environment.json', {'secret_key': utils.random_string(32)}) app_name = os.path.basename(project_path).capitalize() self.update_file( project_path, 'README.rst', { 'title': "{name}\n{heading}".format( name=app_name, heading='=' * len(app_name), ) }) self.update_file( project_path, 'config/settings/base.py', { 'app_name': app_name, 'logo_name_start': app_name[0], 'logo_name_end': app_name[1:], 'logo_name_small_start': app_name[0].upper(), 'logo_name_small_end': app_name[1].upper(), })
def get_context_data(self, *args, **kwargs): """Get dashboard context data""" context = super().get_context_data(*args, **kwargs) dashboard = self.request.user.get_attribute('tx_dashboard', []) if not dashboard: for widget in (settings.TX_DEFAULT_DASHBOARD() if callable(settings.TX_DEFAULT_DASHBOARD) else settings.TX_DEFAULT_DASHBOARD): widget = widget.copy() widget['i'] = utils.random_string(16) dashboard.append(widget) self.request.user.set_attribute('tx_dashboard', dashboard) context.update({ 'widget_templates': [widget().template for index, widget in widgets.items()], 'widgets': [{ 'code': widget.code, 'name': str(widget.name), 'description': str(widget.description), 'image': static(widget().image), 'config_fields': widget().config_fields, 'default_w': widget.default_width, 'default_h': widget.default_height, 'fixed_w': widget.fixed_width, 'fixed_h': widget.fixed_height, 'is_resizable': widget.is_resizable, } for index, widget in widgets.items() if widget.is_visible(self.request)], 'dashboard': dashboard, }) return context
def create_ansible(self, project_path, domain, repo): """Create Ansible live deploy script""" shutil.copytree(self.ansible_path, project_path) self.update_file(project_path, 'production', { 'domain': domain, }) os.mkdir(os.path.join(project_path, 'ssh_keys')) subprocess.check_output([ 'ssh-keygen', '-t', 'rsa', '-b', '4096', '-N', '', '-f', os.path.join(project_path, 'ssh_keys', 'deploy_rsa') ]) subprocess.check_output([ 'ssh-keygen', '-t', 'rsa', '-b', '4096', '-N', '', '-f', os.path.join(project_path, 'ssh_keys', 'connect_rsa') ]) with open(os.path.join(project_path, 'ssh_keys', 'deploy_rsa')) as _file: deploy_key = _file.read() config = { 'ansible_ssh_user': '******', 'ansible_ssh_port': 6969, 'ansible_ssh_private_key_file': 'ssh_keys/connect_rsa', 'app_domain': domain, 'app_repo': repo, 'app_config_template': "{{ playbook_dir }}/templates/environment.json.j2", 'deploy_key': deploy_key, 'secret_key': utils.random_string(32), 'db_password': utils.random_string(16), 'broker_password': utils.random_string(16), 'smtp_password': '', } self.update_file( project_path, 'group_vars/all.yml', { 'config': yaml.dump(config, default_flow_style=False, allow_unicode=True), })
def __init__(self, objects, *fields, **options): """Init Chart""" options['id'] = options.pop('id', utils.random_string(8)) super().__init__(**options) self.chart_data = {} self.chart_scales = {} self.height = options.get('height', '300px') self.options = options self.objects = objects """Can be string with field name relation, Queryset or list""" self.fields = fields # Set all colors from, default first color is theme self.colors = { 'blue': { 'fill': 'rgba(60, 141, 188, 0.2)', 'stroke': 'rgba(60, 141, 188, 1)', }, 'yellow': { 'fill': 'rgba(243, 156, 18, 0.2)', 'stroke': 'rgba(243, 156, 18, 1)', }, 'green': { 'fill': 'rgba(0, 166, 90, 0.2)', 'stroke': 'rgba(0, 166, 90, 1)', }, 'purple': { 'fill': 'rgba(96, 92, 168, 0.2)', 'stroke': 'rgba(96, 92, 168, 1)', }, 'red': { 'fill': 'rgba(221, 75, 57, 0.2)', 'stroke': 'rgba(221, 75, 57, 1)', }, 'black': { 'fill': 'rgba(17, 17, 17, 0.2)', 'stroke': 'rgba(17, 17, 17, 1)', }, } self.color_order = [ 'blue', 'yellow', 'green', 'purple', 'red', 'black' ] self.theme_color = tx_settings.THEME_COLOR.replace('-light', '')
def render(self, form, form_style, context, **kwargs): """Render template""" value = form.data.get(self.name, form.initial.get(self.name, '[]')) if not value: value = '[]' return render_to_string( 'trionyx/forms/filters.html', { **context.flatten(), 'uuid': utils.random_string(8), 'name': self.name, 'value': value, 'content_type_input_id': self.content_type_input_id, 'content_type_id': ContentType.objects.get_for_model(self.model).id if self.model else -1 })
def test_random_string(self): self.assertNotEqual(utils.random_string(8), utils.random_string(8)) self.assertEqual(len(utils.random_string(16)), 16)
def __init__(self, dependencies, *args, **kwargs): """Init""" super().__init__(*args, **kwargs) self.dependencies = dependencies self.css_id = 'depend-' + utils.random_string(8)
def __init__(self, queryset, *args, **kwargs): """Init""" self.field_id = utils.random_string(16) ModelAjaxChoiceField.registered_fields[self.field_id] = self super().__init__(queryset, *args, **kwargs)