Skip to content

Commit

Permalink
Simplify market creation API. See #299
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Sep 14, 2017
1 parent f4a5ed4 commit 1d280b8
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/
var WirecloudCatalogue = function WirecloudCatalogue(options) {

Object.defineProperty(this, 'title', {'value': options.title || options.name});
Object.defineProperty(this, 'name', {'value': options.name});
Object.defineProperty(this, 'permissions', {'value': options.permissions});

Expand Down
2 changes: 1 addition & 1 deletion src/wirecloud/commons/utils/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ def add(self, name, url, type_, expect_error=False, public=False):
self.open_menu().click_entry("Add new marketplace")

form = FormModalTester(self.testcase, self.testcase.wait_element_visible('.wc-add-external-catalogue-modal'))
form.get_field('name').set_value(name)
form.get_field('title').set_value(name)
form.get_field('url').set_value(url)
form.get_field('type').set_value(type_)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ <h4>{% trans "Your browser seems to lack some required features" %}</h4>

{% compress js %}
<script type="text/javascript" src="{% static "js/lib/moment-with-locales.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/lib/urlify.js" %}"></script>
{% endcompress %}

<script type="text/javascript" src="{% url "wirecloud.javascript_translation_catalogue" %}?language={{ LANGUAGE_CODE }}&amp;v={{ WIRECLOUD_VERSION_HASH }}"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
utils.inherit(BusinessAPIEcosystemView, Wirecloud.ui.WorkspaceView);

BusinessAPIEcosystemView.prototype.getLabel = function getLabel() {
return this.desc.name;
return this.desc.title || this.desc.name;
};

BusinessAPIEcosystemView.prototype.goUp = function goUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@
this.appendChild(this.alternatives);
this.currentStore = 'All stores';
Object.defineProperty(this, 'desc', {value: options.marketplace_desc});
if (this.desc.user != null) {
Object.defineProperty(this, 'market_id', {value: this.desc.user + '/' + this.desc.name});
} else {
Object.defineProperty(this, 'market_id', {value: this.desc.name});
}
Object.defineProperty(this, 'market_id', {value: this.desc.user + '/' + this.desc.name});
this.loading = null;
this.error = false;
this.callbacks = [];
Expand Down Expand Up @@ -135,7 +131,7 @@
FiWareCatalogueView.prototype = new StyledElements.Alternative();

FiWareCatalogueView.prototype.getLabel = function () {
return this.desc.name;
return this.desc.title || this.desc.name;
};

FiWareCatalogueView.prototype.onHistoryChange = function onHistoryChange(state) {
Expand Down
4 changes: 3 additions & 1 deletion src/wirecloud/platform/fixtures/selenium_test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@
"model": "platform.market",
"fields": {
"name": "origin",
"options": "{\"url\": \"http://wcatalogue.example.com\", \"type\":\"wirecloud\", \"label\":\"origin\"}"
"public": true,
"options": "{\"public\": true, \"url\": \"http://wcatalogue.example.com\", \"type\":\"wirecloud\", \"label\":\"origin\"}",
"user": 1
}
}
]
10 changes: 4 additions & 6 deletions src/wirecloud/platform/markets/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2012-2015 CoNWeT Lab., Universidad Politécnica de Madrid
# Copyright (c) 2012-2017 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of Wirecloud.

Expand All @@ -27,8 +27,9 @@

@python_2_unicode_compatible
class Market(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('User'), blank=True, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('User'), blank=True)
name = models.CharField(_('Name'), max_length=50)
public = models.BooleanField(_('Public'), default=False)
options = JSONField(_('Options'))

class Meta:
Expand All @@ -37,10 +38,7 @@ class Meta:
db_table = 'wirecloud_market'

def __str__(self):
if self.user is None:
return self.name
else:
return self.user.username + '/' + self.name
return self.user.username + '/' + self.name


@python_2_unicode_compatible
Expand Down
31 changes: 12 additions & 19 deletions src/wirecloud/platform/markets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ class MarketCollection(Resource):
def read(self, request):
result = []

for market in Market.objects.filter(Q(user=None) | Q(user=request.user)).order_by('user'):
for market in Market.objects.filter(Q(public=True) | Q(user=request.user)).order_by('user'):
market_data = market.options
market_data['name'] = market.name

if market.user is not None:
market_data['user'] = market.user.username
else:
market_data['user'] = None
market_data['user'] = market.user.username
market_data['public'] = market.public

market_data['permissions'] = {
'delete': request.user.is_superuser or market.user == request.user
Expand All @@ -68,32 +65,28 @@ def create(self, request):

received_data = parse_json_request(request)

if 'options' not in received_data:
return build_error_response(request, 400, _("Missing marketplace options"))

validate_url_param(request, 'options.url', received_data['options']['url'])
validate_url_param(request, 'url', received_data['url'])

if 'user' not in received_data['options'] or received_data['options']['user'] == request.user.username:
user_entry = request.user
if 'user' not in received_data or received_data['user'] == request.user.username:
target_user = request.user
else:
try:
user_entry = User.objects.get(username=received_data['options']['user'])
target_user = User.objects.get(username=received_data['user'])
except:
return build_error_response(request, 422, _("invalid user option"))

if user_entry != request.user and not request.user.is_superuser:
if target_user != request.user and not request.user.is_superuser:
return build_error_response(request, 403, _("You don't have permissions for adding marketplaces in name of other user"))

if 'user' in received_data['options']:
del received_data['options']['user']
received_data['user'] = target_user.username

try:
Market.objects.create(user=user_entry, name=received_data['name'], options=received_data['options'])
Market.objects.create(user=target_user, name=received_data['name'], public=received_data['public'], options=received_data)
except IntegrityError:
return build_error_response(request, 409, 'Market name already in use')

market_managers = get_market_managers(user_entry)
market_managers[user_entry.username + '/' + received_data['name']].create(user_entry, received_data['options'])
market_managers = get_market_managers(target_user)
market_managers[target_user.username + '/' + received_data['name']].create(target_user, received_data)

return HttpResponse(status=201)

Expand Down
20 changes: 20 additions & 0 deletions src/wirecloud/platform/migrations/0008_market_public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-09-13 07:33
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('platform', '0007_remove_value_size_constraints_from_preference_models'),
]

operations = [
migrations.AddField(
model_name='market',
name='public',
field=models.BooleanField(default=False, verbose_name='Public'),
),
]
33 changes: 33 additions & 0 deletions src/wirecloud/platform/migrations/0009_migrate_public_markets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-09-13 07:33
from __future__ import unicode_literals

from django.db import migrations


def migrate_public_markets(apps, schema_editor):
Market = apps.get_model('platform', 'Market')
User = apps.get_model('auth', 'User')
wirecloud_user = None

for market in Market.objects.all():
market.public = market.user is None

if market.public:
if wirecloud_user is None:
wirecloud_user = User.objects.get(username="wirecloud")

market.user = wirecloud_user

market.save()


class Migration(migrations.Migration):

dependencies = [
('platform', '0008_market_public'),
]

operations = [
migrations.RunPython(migrate_public_markets),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-09-13 07:36
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('platform', '0009_migrate_public_markets'),
]

operations = [
migrations.AlterField(
model_name='market',
name='user',
field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='User'),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
CatalogueView.prototype = new StyledElements.Alternative();

CatalogueView.prototype.getLabel = function getLabel() {
return this.catalogue.name;
return this.catalogue.title;
};

CatalogueView.prototype._onShow = function _onShow() {
Expand Down
16 changes: 2 additions & 14 deletions src/wirecloud/platform/static/js/wirecloud/ui/MarketplaceView.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@

for (i = 0; i < response.length; i++) {
view_element = response[i];
if (view_element.user != null) {
market_key = view_element.user + '/' + view_element.name;
} else {
market_key = view_element.name;
}
market_key = view_element.user + '/' + view_element.name;

if (market_key in old_views) {
this.viewsByName[market_key] = old_views[market_key];
Expand Down Expand Up @@ -231,10 +227,6 @@
return [utils.gettext('marketplace list not available')];
} else {
breadcrum = ['marketplace'];
if (current_alternative.desc.user) {
breadcrum.push(current_alternative.desc.user);
}

breadcrum.push(current_alternative.getLabel());
// TODO
if (current_alternative.alternatives != null) {
Expand All @@ -258,11 +250,7 @@
if (current_alternative === this.emptyAlternative || current_alternative === this.errorsAlternative) {
return utils.gettext('Marketplace');
} else {
if (current_alternative.desc.user) {
marketname = current_alternative.desc.user + '/' + current_alternative.getLabel();
} else {
marketname = current_alternative.getLabel();
}
marketname = current_alternative.getLabel();
title = utils.interpolate(utils.gettext('Marketplace - %(marketname)s'), {marketname: marketname});
subalternative = current_alternative.alternatives.getCurrentAlternative();
if (subalternative.view_name === "details" && subalternative.currentEntry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
var menu, fields;

fields = {
'name': {
'title': {
'type': 'text',
'label': utils.gettext('Name'),
'required': true
Expand Down Expand Up @@ -97,23 +97,12 @@

// Form data is sent to server
menu.executeOperation = (data) => {
var market_info = {
"name": data.name,
"options": {
"name": data.name,
"url": data.url,
"type": data.type,
}
};
if (data.public === true) {
market_info.options.user = null;
} else {
market_info.options.user = Wirecloud.contextManager.get('username');
}
var task = Wirecloud.MarketManager.addMarket(market_info);
data.name = URLify(data.title);
data.user = Wirecloud.contextManager.get("username");
var task = Wirecloud.MarketManager.addMarket(data);
// TODO move to use events
task.then(() => {
this.market.addMarket(market_info.options);
this.market.addMarket(data);
});
return task;
};
Expand Down
Loading

0 comments on commit 1d280b8

Please sign in to comment.