def project_build(request, project_name): """ get build info or execute build operation :param request: request object :param project_name: project name :return: json """ # get project folder path = os.path.abspath(join(os.getcwd(), PROJECTS_FOLDER)) project_path = join(path, project_name) # get build version if request.method == 'GET': egg = find_egg(project_path) # if built, save or update project to db if egg: built_at = timezone.datetime.fromtimestamp(os.path.getmtime(join(project_path, egg)), tz=pytz.timezone(TIME_ZONE)) if not Project.objects.filter(name=project_name): Project(name=project_name, built_at=built_at, egg=egg).save() model = Project.objects.get(name=project_name) else: model = Project.objects.get(name=project_name) model.built_at = built_at model.egg = egg model.save() # if not built, just save project name to db else: if not Project.objects.filter(name=project_name): Project(name=project_name).save() model = Project.objects.get(name=project_name) # transfer model to dict then dumps it to json data = model_to_dict(model) return JsonResponse(data) # build operation manually by clicking button elif request.method == 'POST': data = json.loads(request.body) description = data['description'] build_project(project_name) egg = find_egg(project_path) if not egg: return JsonResponse({'message': 'egg not found'}, status=500) # update built_at info built_at = timezone.now() # if project does not exists in db, create it if not Project.objects.filter(name=project_name): Project(name=project_name, description=description, built_at=built_at, egg=egg).save() model = Project.objects.get(name=project_name) # if project exists, update egg, description, built_at info else: model = Project.objects.get(name=project_name) model.built_at = built_at model.egg = egg model.description = description model.save() # transfer model to dict then dumps it to json data = model_to_dict(model) return JsonResponse(data)
def project_build(request, project): print(request.body) path = os.path.abspath(merge(os.getcwd(), PROJECTS_FOLDER)) project_path = merge(path, project) if request.method == 'GET': print(project) egg = find_egg(project_path) print('Egg', egg) if egg: built_at = os.path.getmtime(merge(project_path, egg)) print(built_at) if not Project.objects.filter(name=project): Project(name=project, built_at=built_at, egg=egg).save() model = Project.objects.get(name=project) else: model = Project.objects.get(name=project) model.built_at = built_at model.egg = egg model.save() print(model) dict = model_to_dict(model) print(dict) del dict['clients'] localtime = time.localtime(int(built_at)) dict['built_at'] = time.strftime('%Y-%m-%d %H:%M:%S', localtime) return HttpResponse(json.dumps(dict)) else: return HttpResponse(json.dumps({'name': project})) elif request.method == 'POST': data = json.loads(request.body) print(data) description = data['description'] print(description) build_project(project) egg = find_egg(project_path) built_at = time.time() if not Project.objects.filter(name=project): Project(name=project, description=description, built_at=built_at, egg=egg).save() model = Project.objects.get(name=project) else: model = Project.objects.get(name=project) model.built_at = built_at model.egg = egg model.description = description model.save() print(model) dict = model_to_dict(model) print(dict) del dict['clients'] localtime = time.localtime(int(built_at)) dict['built_at'] = time.strftime('%Y-%m-%d %H:%M:%S', localtime) return HttpResponse(json.dumps(dict))