Initial commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
module Carto
|
||||
module Admin
|
||||
class MapPublicMapAdapter
|
||||
extend Forwardable
|
||||
|
||||
delegate [ :provider, :zoom ] => :map
|
||||
|
||||
attr_reader :map
|
||||
|
||||
def initialize(map)
|
||||
@map = map
|
||||
end
|
||||
|
||||
def public_values
|
||||
{
|
||||
id: @map.id,
|
||||
user_id: @map.user_id,
|
||||
provider: @map.provider,
|
||||
bounding_box_sw: @map.bounding_box_sw,
|
||||
bounding_box_ne: @map.bounding_box_ne,
|
||||
center: @map.center,
|
||||
zoom: @map.zoom,
|
||||
view_bounds_sw: @map.view_bounds_sw,
|
||||
view_bounds_ne: @map.view_bounds_ne,
|
||||
legends: @map.legends,
|
||||
scrollwheel: @map.scrollwheel
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,152 @@
|
||||
require_dependency 'helpers/avatar_helper'
|
||||
require_dependency 'cartodb/central'
|
||||
require_dependency 'helpers/organization_notifications_helper'
|
||||
|
||||
class Carto::Admin::MobileAppsController < Admin::AdminController
|
||||
include Carto::ControllerHelper
|
||||
include MobileAppsHelper
|
||||
include AvatarHelper
|
||||
include OrganizationNotificationsHelper
|
||||
|
||||
ssl_required :index, :show, :new, :create, :update, :destroy
|
||||
before_filter :invalidate_browser_cache
|
||||
before_filter :login_required
|
||||
before_filter :check_user_permissions
|
||||
before_filter :initialize_cartodb_central_client
|
||||
before_filter :load_organization_notifications
|
||||
before_filter :validate_id, only: [:show, :update, :destroy]
|
||||
before_filter :load_mobile_app, only: [:show, :update]
|
||||
before_filter :setup_avatar_upload, only: [:new, :create, :show, :update]
|
||||
|
||||
rescue_from Carto::LoadError, with: :render_404
|
||||
|
||||
layout 'application'
|
||||
|
||||
def index
|
||||
response = @cartodb_central_client.get_mobile_apps(current_user.username).deep_symbolize_keys
|
||||
|
||||
@mobile_apps = response[:mobile_apps].map { |a| Carto::MobileApp.new(a) }
|
||||
@open_monthly_users = response[:monthly_users][:open]
|
||||
@private_monthly_users = response[:monthly_users][:private]
|
||||
rescue CartoDB::CentralCommunicationFailure => e
|
||||
@mobile_apps = []
|
||||
CartoDB::Logger.error(message: 'Error loading mobile apps from Central', exception: e)
|
||||
flash.now[:error] = 'Unable to connect to license server. Try again in a moment.'
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def new
|
||||
@mobile_app = Carto::MobileApp.new
|
||||
end
|
||||
|
||||
def create
|
||||
@mobile_app = Carto::MobileApp.new(params[:mobile_app])
|
||||
@mobile_app.icon_url = get_default_avatar unless @mobile_app.icon_url.present?
|
||||
|
||||
unless @mobile_app.valid?
|
||||
flash.now[:error] = @mobile_app.errors.full_messages.join(', ')
|
||||
render :new
|
||||
return
|
||||
end
|
||||
|
||||
attributes = @mobile_app.as_json.symbolize_keys.slice(:name, :description, :icon_url, :platform, :app_id, :app_type)
|
||||
@cartodb_central_client.create_mobile_app(current_user.username, attributes)
|
||||
|
||||
redirect_to CartoDB.url(self, 'mobile_apps'), flash: { success: 'Your app has been added succesfully!' }
|
||||
|
||||
rescue CartoDB::CentralCommunicationFailure => e
|
||||
if e.response_code == 422
|
||||
if e.errors.any? { |e| e['app_id'] }
|
||||
@mobile_app.errors["app_id"] = 'has already been taken'
|
||||
flash.now[:error] = "That application ID has already been taken. Please make sure that it is unique."
|
||||
else
|
||||
flash.now[:error] = e.errors.join('. ')
|
||||
end
|
||||
else
|
||||
CartoDB::Logger.error(message: 'Error creating mobile_app in Central', exception: e)
|
||||
flash.now[:error] = 'Unable to connect to license server. Try again in a moment.'
|
||||
end
|
||||
render :new
|
||||
end
|
||||
|
||||
def update
|
||||
updated_attributes = params[:mobile_app].symbolize_keys.slice(:name, :description, :icon_url, :app_type)
|
||||
@mobile_app.name = updated_attributes[:name]
|
||||
@mobile_app.icon_url = updated_attributes[:icon_url]
|
||||
@mobile_app.description = updated_attributes[:description]
|
||||
|
||||
unless @mobile_app.valid?
|
||||
flash.now[:error] = @mobile_app.errors.full_messages.join(', ')
|
||||
render :show
|
||||
return
|
||||
end
|
||||
|
||||
@cartodb_central_client.update_mobile_app(current_user.username, @app_id, updated_attributes)
|
||||
|
||||
redirect_to(CartoDB.url(self, 'mobile_app', params: { id: @app_id }),
|
||||
flash: { success: 'Your app has been updated succesfully!' })
|
||||
|
||||
rescue CartoDB::CentralCommunicationFailure => e
|
||||
if e.response_code == 422
|
||||
flash.now[:error] = e.errors
|
||||
else
|
||||
CartoDB::Logger.error(message: 'Error updating mobile_app in Central', exception: e)
|
||||
flash.now[:error] = 'Unable to connect to license server. Try again in a moment.'
|
||||
end
|
||||
render :show
|
||||
end
|
||||
|
||||
def destroy
|
||||
valid_password_confirmation
|
||||
@cartodb_central_client.delete_mobile_app(current_user.username, @app_id)
|
||||
redirect_to CartoDB.url(self, 'mobile_apps'), flash: { success: 'Your app has been deleted succesfully!' }
|
||||
rescue Carto::PasswordConfirmationError => e
|
||||
flash[:error] = e.message
|
||||
redirect_to(CartoDB.url(self, 'mobile_app', params: { id: @app_id }))
|
||||
rescue CartoDB::CentralCommunicationFailure => e
|
||||
raise Carto::LoadError.new('Mobile app not found') if e.response_code == 404
|
||||
CartoDB::Logger.error(message: 'Error deleting mobile app from Central', exception: e, app_id: @app_id)
|
||||
redirect_to(CartoDB.url(self, 'mobile_app', params: { id: @app_id }),
|
||||
flash: { error: 'Unable to connect to license server. Try again in a moment.' })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_user_permissions
|
||||
raise Carto::LoadError.new('Mobile apps disabled') unless current_user.mobile_sdk_enabled?
|
||||
end
|
||||
|
||||
def initialize_cartodb_central_client
|
||||
raise Carto::LoadError.new('Mobile apps disabled') unless Cartodb::Central.sync_data_with_cartodb_central?
|
||||
@cartodb_central_client ||= Cartodb::Central.new
|
||||
end
|
||||
|
||||
def validate_id
|
||||
@app_id = uuid_parameter(:id)
|
||||
end
|
||||
|
||||
def setup_avatar_upload
|
||||
@icon_valid_extensions = AVATAR_VALID_EXTENSIONS
|
||||
end
|
||||
|
||||
def load_mobile_app
|
||||
@mobile_app = Carto::MobileApp.new(@cartodb_central_client.get_mobile_app(current_user.username, @app_id))
|
||||
|
||||
rescue CartoDB::CentralCommunicationFailure => e
|
||||
raise Carto::LoadError.new('Mobile app not found') if e.response_code == 404
|
||||
CartoDB::Logger.error(message: 'Error loading mobile app from Central', exception: e, app_id: @app_id)
|
||||
|
||||
redirect_to(CartoDB.url(self, 'mobile_apps'),
|
||||
flash: { error: 'Unable to connect to license server. Try again in a moment.' })
|
||||
end
|
||||
|
||||
def get_default_avatar
|
||||
if Cartodb.asset_path
|
||||
"#{Cartodb.asset_path}/assets/unversioned/images/avatars/mobile_app_default_avatar.png"
|
||||
else
|
||||
"#{relative_url_root}/#{frontend_version}/images/avatars/mobile_app_default_avatar.png"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
module Carto
|
||||
module Admin
|
||||
class UserPublicMapAdapter
|
||||
extend Forwardable
|
||||
|
||||
delegate [:id, :name, :username, :disqus_shortname, :avatar, :avatar_url, :remove_logo?, :has_organization?,
|
||||
:organization, :organization_id, :twitter_username, :location, :public_url, :subdomain,
|
||||
:sql_safe_database_schema, :account_type, :google_maps_query_string, :basemaps, :default_basemap,
|
||||
:name_or_username] => :user
|
||||
|
||||
attr_reader :user
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def public_table_count
|
||||
@public_table_count ||= Carto::VisualizationQueryBuilder.user_public_tables(@user).build.count
|
||||
end
|
||||
|
||||
def public_visualization_count
|
||||
@public_visualization_count ||= Carto::VisualizationQueryBuilder.user_public_visualizations(@user).build.count
|
||||
end
|
||||
|
||||
def all_visualization_count
|
||||
@all_visualization_count ||= Carto::VisualizationQueryBuilder.user_all_visualizations(@user).build.count
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
module Carto
|
||||
module Admin
|
||||
class UserTablePublicMapAdapter
|
||||
extend Forwardable
|
||||
|
||||
delegate [:dependent_visualizations, :partially_dependent_visualizations,
|
||||
:fully_dependent_visualizations, :name, :id] => :user_table
|
||||
|
||||
attr_reader :user_table
|
||||
|
||||
def initialize(user_table)
|
||||
@user_table = user_table
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,100 @@
|
||||
require_relative '../../../helpers/carto/html_safe'
|
||||
require_dependency 'carto/api/vizjson_presenter'
|
||||
|
||||
module Carto
|
||||
module Admin
|
||||
class VisualizationPublicMapAdapter
|
||||
extend Forwardable
|
||||
include Carto::HtmlSafe
|
||||
GEOMETRY_MAPPING = {
|
||||
'st_multipolygon' => 'polygon',
|
||||
'st_polygon' => 'polygon',
|
||||
'st_multilinestring' => 'line',
|
||||
'st_linestring' => 'line',
|
||||
'st_multipoint' => 'point',
|
||||
'st_point' => 'point'
|
||||
}
|
||||
|
||||
delegate [
|
||||
:type_slide?, :derived?, :organization, :organization?, :id,
|
||||
:password_protected?, :varnish_key, :related_tables, :password_valid?, :get_auth_tokens, :table, :name,
|
||||
:overlays, :created_at, :updated_at, :description, :mapviews, :geometry_types, :privacy, :tags,
|
||||
:surrogate_key, :has_password?, :total_mapviews, :is_viewable_by_user?, :is_accesible_by_user?,
|
||||
:can_be_cached?, :is_privacy_private?, :source, :kind_raster?, :has_read_permission?, :has_write_permission?,
|
||||
:open_in_editor?, :version, :kind, :public?, :public_with_link?, :user_table, :liked_by?
|
||||
] => :visualization
|
||||
|
||||
attr_reader :visualization
|
||||
|
||||
def initialize(visualization, current_viewer, context)
|
||||
@visualization = visualization
|
||||
@current_viewer = current_viewer
|
||||
@context = context
|
||||
end
|
||||
|
||||
def to_vizjson(options = {})
|
||||
Carto::Api::VizJSONPresenter.new(@visualization, $tables_metadata).to_vizjson(options)
|
||||
end
|
||||
|
||||
def is_owner?(user)
|
||||
@visualization.owner?(user)
|
||||
end
|
||||
|
||||
def to_hash(options={})
|
||||
# TODO: using an Api presenter here smells, refactor
|
||||
presenter = Carto::Api::VisualizationPresenter.new(@visualization, @current_viewer, @context, options.merge(show_stats: false))
|
||||
options.delete(:public_fields_only) === true ? presenter.to_public_poro : presenter.to_poro
|
||||
end
|
||||
|
||||
def map
|
||||
Carto::Admin::MapPublicMapAdapter.new(@visualization.map)
|
||||
end
|
||||
|
||||
def user
|
||||
Carto::Admin::UserPublicMapAdapter.new(@visualization.user)
|
||||
end
|
||||
|
||||
# TODO: remove is_ prefixed methods from visualization
|
||||
def private?
|
||||
@visualization.private?
|
||||
end
|
||||
|
||||
def related_canonical_visualizations
|
||||
@visualization.related_canonical_visualizations.map { |rv|
|
||||
Carto::Admin::VisualizationPublicMapAdapter.new(rv, @current_viewer, @context) if rv.public?
|
||||
}.compact
|
||||
end
|
||||
|
||||
def related_visualizations_geometry_types
|
||||
related_canonical_visualizations.collect(&:geometry_types).flatten.uniq
|
||||
end
|
||||
|
||||
def related_tables_geometry_types
|
||||
related_tables.collect(&:geometry_types).flatten.uniq
|
||||
end
|
||||
|
||||
def related_visualizations_simple_geometry_types
|
||||
simplify_geometry_types(related_visualizations_geometry_types)
|
||||
end
|
||||
|
||||
def related_tables_simple_geometry_types
|
||||
simplify_geometry_types(related_tables_geometry_types)
|
||||
end
|
||||
|
||||
def map_zoom
|
||||
map.nil? ? nil : map.zoom
|
||||
end
|
||||
|
||||
def display_name_or_name
|
||||
@visualization.display_name.nil? ? @visualization.name : @visualization.display_name
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def simplify_geometry_types(geometry_types)
|
||||
geometry_types.map { |gt| GEOMETRY_MAPPING.fetch(gt.downcase, "unknown: #{gt}") }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user