Skip to content

Commit

Permalink
Add content and style policy
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMorugin committed Nov 27, 2015
1 parent 7b6b736 commit 59e239d
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 21 deletions.
9 changes: 9 additions & 0 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class ContentsController < ApplicationController
before_action :set_content, only: [:show, :edit, :update, :destroy]
after_action :verify_authorized
before_action :set_authorize

def pundit_user
current_client
end
# GET /contents
# GET /contents.json
def index
Expand Down Expand Up @@ -54,6 +59,7 @@ def update
# DELETE /contents/1
# DELETE /contents/1.json
def destroy
authorize
@content.destroy
respond_to do |format|
format.html { redirect_to contents_url, notice: 'content was successfully destroyed.' }
Expand All @@ -62,6 +68,9 @@ def destroy
end

private
def set_authorize
authorize Content
end
# Use callbacks to share common setup or constraints between actions.
def set_content
@content = Content.find(params[:id])
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/styles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class StylesController < ApplicationController
before_action :set_style, only: [:show, :edit, :update, :destroy]
after_action :verify_authorized
before_action :set_authorize

def pundit_user
current_client
end
# GET /styles
# GET /styles.json
def index
Expand Down Expand Up @@ -62,6 +67,9 @@ def destroy
end

private
def set_authorize
authorize Style
end
# Use callbacks to share common setup or constraints between actions.
def set_style
@style = Style.find(params[:id])
Expand Down
32 changes: 24 additions & 8 deletions app/jobs/bot_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,39 @@ class BotJob

def initialize
@worker_name = :bot1
@admin_email = "cmorugin@gmail.com"
@admin_email = "xxx@gmail.com"
@sleep_time = 10
@end_status = 11
@debug = true
end

def set_config(bot_name)
return if bot_name.nil?
@worker_name = bot_name
file = Rails.root.join('config/config.secret')
par = load_settings(file)
par = par[bot_name.to_s]
return if par.blank?
@admin_email = par[:admin_email]
@sleep_time = par[:sleep_time]
@end_status = par[:end_status]
@debug = par[:debug]

log "config: #{par.to_s}"
end

def execute

def execute
log('-----------------------Start-------------------')
adm = Client.find_by(email: @admin_email)
log("Admin_id = #{adm.id}")
loop do
sleep @sleep_time
@admin = Client.find_by(email: @admin_email)
log('------Loop start-------')
if !check_idle
log("Queue busy")
next
end
set_config(@worker_name)
ci = get_random_content
if ci.nil?
log("No content")
Expand All @@ -42,12 +57,12 @@ def execute
qi.content_id = ci.id
qi.style_id = si.id

qi.client_id = adm.id
qi.client_id = @admin.id
qi.save

start_workers
log("Queue: #{qi.attributes}")
log('----------Loop stop----------')
log('----------Loop end----------')
#
#break
end
Expand All @@ -56,7 +71,8 @@ def execute
private

def check_idle
q = QueueImage.where("status = #{STATUS_NOT_PROCESSED} or status = #{STATUS_IN_PROCESS}")
#q = QueueImage.where("status = #{STATUS_NOT_PROCESSED} or status = #{STATUS_IN_PROCESS}")
q = QueueImage.where("client_id = #{@admin.id} and status = #{STATUS_NOT_PROCESSED}")
q.count == 0
end

Expand All @@ -77,7 +93,7 @@ def get_random_content
end

def log(msg)
write_log(msg, @worker_name.to_s)
write_log(msg, @worker_name.to_s) if @debug
end


Expand Down
14 changes: 7 additions & 7 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ def initialize(user, record)
end

def index?
false
!user.nil? && user.admin?
end

def show?
false
index?
end

def create?
false
index?
end

def new?
create?
index?
end

def update?
false
index?
end

def edit?
update?
index?
end

def destroy?
false
index?
end

def scope
Expand Down
7 changes: 7 additions & 0 deletions app/policies/content_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ContentPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope
end
end
end
7 changes: 7 additions & 0 deletions app/policies/style_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class StylePolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope
end
end
end
2 changes: 1 addition & 1 deletion app/views/contents/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

<%= link_to 'Edit', edit_content_path(@content) %> |
<%= link_to 'Back', contents_path %>|
<%= link_to 'New', new_contents_path %>
<%= link_to 'New', new_content_path %>
3 changes: 3 additions & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<%= image_tag('/logo.png', class: "imagesStyle") %>
</div>
<ul class="nav nav-pills pull-right">
<% if policy(AdminPage).main? %>
<li><%= link_to "Admin", admin_pages_main_path %></li>
<% end %>
<% if client_signed_in? %>
<li><%= link_to "Добавить изображения", new_queue_image_path, class: "btn btn-primary" %></li>
<li><%= link_to "Моя коллекция", queue_images_path %></li>
Expand Down
5 changes: 3 additions & 2 deletions app/views/static_pages/about.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
<p>получаем шедевр, который можно повесить напротив плиты.</p>
<%=image_tag("/starry_megan.png", class: "imagesStyle")%>
<p></p>
<p> Процесс создания картины компьютером конечно не настолько трудоемок, как попытаться нарисовать ее самому, но занимает продолжительное время.
<p> Процесс создания картины компьютером конечно не настолько трудоемок, как попытаться нарисовать ее самому, но занимает продолжительное время. Скажем так, обычному домашнему компьютеру с i5 требутся на это полтора-два часа.
Поэтому не стоит ждать результат мгновенно, а стоит полистать <a href="http://ostagram.ru/lenta">ленту</a> уже готовых работ компьютерного Ван Гога. <p>

<p> А теперь заумный текст, который я сам не понимаю. Для создания картин используется сверточные нейронные сети и алгоритм художественно стиля by Leon A. Gatys, Alexander S. Ecker, and Matthias Bethge.
Более подробно с этим можно ознакомиться по ссылке на <a href="https://github.com/jcjohnson/neural-style">GitHub</a>.</p>
Более подробно с этим портале можно ознакомиться по ссылке на <a href="https://github.com/SergeyMorugin/ostagram">GitHub</a>.</p>

<p> Для добавления фото необходимо зарегистрироваться. Процесс прост как пять копеек, а в подарок вы будете получать свои обработанные фото на почту, как только они «нарисуются».</p>
<p> Если есть вопросы и предложения пишите на почту info@ostagram.ru</p>

<% if !client_signed_in? %>
<div class="actions">
Expand Down
2 changes: 1 addition & 1 deletion app/views/styles/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

<%= link_to 'Show', @style %> |
<%= link_to 'Back', styles_path %>|
<%= link_to 'New', new_styles_path %>
<%= link_to 'New', new_style_path %>
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Rails.application.routes.draw do


#resources :styles
#resources :contents
resources :styles
resources :contents
get 'admin_pages/main'
get 'admin_pages/images'
get 'admin_pages/users'
Expand Down
19 changes: 19 additions & 0 deletions test/policies/content_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class ContentPolicyTest < ActiveSupport::TestCase

def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end
19 changes: 19 additions & 0 deletions test/policies/style_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class StylePolicyTest < ActiveSupport::TestCase

def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end

0 comments on commit 59e239d

Please sign in to comment.