def project_generate(request, project_name): """ generate code of project :param request: request object :param project_name: project name :return: json of generated project """ if request.method == 'POST': # get configuration configuration = Project.objects.get(name=project_name).configuration if not configuration: return JsonResponse({'message': 'Invalid configuration'}, status=500) configuration = json.loads(configuration) if not is_valid_name(project_name): return JsonResponse({'message': 'Invalid project name'}, status=500) # remove original project dir project_dir = join(PROJECTS_FOLDER, project_name) if exists(project_dir): rmtree(project_dir) # generate project copy_tree(join(TEMPLATES_DIR, 'project'), project_dir) move(join(PROJECTS_FOLDER, project_name, 'module'), join(project_dir, project_name)) for paths in TEMPLATES_TO_RENDER: path = join(*paths) tplfile = join( project_dir, string.Template(path).substitute(project_name=project_name)) vars = { 'project_name': project_name, 'items': configuration.get('items'), } render_template(tplfile, tplfile.rstrip('.tmpl'), **vars) # generate spider spiders = configuration.get('spiders') for spider in spiders: source_tpl_file = join(TEMPLATES_DIR, 'spiders', 'crawl.tmpl') new_tpl_file = join(PROJECTS_FOLDER, project_name, project_name, 'spiders', 'crawl.tmpl') spider_file = "%s.py" % join(PROJECTS_FOLDER, project_name, project_name, 'spiders', spider.get('name')) copy(source_tpl_file, new_tpl_file) render_template(new_tpl_file, spider_file, spider=spider, project_name=project_name) # save generated_at attr model = Project.objects.get(name=project_name) model.generated_at = timezone.now() # clear built_at attr model.built_at = None model.save() # return model return JsonResponse(model_to_dict(model))
def project_generate(request, project_name): """ generate code of project :param request: request object :param project_name: project name :return: json of generated project """ if request.method == 'POST': # get configuration configuration = Project.objects.get(name=project_name).configuration configuration = json.loads(configuration) if not is_valid_name(project_name): return JsonResponse({'message': 'Invalid project name'}, status=500) # remove original project dir project_dir = join(PROJECTS_FOLDER, project_name) if exists(project_dir): rmtree(project_dir) # generate project copy_tree(join(TEMPLATES_DIR, 'project'), project_dir) move(join(PROJECTS_FOLDER, project_name, 'module'), join(project_dir, project_name)) for paths in TEMPLATES_TO_RENDER: path = join(*paths) tplfile = join(project_dir, string.Template(path).substitute(project_name=project_name)) vars = { 'project_name': project_name, 'items': configuration.get('items'), } render_template(tplfile, tplfile.rstrip('.tmpl'), **vars) # generate spider spiders = configuration.get('spiders') for spider in spiders: source_tpl_file = join(TEMPLATES_DIR, 'spiders', 'crawl.tmpl') new_tpl_file = join(PROJECTS_FOLDER, project_name, project_name, 'spiders', 'crawl.tmpl') spider_file = "%s.py" % join(PROJECTS_FOLDER, project_name, project_name, 'spiders', spider.get('name')) copy(source_tpl_file, new_tpl_file) render_template(new_tpl_file, spider_file, spider=spider, project_name=project_name) # save generated_at attr model = Project.objects.get(name=project_name) model.generated_at = timezone.now() # clear built_at attr model.built_at = None model.save() # return model return JsonResponse(model_to_dict(model))