Commit 406ea607 authored by Janez K's avatar Janez K

added option to deprecate widgets by moving them to deprecated_widgets directory

parent 8dea4323
...@@ -7,6 +7,7 @@ import uuid ...@@ -7,6 +7,7 @@ import uuid
import os import os
import sys import sys
from django.conf import settings from django.conf import settings
from django.core.management.color import color_style
import json import json
def add_category(category,categories): def add_category(category,categories):
...@@ -71,6 +72,9 @@ def serialize_category(c): ...@@ -71,6 +72,9 @@ def serialize_category(c):
return data return data
def export_package(package_name,writer): def export_package(package_name,writer):
style = color_style()
if 'workflows.'+package_name not in settings.INSTALLED_APPS: if 'workflows.'+package_name not in settings.INSTALLED_APPS:
raise CommandError("Package not found in INSTALLED_APPS.") raise CommandError("Package not found in INSTALLED_APPS.")
...@@ -89,6 +93,7 @@ def export_package(package_name,writer): ...@@ -89,6 +93,7 @@ def export_package(package_name,writer):
package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/") package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/")
ensure_dir(package_directory) ensure_dir(package_directory)
widgets_directory = os.path.join(package_directory,"widgets") widgets_directory = os.path.join(package_directory,"widgets")
deprecated_widgets_directory = os.path.join(package_directory,"deprecated_widgets")
ensure_dir(widgets_directory) ensure_dir(widgets_directory)
categories_directory = os.path.join(package_directory,"categories") categories_directory = os.path.join(package_directory,"categories")
ensure_dir(categories_directory) ensure_dir(categories_directory)
...@@ -102,6 +107,9 @@ def export_package(package_name,writer): ...@@ -102,6 +107,9 @@ def export_package(package_name,writer):
for aw in aws: for aw in aws:
aw.update_uid() aw.update_uid()
if os.path.isfile(os.path.join(deprecated_widgets_directory,aw.uid+'.json')):
writer.write(style.ERROR(" - Deprecated widget "+str(aw)+" found! Please import package to remove it. This widget has NOT been exported.\n"))
continue
add_category(aw.category,categories) add_category(aw.category,categories)
serialized_widget = serialize_widget(aw) serialized_widget = serialize_widget(aw)
......
from unicodedata import category from unicodedata import category
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Widget
from django.core import serializers from django.core import serializers
from optparse import make_option from optparse import make_option
import uuid import uuid
...@@ -9,6 +9,7 @@ import sys ...@@ -9,6 +9,7 @@ import sys
from django.conf import settings from django.conf import settings
import json import json
from .export_package import serialize_category, serialize_widget from .export_package import serialize_category, serialize_widget
from django.core.management.color import color_style
def parsewidgetdata(widget_data): def parsewidgetdata(widget_data):
widget = None widget = None
...@@ -29,13 +30,19 @@ def parsewidgetdata(widget_data): ...@@ -29,13 +30,19 @@ def parsewidgetdata(widget_data):
return widget, inputs, outputs, options return widget, inputs, outputs, options
def import_package(package_name,writer): def import_package(package_name,writer):
style = color_style()
package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/") package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/")
widgets_directory = os.path.join(package_directory,"widgets") widgets_directory = os.path.join(package_directory,"widgets")
deprecated_widgets_directory = os.path.join(package_directory,"deprecated_widgets")
categories_directory = os.path.join(package_directory,"categories") categories_directory = os.path.join(package_directory,"categories")
if not os.path.exists(package_directory) or not os.path.exists(widgets_directory) or not os.path.exists(categories_directory): if not os.path.exists(package_directory) or not os.path.exists(widgets_directory) or not os.path.exists(categories_directory):
raise CommandError("Cannot find package data. Are you sure this package has been exported already?") raise CommandError("Cannot find package data. Are you sure this package has been exported already?")
try:
deprecated_widgets_files = os.listdir(deprecated_widgets_directory)
except:
deprecated_widgets_files = []
widget_files = os.listdir(widgets_directory) widget_files = os.listdir(widgets_directory)
category_files = os.listdir(categories_directory) category_files = os.listdir(categories_directory)
...@@ -153,6 +160,24 @@ def import_package(package_name,writer): ...@@ -153,6 +160,24 @@ def import_package(package_name,writer):
stale_os.delete() stale_os.delete()
writer.write(" - Removing stale options\n") writer.write(" - Removing stale options\n")
if deprecated_widgets_files:
for deprecated_widget_file in deprecated_widgets_files:
wfilepath = os.path.join(deprecated_widgets_directory,deprecated_widget_file)
w_file = open(wfilepath,'r')
w_data = json.loads(w_file.read())
w_file.close()
widget, inputs, outputs, options = parsewidgetdata(w_data)
created = False
try:
aw = AbstractWidget.objects.get(uid=widget['fields']['uid'],package=package_name)
if Widget.objects.filter(abstract_widget=aw).count()==0:
writer.write(' - Removing widget '+str(widget['fields']['name'])+'\n')
aw.delete()
else:
writer.write(style.ERROR(' - The widget '+str(widget['fields']['name'])+' is still used in workflows. It was not removed, but it is deprecated!\n'))
except AbstractWidget.DoesNotExist:
pass
if not global_change: if not global_change:
#writer.write(" No changes detected in the widgets.\n") #writer.write(" No changes detected in the widgets.\n")
pass pass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment