def get_last_ten_worked(self): """Returns last ten worked boxes data.""" entries = None if has_permission('manage'): entries = BoxEngine.get_all_boxes() else: entries = BoxEngine.get_all_user_boxes( request.identity['user']._id) entries = sorted(entries, key=lambda x: x.datetime_of_modify, reverse=True) entries = entries[:5] data = [] for entry in entries: box = {} box['box_id'] = entry.box_id box['name'] = entry.name box['status'] = entry.status box['datetime_of_creation'] = entry.datetime_of_creation.strftime( "%Y-%m-%d %H:%M") box['datetime_of_modify'] = entry.datetime_of_modify.strftime( "%Y-%m-%d %H:%M") data.append(box) return json.dumps(data)
def index(self, came_from=lurl('/'), failure=None, login=''): """Handle the front-page.""" if request.identity: has_boxes = False if has_permission('manage'): all_boxes = BoxEngine.get_number_of_all_boxes() if all_boxes['created'] > 0 or all_boxes[ 'stopped'] > 0 or all_boxes['started'] > 0: has_boxes = True else: my_boxes = BoxEngine.get_number_of_user_boxes( request.identity['user']._id) if my_boxes['created'] > 0 or my_boxes[ 'stopped'] > 0 or my_boxes['started'] > 0: has_boxes = True return dict(page='index', has_boxes=has_boxes) else: error_msg = None if failure is not None: if failure == 'user-not-found': error_msg = "Пользователь не найден".decode('utf8') elif failure == 'invalid-password': error_msg = "Некорректный пароль".decode('utf8') else: error_msg = "Ошибка авторизации".decode('utf8') return dict(page='index', came_from=came_from, login=login, error_msg=error_msg)
def get(self): """Returns boxes data.""" entries = None data = [] if has_permission('manage'): entries = BoxEngine.get_all_boxes() for entry in entries: box = [] box.append(entry.box_id) box.append(entry.name) box.append(entry.status) box.append(entry.user.display_name) box.append( entry.datetime_of_creation.strftime("%Y-%m-%d %H:%M")) box.append(entry.datetime_of_modify.strftime("%Y-%m-%d %H:%M")) data.append(box) else: entries = BoxEngine.get_all_user_boxes( request.identity['user']._id) for entry in entries: box = [] box.append(entry.box_id) box.append(entry.name) box.append(entry.status) box.append( entry.datetime_of_creation.strftime("%Y-%m-%d %H:%M")) box.append(entry.datetime_of_modify.strftime("%Y-%m-%d %H:%M")) data.append(box) json_entries = {} json_entries['data'] = data return json.dumps(json_entries)
def update_status(self): """Update status of boxes.""" try: if has_permission('manage'): BoxEngine.update_all_boxes_status() else: BoxEngine.update_all_user_boxes_status( request.identity['user']._id) except Exception as ex: return HTTPServerError(ex.message)
def get_number_of_all_boxes(self): """Returns number of all boxes.""" if request.identity: return json.dumps(BoxEngine.get_number_of_all_boxes()) else: return None
def get_load_value(self): """Returns server load value.""" if request.identity: return json.dumps(BoxEngine.get_server_load_value()) else: return None
def get_number_of_user_boxes(self): """Returns number of all users boxes.""" if request.identity: return json.dumps( BoxEngine.get_number_of_user_boxes( request.identity['user']._id)) else: return None
def list(self): """Handle the box list page.""" has_boxes = False if has_permission('manage'): all_boxes = BoxEngine.get_number_of_all_boxes() if all_boxes['created'] > 0 or all_boxes[ 'stopped'] > 0 or all_boxes['started'] > 0: has_boxes = True else: my_boxes = BoxEngine.get_number_of_user_boxes( request.identity['user']._id) if my_boxes['created'] > 0 or my_boxes['stopped'] > 0 or my_boxes[ 'started'] > 0: has_boxes = True return dict(page='list', has_boxes=has_boxes)
def delete(self, box_id): """Delete box.""" box_id = int(box_id) try: if not has_permission('manage'): if not BoxEngine.is_author( request.identity['repoze.who.userid'], box_id): return HTTPForbidden( "У Вас нет прав доступа для выполнения действий с этой виртуальной средой" ) BoxEngine.delete_box(box_id) model.History.add_record( request.identity['repoze.who.userid'], None, "Удаление виртуальной среды #" + str(box_id)) except Exception as ex: return HTTPServerError(ex.message)
def id(self, box_id): """Handle the box page.""" box_id = int(box_id) try: if not has_permission('manage'): if not BoxEngine.is_author( request.identity['repoze.who.userid'], box_id): return HTTPForbidden( "У Вас нет прав доступа для выполнения действий с этой виртуальной средой" ) box = BoxEngine.get_box_by_id(box_id) vagrantfile = BoxEngine.get_vagrantfile_data(box_id) host = get_hostname() except Exception as ex: return HTTPServerError(ex.message) return dict(page='id', box=box, vagrantfile=vagrantfile, host=host)
def copy(self, copied_box_id): """Copy box.""" copied_box_id = int(copied_box_id) try: if not has_permission('manage'): if not BoxEngine.is_author( request.identity['repoze.who.userid'], copied_box_id): return HTTPForbidden( "У Вас нет прав доступа для выполнения действий с этой виртуальной средой" ) box_id = BoxEngine.copy_box(request.identity['repoze.who.userid'], copied_box_id) model.History.add_record( request.identity['repoze.who.userid'], box_id, "Копирование виртуальной среды #" + str(copied_box_id) + " (создана #" + str(box_id) + ")") except Exception as ex: return HTTPServerError(ex.message)
def update_vagrantfile(self, box_id, vagrantfile_text): """Update vagrantfile.""" box_id = int(box_id) try: if not has_permission('manage'): if not BoxEngine.is_author( request.identity['repoze.who.userid'], box_id): return HTTPForbidden( "У Вас нет прав доступа для выполнения действий с этой виртуальной средой" ) BoxEngine.update_vagrantfile(box_id, vagrantfile_text) model.History.add_record( request.identity['repoze.who.userid'], box_id, "Изменение Vagrantfile виртуальной среды #" + str(box_id)) return BoxEngine.get_vagrantfile_data(box_id) except Exception as ex: return HTTPServerError(ex.message)
def create_from_parameters(self): """Create box from given parameters.""" num_of_copies = int(request.POST['num-of-copies']) box_name = request.POST['box-name'] vagrantfile = request.POST['vagrantfile-text'] for _ in range(num_of_copies): box_id = BoxEngine.create_box_from_parameters( box_name, request.identity['repoze.who.userid'], str(vagrantfile)) model.History.add_record( request.identity['repoze.who.userid'], box_id, "Создание виртуальной среды #" + str(box_id)) return HTTPFound(location='/box/list/')