def update_contentpageid(): error = None form = contentpage_idform.ContentpageidForm() instance = SysInfo() author = g.user if (request.method == 'POST' and author is not None and request.form['save']): if form.validate_on_submit(): # we assign record time to this form element # as unicode string to be consistent with other form elements here form.content_date.data = unicode(instance.altertime()) conn = engine.connect() stmt = update(Content).where(Content.id == form.id.data).values( id=form.id.data, content_title=form.content_title.data, content_author=form.content_author.data, content_category=form.content_category.data, content_date=form.content_date.data, content_text=form.content_text.data) try: conn.execute(stmt) flash("Content is changed", 'info') except exc.IntegrityError: flash("Content with same name is exist", 'error') return redirect(url_for('show_contentpageid', id=form.id.data)) else: flash("Content is not changed", 'error') return redirect(url_for('show_contentpageid')) return render_template('adminboard/editpage_id_content.html', form=form, error=error)
def show_dashboard_inner(): per_page = 9 paginator = Paginator() form = dashboard_itemsform.DashboardItemsForm() servername = socket.gethostname() approot = os.path.split(app.root_path) users = g.user instance = SysInfo() freespace = instance.diskspace() ltime = instance.systime() atime = instance.altertime() pages = request.args.get('page', type=int, default=1) if not users and pages == None: abort(404) else: contents_loop = sql.session.query(Content).limit( (per_page)).offset( (pages - 1) * per_page).all() pagination = paginator.paginate_queries(Content, pages, per_page) return render_template('adminboard/adminboard_inner.html', servername=servername, approot=approot[-2], freespace=freespace, users=users, ltime=ltime, atime=atime, contents_loop=contents_loop, form=form, pagination=pagination )
def __init__(self): self.backup_path = app.config['BACKUP_PATH'] self.mysql_export_path = "{}/{}.sql".format(self.backup_path, app.config['DBNAME']) self.site_path = os.path.split(self.backup_path) self.site_parent_path = app.root_path.strip('application') self.instance = SysInfo() self.file_output = "{}-{}.tar.gz".format( app.config['DBNAME'], str(self.instance.altertime()))
def show_dashboard_filenamanager(): browser = FileBrowser() per_page = 9 pages = request.args.get('page', type=int, default=1) paginator = Paginator() form = dashboard_filesform.DashboardFilesForm() servername = socket.gethostname() approot = os.path.split(app.root_path) users = g.user instance = SysInfo() freespace = instance.diskspace() ltime = instance.systime() atime = instance.altertime() f = None get_relpath = None if session['login'] != 'admin': flash("You don't have administrator privilegies!", 'error') return redirect(url_for('show_dashboard')) if not users and pages is None: abort(404) else: if exists("{}/static/get_path.tmp".format(app.root_path)) is False: get_relpath = "/static/images/" elif exists("{}/static/get_path.tmp".format(app.root_path)): with open("{}/static/get_path.tmp".format(app.root_path), 'r') as f: get_relpath = f.read() limit = per_page if get_relpath == '': get_relpath = "/static/images/" files = browser.show_files(get_relpath) offset = ((pages - 1) * per_page) if pages == 0 or pages == 1: f = files[:limit] elif len(files) - offset > offset: f = files[offset:-offset] elif len(files) - offset < offset: f = files[offset:] pagination = paginator.paginate(files, pages, per_page) return render_template('adminboard/adminboard_filemanager.html', servername=servername, approot=approot[-2], freespace=freespace, users=users, ltime=ltime, atime=atime, files=f, get_relpath=get_relpath, form=form, pagination=pagination)
def __init__(self): self.get_dirname = app.root_path self.filename = None self.old_object = None self.new_object = None self.obj = None self.attr = None self.get_path = None self.sbytes = None self.sysinfo = SysInfo()
def show_categorypageid(id): form = categorypage_idform.CategorypageidForm() editpage_output_loop = sql.session.query(Categories).filter_by( id=id).first() instance = SysInfo() atime = instance.altertime() author = g.user if (editpage_output_loop and atime and author is not None): return render_template('adminboard/editpage_id_category.html', editpage_output_loop=editpage_output_loop, form=form) else: return redirect(url_for('show_login'))
def show_editpage_category(): form = categorypageform.CategorypageAddForm() editpage_output_loop = Categories.query.filter_by( category_author='admin').first() instance = SysInfo() atime = instance.altertime() author = g.user if (editpage_output_loop and atime and author is not None): return render_template('adminboard/editpage_category.html', author=author, atime=atime, editpage_output_loop=editpage_output_loop, form=form) else: return redirect(url_for('show_login'))
class Backup: def __init__(self): self.backup_path = app.config['BACKUP_PATH'] self.mysql_export_path = "{}/{}.sql".format(self.backup_path, app.config['DBNAME']) self.site_path = os.path.split(self.backup_path) self.site_parent_path = app.root_path.strip('application') self.instance = SysInfo() self.file_output = "{}-{}.tar.gz".format( app.config['DBNAME'], str(self.instance.altertime())) def db_backup(self): if (os.path.isdir(self.backup_path) and os.access(self.backup_path, os.W_OK) and os.path.isdir(self.site_parent_path)): command = 'mysqldump --opt -h {} -u {} -p{} {} > {}'.format( app.config['DBHOST'], app.config['DBUSERNAME'], app.config['DBPASSWORD'], app.config['DBNAME'], self.mysql_export_path) os.system(command) with tarfile.open( "{}{}".format(self.backup_path, self.file_output), "w:gz") as tar: tar.add(self.site_parent_path) return "Backup is created: {}".format(self.file_output) else: msg = "No correct data for export the database or destination/site parent path isn't exist.\n" \ "Backup Path: {}\n" \ "Backup Path Access: {}\n" \ "Please, contact to administrator \n".format(self.backup_path, os.access(self.backup_path, os.W_OK)) return msg
def show_userspageid(id): form = userspage_idform.UsersidForm() editpage_output_loop = Users.query.filter_by(id=id).first() categories_loop = Categories.query.all() instance = SysInfo() atime = instance.altertime() author = g.user if (editpage_output_loop and categories_loop and atime and author is not None): return render_template('adminboard/editpage_id_users.html', atime=atime, editpage_output_loop=editpage_output_loop, form=form) else: return redirect(url_for('show_login'))
def show_dashboard(): if not current_user.is_authenticated: return current_app.login_manager.unauthorized() else: servername = socket.gethostname() approot = os.path.split(app.root_path) users = g.user instance = SysInfo() freespace = instance.diskspace() ltime = instance.systime() atime = instance.altertime() flash('Welcome, ' + users, 'info') return render_template('adminboard/adminboard.html', servername=servername, approot=approot[-2], freespace=freespace, users=users, ltime=ltime, atime=atime)
class FileBrowser: def __init__(self): self.get_dirname = app.root_path self.filename = None self.old_object = None self.new_object = None self.obj = None self.attr = None self.get_path = None self.sbytes = None self.sysinfo = SysInfo() def allowed_file(self, filename): self.filename = filename return '.' in self.filename and \ self.filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS def make_dir(self, get_dirname): self.get_dirname = get_dirname if type(self.get_dirname) == str: return mkdir(self.get_dirname) else: return False def rename_file_dir(self, old_object, new_object): self.old_object = old_object self.new_object = new_object if (isinstance(self.old_object, str) and isinstance(self.new_object, str)): return rename(self.old_object, self.new_object) else: return False def del_file_dir(self, get_path): self.get_dirname = get_path if isinstance(self.get_dirname, str): if isdir("{}".format(self.get_dirname)): return shutil.rmtree(self.get_dirname) elif isfile("{}".format(self.get_dirname)): return remove("{}".format(self.get_dirname)) else: return False def move_file_dir(self, get_path): pass # TODO: shutil.copytree and shutil.move def file_size(self, file_path): """ Return the file size """ if isfile(file_path): file_info = stat(file_path) return self.sysinfo.conv_bytes(file_info.st_size) def file_attr(self, object, attribute): """ Return file attributes """ self.obj = object self.attr = attribute if self.attr == 'st_ctime': ct = stat(self.obj) return "{}".format(datetime.fromtimestamp(ct.st_ctime)) elif self.attr == 'st_mode': mo = stat(self.obj) return oct(mo.st_mode)[-3:] elif self.attr == 'st_uid': uid = stat(self.obj) return getpwuid(uid.st_uid).pw_name else: return False def show_files(self, get_path): """ Shows files for the dashboard media section """ # get_path is unicode string, make it simple string self.get_path = str(get_path) files = [] if (isinstance(self.get_path, str) and isdir("{}{}".format(app.root_path, self.get_path))): # if no slash at the end of path if self.get_path.endswith("/") is False: self.get_path = "{}/".format(self.get_path) f_list = listdir("{}{}".format(app.root_path, self.get_path)) for num, file_x, in enumerate(f_list, 1): inner = [{ "id": "{}".format(num), "relpath": "{}{}".format(self.get_path, file_x), "name": "{}".format(file_x), "owner": self.file_attr( "{}/{}{}".format(app.root_path, self.get_path, file_x), 'st_uid'), "size": self.file_size(("{}/{}{}".format(app.root_path, self.get_path, file_x))), "date": self.file_attr( "{}/{}{}".format(app.root_path, self.get_path, file_x), 'st_ctime'), "perm": self.file_attr( "{}/{}{}".format(app.root_path, self.get_path, file_x), 'st_mode') }] files += inner return files def change_own(self, get_path): """ Change file owner & permissions """ self.get_path = get_path if isinstance(self.get_path, str): return chmod(self.get_path) elif isinstance(self.get_path, list): for files in self.get_path: per_file = files per_path = getcwd() if isinstance(per_file, str) and isinstance(per_path, str): file_array = "{}/{}".format(per_path, per_file) chmod(file_array) else: break return "input objects are not strings" else: return "Incorrect input object"