mirror of
https://github.com/discourse/discourse-data-explorer.git
synced 2025-02-16 16:34:47 +00:00
Finish group/category validation, add user lists
This commit is contained in:
parent
0672e99da2
commit
5b11bbec68
@ -1,3 +1,6 @@
|
|||||||
|
// import Category from 'discourse/models/category';
|
||||||
|
const Category = Discourse.Category;
|
||||||
|
|
||||||
const layoutMap = {
|
const layoutMap = {
|
||||||
int: 'int',
|
int: 'int',
|
||||||
bigint: 'int',
|
bigint: 'int',
|
||||||
@ -7,15 +10,15 @@ const layoutMap = {
|
|||||||
date: 'generic',
|
date: 'generic',
|
||||||
datetime: 'generic',
|
datetime: 'generic',
|
||||||
double: 'string',
|
double: 'string',
|
||||||
inet: 'generic',
|
|
||||||
user_id: 'user_id',
|
user_id: 'user_id',
|
||||||
post_id: 'string',
|
post_id: 'string',
|
||||||
topic_id: 'int',
|
topic_id: 'generic',
|
||||||
category_id: 'int',
|
category_id: 'generic',
|
||||||
group_id: 'int',
|
group_id: 'generic',
|
||||||
badge_id: 'int',
|
badge_id: 'generic',
|
||||||
int_list: 'generic',
|
int_list: 'generic',
|
||||||
string_list: 'generic'
|
string_list: 'generic',
|
||||||
|
user_list: 'user_list'
|
||||||
};
|
};
|
||||||
|
|
||||||
function allowsInputTypeTime() {
|
function allowsInputTypeTime() {
|
||||||
@ -56,6 +59,7 @@ export default Ember.Component.extend({
|
|||||||
|
|
||||||
const intVal = parseInt(value, 10);
|
const intVal = parseInt(value, 10);
|
||||||
const intValid = !isNaN(intVal) && intVal < 2147483648 && intVal > -2147483649;
|
const intValid = !isNaN(intVal) && intVal < 2147483648 && intVal > -2147483649;
|
||||||
|
const isPositiveInt = /^\d+$/.test(value);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'int':
|
case 'int':
|
||||||
return /^-?\d+$/.test(value) && intValid;
|
return /^-?\d+$/.test(value) && intValid;
|
||||||
@ -70,7 +74,39 @@ export default Ember.Component.extend({
|
|||||||
return /^(-?\d+|null)$/.test(i.trim());
|
return /^(-?\d+|null)$/.test(i.trim());
|
||||||
});
|
});
|
||||||
case 'post_id':
|
case 'post_id':
|
||||||
return /^\d+$/.test(value) || /\d+\/\d+(\?u=.*)?$/.test(value);
|
return isPositiveInt || /\d+\/\d+(\?u=.*)?$/.test(value);
|
||||||
|
case 'category_id':
|
||||||
|
const cats = Category.list();
|
||||||
|
if (!isPositiveInt && value !== value.dasherize()) {
|
||||||
|
this.set('value', value.dasherize());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPositiveInt) {
|
||||||
|
return !!cats.find(function(c) {
|
||||||
|
return c.get('id') === intVal;
|
||||||
|
});
|
||||||
|
} else if (/\//.test(value)) {
|
||||||
|
const match = /(.*)\/(.*)/.exec(value);
|
||||||
|
if (!match) return false;
|
||||||
|
const result = Category.findBySlug(match[2].dasherize(), match[1].dasherize());
|
||||||
|
return !!result;
|
||||||
|
} else {
|
||||||
|
return !!Category.findBySlug(value.dasherize());
|
||||||
|
}
|
||||||
|
case 'group_id':
|
||||||
|
const groups = this.site.get('groups');
|
||||||
|
if (isPositiveInt) {
|
||||||
|
return !!groups.find(function(g) {
|
||||||
|
return g.id === intVal;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (value !== value.underscore()) {
|
||||||
|
this.set('value', value.underscore());
|
||||||
|
}
|
||||||
|
return !!groups.find(function(g) {
|
||||||
|
return g.name === value.underscore();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}.property('value', 'info.type', 'info.nullable'),
|
}.property('value', 'info.type', 'info.nullable'),
|
||||||
@ -83,7 +119,7 @@ export default Ember.Component.extend({
|
|||||||
if (layoutMap[type]) {
|
if (layoutMap[type]) {
|
||||||
return layoutMap[type];
|
return layoutMap[type];
|
||||||
}
|
}
|
||||||
return type;
|
return 'generic';
|
||||||
}.property('info.type'),
|
}.property('info.type'),
|
||||||
|
|
||||||
layoutName: function() {
|
layoutName: function() {
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
{{text-field value=value}}
|
{{text-field value=value type="text"}}
|
||||||
<span class="param-name">{{info.identifier}}</span>
|
<span class="param-name">{{info.identifier}}</span>
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
{{user-selector usernames=value}}
|
||||||
|
<span class="param-name">{{info.identifier}}</span>
|
@ -166,11 +166,15 @@
|
|||||||
background-color: mix($danger, $secondary, 20%);
|
background-color: mix($danger, $secondary, 20%);
|
||||||
}
|
}
|
||||||
.param {
|
.param {
|
||||||
|
width: 300px;
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
overflow-x: visible;
|
overflow-x: visible;
|
||||||
.ac-wrap {
|
.ac-wrap {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
input {
|
||||||
|
width: 190px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.param-name {
|
.param-name {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
|
33
plugin.rb
33
plugin.rb
@ -389,11 +389,11 @@ SQL
|
|||||||
def self.types
|
def self.types
|
||||||
@types ||= Enum.new(
|
@types ||= Enum.new(
|
||||||
# Normal types
|
# Normal types
|
||||||
:int, :bigint, :boolean, :string, :date, :time, :datetime, :double, :inet,
|
:int, :bigint, :boolean, :string, :date, :time, :datetime, :double,
|
||||||
# Selection help
|
# Selection help
|
||||||
:user_id, :post_id, :topic_id, :category_id, :group_id, :badge_id,
|
:user_id, :post_id, :topic_id, :category_id, :group_id, :badge_id,
|
||||||
# Arrays
|
# Arrays
|
||||||
:int_list, :string_list
|
:int_list, :string_list, :user_list
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -402,7 +402,6 @@ SQL
|
|||||||
integer: :int,
|
integer: :int,
|
||||||
text: :string,
|
text: :string,
|
||||||
timestamp: :datetime,
|
timestamp: :datetime,
|
||||||
ipaddr: :inet,
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -458,15 +457,22 @@ SQL
|
|||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
invalid_format string, e.message
|
invalid_format string, e.message
|
||||||
end
|
end
|
||||||
when :ipaddr
|
|
||||||
begin
|
|
||||||
value = IPAddr.new string
|
|
||||||
rescue ArgumentError => e
|
|
||||||
invalid_format string, e.message
|
|
||||||
end
|
|
||||||
when :double
|
when :double
|
||||||
value = string.to_f
|
value = string.to_f
|
||||||
when :user_id, :post_id, :topic_id, :category_id, :group_id, :badge_id
|
when :category_id
|
||||||
|
if string =~ /(.*)\/(.*)/
|
||||||
|
parent_name = $1
|
||||||
|
child_name = $2
|
||||||
|
parent = Category.query_parent_category(parent_name)
|
||||||
|
invalid_format string, "Could not find category named #{parent_name}" unless parent
|
||||||
|
object = Category.query_category(child_name, parent)
|
||||||
|
invalid_format string, "Could not find subcategory of #{parent_name} named #{child_name}" unless object
|
||||||
|
else
|
||||||
|
object = Category.where(id: string.to_i).first || Category.where(slug: string).first || Category.where(name: string).first
|
||||||
|
invalid_format string, "Could not find category named #{string}" unless object
|
||||||
|
end
|
||||||
|
value = object.id
|
||||||
|
when :user_id, :post_id, :topic_id, :group_id, :badge_id
|
||||||
if string.gsub(/[ _]/, '') =~ /^-?\d+$/
|
if string.gsub(/[ _]/, '') =~ /^-?\d+$/
|
||||||
clazz_name = (/^(.*)_id$/.match(type.to_s)[1].classify.to_sym)
|
clazz_name = (/^(.*)_id$/.match(type.to_s)[1].classify.to_sym)
|
||||||
begin
|
begin
|
||||||
@ -497,6 +503,10 @@ SQL
|
|||||||
invalid_format string, "The topic with id #{$1} was not found"
|
invalid_format string, "The topic with id #{$1} was not found"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elsif type == :group_id
|
||||||
|
object = Group.where(name: string).first
|
||||||
|
invalid_format string, "The group named #{string} was not found" unless object
|
||||||
|
value = object.id
|
||||||
else
|
else
|
||||||
invalid_format string
|
invalid_format string
|
||||||
end
|
end
|
||||||
@ -506,6 +516,9 @@ SQL
|
|||||||
when :string_list
|
when :string_list
|
||||||
value = string.split(',').map {|s| s.downcase == '#null' ? nil : s }
|
value = string.split(',').map {|s| s.downcase == '#null' ? nil : s }
|
||||||
invalid_format string, "can't be empty" if value.length == 0
|
invalid_format string, "can't be empty" if value.length == 0
|
||||||
|
when :user_list
|
||||||
|
value = string.split(',').map {|s| User.find_by_username_or_email(s) }
|
||||||
|
invalid_format string, "can't be empty" if value.length == 0
|
||||||
else
|
else
|
||||||
raise TypeError.new('unknown parameter type??? should not get here')
|
raise TypeError.new('unknown parameter type??? should not get here')
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user