def generate_component(component: SingleFileComponent, path: str): file_path = os.path.join(path, component.path) os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, 'w+') as file: log('writing to ' + file_path) file.write(component.render())
def handle(self, *args, **options): path = get_output_path() for module in options.get('modules', []): log(f'Generating views for {module}') module = get_resolver(module) generate_views(module.url_patterns, path)
def from_template(cls, view): model = '' # workaround for state variance. No default value is set for object # django/views/generic/detail.py:144 # django 1.11 if hasattr(view, 'model'): view.object = None view.object_list = view.model.objects.all() model = view.model.__name__ app = get_app_name(view) candidates = view.get_template_names() use_generic_html = True try: template_path = loader.select_template(candidates).template.name use_generic_html = False log('Creating component from template:') except TemplateDoesNotExist: log('No Template found. Blindly creating component from first ' 'template candidate') template_path = candidates[0] name = convert_file_to_component_name(template_path) component = ModuleComponent('generic', app, name, renderer=CBVRenderer) model and component.add_context({'model': model}) if not use_generic_html: component.renderer.html_template = template_path return component
def write(self, payload): file_path = self.path os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, 'w+') as file: log('writing to ' + file_path) file.write(payload)
def handle(self, *args, **options): stuff = { 'components': 'components', 'store': 'store', 'views': 'views', 'util': 'util.js', 'main': 'main.js', } base = 'djue/core/' for option in options['modules']: module = stuff.get(option) if module is None: log(f'"{option}" is an invalid core module') log(f'Please select from: {stuff.keys()}') continue path = base + module if os.path.isdir(path): for dirname, dirs, files in os.walk(path): print(dirname) return c = CoreComponent('djue/core/components/Serializer.vue', app='', name='') print(c.root)
def handle(self, *args, **options): path = get_output_path() for module in options.get('modules', []): log(f'Generating component: {module}') form = import_string(module) component = ComponentFactory.from_form(form) generate_component(component, path)
def handle(self, *args, **options): path = get_output_path() for module in options.get('modules', []): log(f'Generating view: {module}\n') view = import_string(module) component = ViewFactory.from_view(view) generate_component(component, path)
def from_form(cls, form_class): log('Creating form_class component') form_class.as_vue = as_vue name = form_class.__name__ model = form_class._meta.model.__name__ app = get_app_name(form_class) component = ModuleComponent('component', app, name) component.add_context({'model': model, 'form': form_class()}) return component
def from_callback(cls, callback): if hasattr(callback, 'actions'): return cls.from_viewset(callback) components = [ComponentFactory.from_callback(callback)] app = get_app_name(callback) if components in [[None], []]: log(f'No component generated for {callback.__name__}') return if hasattr(callback, 'view_class'): name = callback.view_class.__name__ else: name = convert_to_camelcase(callback.__name__) return View(components, app, name)
def handle(self, *args, **options): path = get_output_path() for module in options.get('modules', []): log(f'Generating store module: {module}\n') form = import_string(module) if hasattr(form, '_meta'): component = StoreFactory.from_form(form) elif hasattr(form, 'Meta'): component = StoreFactory.from_serializer(form) else: log(f'Could not create vue store from {module}') continue generate_component(component, path)
def from_drf_view(cls, view): log(f'Creating from DRF class: {view}') if isinstance(view, ModelViewSet): serializer = view.get_serializer_class() return cls.from_serializer(serializer) if isinstance(view, APIRootView): return StaticFile(template='djue/raw/APIRootView.vue', output_path='modules/rest_framework/components') raise Exception( 'Sumpin done screwed up while trying to create a component from a' 'DRF class. It is not your fault though (unless you are me). the ' 'stupid author of this library clearly is aware of the this case. ' 'Hell, it is not even an edge case. it is a common scenario for ' 'anyone using basic DRF APIViews. I will get around to it soon')
def generate_components(patterns, path): for url in patterns: log(f'url: {url.regex.pattern}') if isinstance(url, RegexURLResolver): log('URL Resolver found! Stepping down the rabbit hole...') generate_components(url.url_patterns, path) continue callback = url.callback if hasattr(callback, 'actions'): for method, action in callback.actions.items(): comp, form = ComponentFactory.from_junk( callback, method, action) comp.add_context({'route': url.name}) comp.write() form and form.write() continue component, form = ComponentFactory.from_callback(callback) if not component: log(f'No Component was generated for: {str(url)}') continue component.add_context({'route': url.name}) component.write() form and form.write()
def generate_views(patterns, path): for url in patterns: log(f'url: {url.regex.pattern}') if isinstance(url, RegexURLResolver): log('URL Resolver found! Stepping down the rabbit hole...') generate_views(url.url_patterns, path) continue callback = url.callback if hasattr(callback, 'actions'): log('Generating views from DRF ViewSet...') component = ViewFactory.from_viewset(callback) else: component = ViewFactory.from_callback(callback) if not component: log(f'No CrudComponent was generated for: {str(url)}') continue generate_component(component, path)
def from_callback(cls, callback): log("Creating from callback") if callback is None: log('Something strange happened. NoneType passed as callback. ' 'GTFO!') return if not hasattr(callback, 'view_class'): name = callback.__name__ app = get_app_name(callback) log(f'Creating Anonymous component for functional view: {name}') return ModuleComponent('generic', app, name) view = callback.view_class() if isinstance(view, APIView): return cls.from_drf_view(view) return cls.from_cbv(view)
def from_cbv(cls, view: View): if not isinstance(view, TemplateResponseMixin): log("CBV's must be of type TemplateResponseMixin.") log(f"Nothing created for:{TemplateResponseMixin}") return None, None log(f'Creating from CBV: {view.__module__}.{view.__class__}') form = None if isinstance(view, ModelFormMixin): form_class = view.get_form_class() form = cls.from_form(form_class) model = '' # workaround for state variance. No default value is set for object # django/views/generic/detail.py:144 # django 1.11 if hasattr(view, 'model'): view.object = None view.object_list = view.model.objects.all() model = view.model.__name__ app = get_app_name(view) name = view.__class__.__name__ + 'Component' if isinstance(view, generic.CreateView): action = 'create' elif isinstance(view, generic.DeleteView): action = 'destroy' elif isinstance(view, generic.ListView): action = 'list' elif isinstance(view, generic.UpdateView): action = 'update' elif isinstance(view, generic.DetailView): action = 'retrieve' else: log(f'Using generic template for {view}') action = 'generic' component = ModuleComponent(action, app, name, renderer=CBVRenderer) model and component.add_context({'model': model}) form and component.add_context({'component': form}) candidates = view.get_template_names() try: template_path = loader.select_template(candidates).template.name component.renderer.html_template = template_path log('Using HTML template: ' + template_path) except TemplateDoesNotExist: log('Using generic template for action: ' + action) component.add_context({ 'context_obj_name': view.get_context_object_name(None) or 'object' }) return component, form