Receipt create. Part 2

master
alexey 2 months ago
parent 6527af5005
commit d608f73772

122
app.rb

@ -14,6 +14,7 @@ require_relative "classes/config"
require_relative "classes/gitinfo"
require_relative "classes/db"
require_relative "classes/systeminfo"
require_relative "classes/utilities"
def print_error_page(error_status, error_meaasge)
@page_name = "Ошибка выполнения"
@ -22,14 +23,6 @@ def print_error_page(error_status, error_meaasge)
halt erb(:page5xx)
end
def sanitize_filename(filename)
filename = filename.strip
sanitized = filename.gsub(/[^a-zA-Z0-9_]/, "_")
sanitized.gsub!(/_+/, "_")
sanitized.gsub!(/^_+|_+$/, "")
sanitized
end
cfg = IniConfig.new()
db = DBase.new()
@ -155,11 +148,118 @@ get "/recips" do
erb :receips
end
get "/recips/:rcp_id" do
repo = GitRepo.new(cfg.get_repo, db)
if repo.path.nil?
print_error_page(503, "Путь к репозиториям не существует")
else
@repo_data = repo.getrepos
@rcp_id = params["rcp_id"]
info = db.get_rcp_info_by_id(@rcp_id)
@page_name = info[:filepath]
@rcp_name = info[:filepath]
if session[:rcp_old_description].nil?
@old_filepath = info[:filepath]
else
@old_filepath = session[:rcp_old_filepath]
end
if session[:rcp_old_description].nil?
@old_description = info[:descr]
else
@old_description = session[:rcp_old_description]
end
if session[:rcp_old_codedata].nil?
@old_codedata = info[:content]
else
@old_codedata = session[:rcp_old_codedata]
end
if session[:rcp_old_gitlst].nil?
@old_gitlst = info[:repos_list]
else
@old_gitlst = session[:rcp_old_gitlst]
end
@error_data = session[:rcpcreate_error]
session[:rcpcreate_error] = nil
session[:rcp_old_filepath] = nil
session[:rcp_old_description] = nil
session[:rcp_old_codedata] = nil
session[:rcp_old_gitlst] = nil
erb :rcpedit
end
end
post "/recips/:rcp_id" do
rcp_id = params["rcp_id"]
session[:rcp_old_filepath] = params["filepath"]
session[:rcp_old_description] = params["description"]
session[:rcp_old_codedata] = params["codedata"]
session[:rcp_old_gitlst] = params["gitlst"]
if params["filepath"].nil? || params["description"].nil? || params["filepath"].strip == "" || params["description"].strip == ""
session[:rcpcreate_error] = "Имя рецепта и описание не должны быть пустыми"
redirect url("/recips/#{rcp_id}")
else
@error_data = db.updaterecip(rcp_id, params["filepath"], params["description"], params["codedata"], params["gitlst"])
unless @error_data.nil?
session[:rcpcreate_error] = @error_data
redirect url("/recips/#{rcp_id}")
else
redirect "/recips"
end
end
end
post "/rcpdelete/:rcp_id" do
input_name = params["rcpnamedup"]
rcp_id = params["rcp_id"]
info = db.get_rcp_info_by_id(rcp_id)
if info.nil?
print_error_page(404, "Рецепта не существует")
else
if info[:filepath] == input_name
db.delete_rcp(rcp_id)
end
redirect "/recips"
end
end
post "/rcpdelete" do
redirect "/recips"
end
get "/rcpcreate" do
@page_name = "Создать новый рецепт"
@error_data = session[:rcpcreate_error]
session[:gitcreate_error] = nil
erb :rcpcrt
@old_filepath = session[:rcp_old_filepath]
@old_description = session[:rcp_old_description]
@old_codedata = session[:rcp_old_codedata]
@old_gitlst = session[:rcp_old_gitlst]
repo = GitRepo.new(cfg.get_repo, db)
if repo.path.nil?
print_error_page(503, "Путь к репозиториям не существует")
else
@repo_data = repo.getrepos
@error_data = session[:rcpcreate_error]
session[:rcpcreate_error] = nil
erb :rcpcrt
end
end
post "/rcpcreate" do
session[:rcp_old_filepath] = params["filepath"]
session[:rcp_old_description] = params["description"]
session[:rcp_old_codedata] = params["codedata"]
session[:rcp_old_gitlst] = params["gitlst"]
if params["filepath"].nil? || params["description"].nil? || params["filepath"].strip == "" || params["description"].strip == ""
session[:rcpcreate_error] = "Имя рецепта и описание не должны быть пустыми"
redirect "/rcpcreate"
else
@error_data = db.createrecip(params["filepath"], params["description"], params["codedata"], params["gitlst"])
unless @error_data.nil?
session[:rcpcreate_error] = @error_data
redirect "/rcpcreate"
else
redirect "/recips"
end
end
end
not_found do

@ -17,8 +17,8 @@ class DBase
def creategit(project_name, description)
@error = nil
data = Repos.where(reponame: project_name)
unless data.nil?
data = Repos.where(reponame: project_name).first
if data.nil?
id = Repos.insert(reponame: project_name, descr: description, public: 1)
@last_id = id
else
@ -46,4 +46,66 @@ class DBase
Repos.where(id: id).delete
end
end
def createrecip(filepath, description, codedata, gitlist)
error_data = nil
filepath_san = sanitize_rcptname(filepath)
is_data = Recips.where(filepath: filepath_san).first
if codedata.nil? || codedata.strip == ""
error_data
else
if is_data.nil?
id = Recips.insert(filepath: filepath_san, descr: description, content: codedata)
@last_id = id
if !gitlist.nil? && gitlist.length > 0
gitlist.each do |item|
data = Repos.where(id: item.to_i).first
unless data.nil?
RepocRecips.insert(repo_id: data[:id], recip_id: id)
end
end
end
error_data
else
"Рецепт с таким именем #{filepath_san} уже существует"
end
end
end
def updaterecip(id, filepath, description, codedata, gitlist)
error_data = nil
filepath_san = sanitize_rcptname(filepath)
is_data = Recips.where(filepath: filepath_san).first
if codedata.nil? || codedata.strip == ""
error_data
else
unless is_data.nil?
Recips.where(id: id.to_i).update(filepath: filepath_san, descr: description, content: codedata)
RepocRecips.where(recip_id: id.to_i).delete
if !gitlist.nil? && gitlist.length > 0
gitlist.each do |item|
data = Repos.where(id: item.to_i).first
unless data.nil?
RepocRecips.insert(repo_id: data[:id], recip_id: id)
end
end
end
error_data
else
"Рецепт с таким именем #{filepath_san} не существует"
end
end
end
def get_rcp_info_by_id(rcpi_id)
info = Recips[rcpi_id.to_i]
gits = RepocRecips.where(recip_id: info[:id])
info[:repos_list] = gits.map { |item| item[:repo_id].to_s }
info
end
def delete_rcp(id)
RepocRecips.where(recip_id: id.to_i).delete
Recips.where(id: id.to_i).delete
end
end

@ -67,13 +67,13 @@ class GitRepo
db_info = @db.get_repo_info_by_name(repo_name)
unless db_info.nil?
db_info = db_info.first
repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public }
repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :id => db_info.id }
else
result = create_git_db_only(repo_name)
if result.nil?
db_info = @db.get_repo_info_by_name(repo_name)
db_info = db_info.first
repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public }
repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :id => db_info.id }
end
end
end

@ -0,0 +1,15 @@
def sanitize_filename(filename)
filename = filename.strip
sanitized = filename.gsub(/[^a-zA-Z0-9_]/, "_")
sanitized.gsub!(/_+/, "_")
sanitized.gsub!(/^_+|_+$/, "")
sanitized
end
def sanitize_rcptname(filename)
filename = filename.strip
sanitized = filename.gsub(/[^a-zA-Z0-9_\.]/, "_")
sanitized.gsub!(/_+/, "_")
sanitized.gsub!(/^_+|_+$/, "")
sanitized
end

@ -27,7 +27,7 @@ Sequel.migration do
primary_key :id
String :content, text: true
String :filepath, text: true
String :desct, text: true
String :descr, text: true
Datetime :create_at, default: Sequel.lit("CURRENT_TIMESTAMP")
end

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -7,6 +7,7 @@
<title><%= @page_name %></title>
<link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="/assets/css/Footer-Dark-icons.css">
<link rel="stylesheet" href="/js/jquery.dropdown.min.css">
<script src="/js/jquery-3.7.1.min.js"></script>
</head>
@ -20,6 +21,10 @@
</div>
<div class="container">
<div class="hstack gap-3">
<span class="p-1">
<a href="/">главная</a>
</span>
<span>|</span>
<span class="p-1">
<a href="/repos">git-репозитории</a>
</span>

@ -9,6 +9,7 @@
toolbar: "search, go_to_line, fullscreen, |, undo, redo, |, select_font,|, highlight, reset_highlight, word_wrap, |, syntax_selection"
});
</script>
<script src="/js/jquery.dropdown.min.js"></script>
<% unless @error_data.nil? %>
<div class="container">
<div class="alert alert-warning alert-dismissible fade show" role="alert"><span><%= @error_data %></span><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Закрыть"></button></div>
@ -18,19 +19,38 @@
<form action="/rcpcreate" method="post">
<div class="mb-3">
<label for="filepath" class="form-label">Имя рецепта (англиские буквы, _, .)</label>
<input type="email" class="form-control" id="filepath" name="filepath">
<input type="text" class="form-control" id="filepath" name="filepath" value="<%= @old_filepath %>">
</div>
<div class="mb-3">
<label for="description" class="form-label">Описание рецепта сборки</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
<textarea class="form-control" id="description" name="description" rows="3"><%= @old_description %></textarea>
</div>
<div class="mb-3">
<label for="codedata" class="form-label">Код</label>
<textarea class="form-control" id="codedata" name="codedata" rows="3"></textarea>
<textarea class="form-control" id="codedata" name="codedata" rows="3"><%= @old_codedata %></textarea>
</div>
<div class="mb-3">
<label for="gitlist" class="form-label">git проекты, использующие рецепт</label>
<div class="gitlist">
<select class="form-control" id="gitlst" name="gitlst[]" multiple>
<% @repo_data.each do |item| %>
<% if !@old_gitlst.nil? && @old_gitlst.include?(item[:id].to_s) %>
<option value="<%= item[:id] %>" selected><%= item[:reponame] %></option>
<% else %>
<option value="<%= item[:id] %>"><%= item[:reponame] %></option>
<% end %>
<% end %>
</select>
</div>
</div>
<div class="mb-3 text-center">
<button type="submit" class="btn btn-primary">Сохранить</button>
</div>
</form>
</div>
<script>
$('.gitlist').dropdown({
searchNoData: '<li style="color:#ddd">Нет данных</li>',
});
</script>
<%= erb :footer %>

@ -0,0 +1,125 @@
<%= erb :header %>
<script src="/js/edit_area_full.js"></script>
<script language="javascript" type="text/javascript">
editAreaLoader.init({
id: "codedata",
syntax: "bash",
start_highlight: true,
language: "ru",
toolbar: "search, go_to_line, fullscreen, |, undo, redo, |, select_font,|, highlight, reset_highlight, word_wrap, |, syntax_selection"
});
</script>
<script src="/js/jquery.dropdown.min.js"></script>
<% unless @error_data.nil? %>
<div class="container">
<div class="alert alert-warning alert-dismissible fade show" role="alert"><span><%= @error_data %></span><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Закрыть"></button></div>
</div>
<% end %>
<div class="container">
<form action="/recips/<%= ERB::Util.url_encode(@rcp_id) %>" method="post">
<div class="mb-3">
<label for="filepath" class="form-label">Имя рецепта (англиские буквы, _, .)</label>
<input type="text" class="form-control" id="filepath" name="filepath" value="<%= @old_filepath %>">
</div>
<div class="mb-3">
<label for="description" class="form-label">Описание рецепта сборки</label>
<textarea class="form-control" id="description" name="description" rows="3"><%= @old_description %></textarea>
</div>
<div class="mb-3">
<label for="codedata" class="form-label">Код</label>
<textarea class="form-control" id="codedata" name="codedata" rows="3"><%= @old_codedata %></textarea>
</div>
<div class="mb-3">
<label for="gitlist" class="form-label">git проекты, использующие рецепт</label>
<div class="gitlist">
<select class="form-control" id="gitlst" name="gitlst[]" multiple>
<% @repo_data.each do |item| %>
<% if !@old_gitlst.nil? && @old_gitlst.include?(item[:id].to_s) %>
<option value="<%= item[:id] %>" selected><%= item[:reponame] %></option>
<% else %>
<option value="<%= item[:id] %>"><%= item[:reponame] %></option>
<% end %>
<% end %>
</select>
</div>
</div>
<div class="mb-3 text-center">
<button type="submit" class="btn btn-primary">Сохранить</button>
<button type="submit" class="btn btn-danger" id="delete">Удалить</button>
<div id="winkeeper"></div>
</div>
</form>
</div>
<script>
$('.gitlist').dropdown({
searchNoData: '<li style="color:#ddd">Нет данных</li>',
});
</script>
<script src="/js/jquery-confirm.min.js"></script>
<script>
$("#delete").confirm({
title: 'Подтвердите удаление репозитория!',
content: '' +
'<div class="container">' +
'<form action="/rcpdelete/<%= ERB::Util.url_encode(@rcp_id) %>" class="formName" method="post">' +
'<div class="text-danger mx-auto p-2">' +
'<label>Введите название удаляемого рецепта</label>' +
'<input type="text" class="frmchk form-control" required name="rcpnamedup"/>' +
'<input type="hidden" class="frmchk form-control" id="rcphid" name="rcphid" value="<%= @rcp_name %>"/>' +
'</div>' +
'</form>' +
'</div>',
container: '#winkeeper',
theme: 'bootstrap',
buttons: {
formSubmit: {
text: 'Удалить',
titleClass: 'text-danger mx-auto p-2',
btnClass: 'btn btn-danger',
action: function () {
var correct_name = this.$content.find("#rcphid").val();
var name = this.$content.find('.frmchk').val();
if(!name){
$.alert({
title: 'Предупреждение!',
content: 'Название не может быть пустым',
container: '#winkeeper',
theme: 'bootstrap',
buttons: {
ok: {
text: 'Хорошо',
btnClass: 'btn btn-danger',
}
}
});
return false;
}
if (name != correct_name){
$.alert({
title: 'Предупреждение!',
content: 'Название задано неверно',
container: '#winkeeper',
theme: 'bootstrap',
buttons: {
ok: {
text: 'Хорошо',
btnClass: 'btn btn-danger',
}
}
});
return false;
}
this.$content.find('form').submit();
return true;
}
},
cancel: {
text: 'Отменить',
btnClass: 'btn btn-primary',
action: function () {
//close
}},
},
});
</script>
<%= erb :footer %>

@ -1,16 +1,19 @@
<%= erb :header %>
<div class="container py-4 py-xl-5">
<div class="row mb-5">
<div class="row pb-5">
<div class="col-md-8 col-xl-6 text-center mx-auto">
<h2>Доступные рецепты</h2>
<p class="w-lg-50">Страница управления рецептами</p>
<div>
<div class="px-3"><a href="/rcpcreate"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" viewBox="0 0 16 16" class="bi bi-plus-circle text-primary" style="font-size: 29px;">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"></path>
<path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"></path>
</svg>&nbsp;Добавить рецепт</a></div>
<div class="px-3"><a href="/rcpcreate">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" viewBox="0 0 16 16" class="bi bi-plus-circle text-primary" style="font-size: 29px;">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"></path>
<path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"></path>
</svg>&nbsp;Добавить рецепт</a>
</div>
</div>
</div>
</div>
<div class="container text-center">
<div class="row gy-3">
<% @rcp_data.each do |item| %>

Loading…
Cancel
Save