1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

iCalendar server api & db model

This commit is contained in:
Sylvain 2019-11-27 17:05:19 +01:00
parent 9e2134c9cf
commit baf8cfb487
18 changed files with 152 additions and 31 deletions

View File

@ -762,10 +762,10 @@ Application.Controllers.controller('DeleteRecurrentAvailabilityController', ['$s
* Controller used in the iCalendar (ICS) imports management page
*/
Application.Controllers.controller('AdminICalendarController', ['$scope',
function ($scope) {
Application.Controllers.controller('AdminICalendarController', ['$scope', 'iCalendars',
function ($scope, iCalendars) {
// list of ICS sources
$scope.calendars = [];
$scope.calendars = iCalendars;
// configuration of a new ICS source
$scope.newCalendar = {

View File

@ -41,7 +41,7 @@ Application.Controllers.controller('CalendarController', ['$scope', '$state', '$
// add availabilities source to event sources
$scope.eventSources = [];
$scope.eventSources.push({
url: '/api/ical/externals',
url: '/api/i_calendar/events',
textColor: 'black'
});

View File

@ -668,6 +668,7 @@ angular.module('application.router', ['ui.router'])
}
},
resolve: {
iCalendars: ['ICalendar', function (ICalendar) { return ICalendar.query().$promise; }],
translations: ['Translations', function (Translations) { return Translations.query('app.admin.icalendar').$promise; }]
}
})

View File

@ -1,5 +0,0 @@
'use strict';
Application.Services.factory('Ical', ['$resource', function ($resource) {
return $resource('/api/ical/externals');
}]);

View File

@ -0,0 +1,12 @@
'use strict';
Application.Services.factory('ICalendar', ['$resource', function ($resource) {
return $resource('/api/i_calendar/:id',
{ id: '@id' }, {
events: {
method: 'GET',
url: '/api/i_calendar/events'
}
}
);
}]);

View File

@ -0,0 +1,55 @@
# frozen_string_literal: true
# API Controller for resources of type iCalendar
class API::ICalendarController < API::ApiController
before_action :authenticate_user!, except: %i[index events]
before_action :set_i_cal, only: [:destroy]
respond_to :json
def index
@i_cals = ICalendar.all
end
def create
authorize ICalendar
@i_cal = ICalendar.new(i_calendar_params)
if @i_cal.save
render :show, status: :created, location: @i_cal
else
render json: @i_cal.errors, status: :unprocessable_entity
end
end
def destroy
authorize ICalendar
@i_cal.destroy
head :no_content
end
def events
require 'net/http'
require 'uri'
require 'icalendar'
@events = []
@i_cals = ICalendar.all.each do |i_cal|
ics = Net::HTTP.get(URI.parse(i_cal.url))
cals = Icalendar::Calendar.parse(ics)
cals.first.events.each do |evt|
@events.push(evt.merge!(color: i_cal.color))
end
end
end
private
def set_i_cal
@i_cal = ICalendar.find(params[:id])
end
def i_calendar_params
params.require(:i_calendar).permit(:url, :color, :text_color, :text_hidden)
end
end

View File

@ -1,19 +0,0 @@
# frozen_string_literal: true
# API Controller for resources of type iCalendar
class API::IcalController < API::ApiController
respond_to :json
def externals
require 'net/http'
require 'uri'
ics = Net::HTTP.get(URI.parse('https://calendar.google.com/calendar/ical/sylvain%40sleede.com/public/basic.ics'))
require 'icalendar'
require 'icalendar/tzinfo'
cals = Icalendar::Calendar.parse(ics)
@events = cals.first.events
end
end

5
app/models/i_calendar.rb Normal file
View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
# iCalendar (RFC 5545) files, stored by URL and kept with their display configuration
class ICalendar < ActiveRecord::Base
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
# Check the access policies for API::ICalendarController
class ICalendarPolicy < ApplicationPolicy
def create?
user.admin?
end
def destroy?
user.admin?
end
end

View File

@ -0,0 +1,3 @@
# frozen_string_literal: true
json.extract! i_cal, :id, :url, :color, :text_color, :text_hidden

View File

@ -1,8 +1,10 @@
# frozen_string_literal: true
json.array!(@events) do |event|
json.id event.uid
json.title event.summary
json.start event.dtstart.iso8601
json.end event.dtend.iso8601
json.backgroundColor 'white'
json.borderColor '#214712'
json.borderColor event.color
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
json.array!(@i_cals) do |i_cal|
json.partial! 'api/i_calendar/i_calendar', i_cal: i_cal
end

View File

@ -0,0 +1,3 @@
# frozen_string_literal: true
json.partial! 'api/i_calendar/i_calendar', i_cal: @i_cal

View File

@ -112,7 +112,9 @@ Rails.application.routes.draw do
get 'first', action: 'first', on: :collection
end
get 'ical/externals' => 'ical#externals'
resources :i_calendar, only: %i[index create destroy] do
get 'events', on: :collection
end
# for admin
resources :trainings do

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
# From this migration, we store URL to iCalendar files and a piece of configuration about them.
# This allows to display the events of these external calendars in fab-manager
class CreateICalendars < ActiveRecord::Migration
def change
create_table :i_calendars do |t|
t.string :url
t.string :color
t.string :text_color
t.boolean :text_hidden
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20191113103352) do
ActiveRecord::Schema.define(version: 20191127153729) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -251,6 +251,15 @@ ActiveRecord::Schema.define(version: 20191113103352) do
add_index "history_values", ["invoicing_profile_id"], name: "index_history_values_on_invoicing_profile_id", using: :btree
add_index "history_values", ["setting_id"], name: "index_history_values_on_setting_id", using: :btree
create_table "i_calendars", force: :cascade do |t|
t.string "url"
t.string "color"
t.string "text_color"
t.boolean "text_hidden"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "imports", force: :cascade do |t|
t.integer "user_id"
t.string "attachment"

13
test/fixtures/i_calendars.yml vendored Normal file
View File

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
url: MyString
color: MyString
text_color: MyString
text_hidden: false
two:
url: MyString
color: MyString
text_color: MyString
text_hidden: false

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ICalendarTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end