Created
June 24, 2021 17:00
-
-
Save enyachoke/57efec171d18511ae726e40909b2ef7c to your computer and use it in GitHub Desktop.
SENAITE initializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################ | |
# | |
# Buildout Configuration File for Plone | |
# ------------------------------------- | |
# | |
# ALWAYS back up all Plone/Zope data and components | |
# before changing configuration. | |
# | |
# Running "bin/buildout" will update your installation, | |
# installing missing components as necessary. | |
# | |
# This will update the add-on products you've added in the eggs= lines. | |
# This will not, however, upgrade Plone itself (or anything else you've | |
# pinned with a version specification). To upgrade Plone itself, see the | |
# comments in "Plone Component Versions". | |
# | |
# Tutorial instructions for using zc.buildout for | |
# configuration management are available at: | |
# https://docs.plone.org/manage/installing/installing_addons.html | |
# Full details at http://pypi.python.org/pypi/zc.buildout | |
# | |
############################################ | |
[buildout] | |
# buildout.sanitycheck makes sure you're not running buildout | |
# as root. | |
extensions = | |
buildout.sanitycheck | |
buildout.environ | |
############################################ | |
# Plone Component Versions | |
# ------------------------ | |
# This version of the Unified Installer has the components of Plone 5 | |
# preloaded so that it can install without an Internet connection. | |
# If you want to update, uncomment the "http://..." line below, | |
# edit it to point to the current version URL, comment out the | |
# "versions.cfg" line and run "bin/buildout" while attached to the | |
# Internet. Generally, you only want to do that as part of a planned migration. | |
# Note that if you are updating components, you should also check the versions | |
# section at the end of this file, since recipes or components other than | |
# those of Zope and Plone may need updating at the same time. | |
# | |
extends = | |
base.cfg | |
release-5.2.2-versions.cfg | |
# https://dist.plone.org/release/5.2.2/versions.cfg | |
# If you change your Plone version, you'll also need to update | |
# the repository link below. | |
find-links += | |
https://dist.plone.org/release/5.2.2 | |
# This user will own the non-data parts of the installation, and should be used to | |
# run buildout. | |
buildout-user = enyachoke | |
# A flag to tell the Unified Installer whether or not to document sudo use. | |
need-sudo = no | |
############################################ | |
# Eggs | |
# ---- | |
# Add an indented line to the eggs section for any Python | |
# eggs or packages you wish to include in your Plone instance. | |
# | |
# Note that versions may be specified here or in the [versions] | |
# section below. You should always specify versions that you know | |
# are compatible with the Plone release and at an acceptable | |
# development level. | |
# | |
# If you update to a later version of Plone, remove the hotfix. | |
# | |
eggs = | |
Plone | |
Pillow | |
Products.PloneHotfix20200121 | |
senaite.lims | |
collective.recipe.plonesite | |
ftw.oidcauth | |
############################################ | |
# ZCML Slugs | |
# ---------- | |
# Some eggs need ZCML slugs to tell Zope to | |
# use them. This is increasingly rare. | |
zcml = | |
# plone.reload | |
############################################ | |
# Development Eggs | |
# ---------------- | |
# You can use paster to create "development eggs" to | |
# develop new products/themes. Put these in the src/ | |
# directory. | |
# You will also need to add the egg names in the | |
# eggs section above, and may also need to add them | |
# to the zcml section. | |
# | |
# Provide the *paths* to the eggs you are developing here: | |
develop = | |
# src/ftw.oidcauth | |
############################################ | |
# var Directory | |
# ------------- | |
# Sets the target directory for the "var" components of the install such as | |
# database and log files. | |
# | |
var-dir=${buildout:directory}/var | |
############################################ | |
# Backup Directory | |
# ---------------- | |
# Sets the target directory for the bin/backup and bin/snapshotbackup | |
# commands. Default is inside this project's var directory, but ideally | |
# this should be on a separate volume or backup server. | |
# | |
backups-dir=${buildout:var-dir} | |
############################################ | |
# Initial User | |
# ------------ | |
# This is the user id and password that will be used to create the initial | |
# user id that will allow you to log in and create a Plone site. This only | |
# sets the initial password; it will not allow you to change an already | |
# existing password. If you change the admin password via the web interface, | |
# the one below will no longer be valid. | |
# If you find yourself locked out of your Zope/Python installation, you may | |
# add an emergency user via "bin/plonectl adduser". | |
user=admin:admin | |
############################################ | |
# Debug Options | |
# ------------- | |
# Start Zope/Plone instances in "fg" mode to turn on debug mode; | |
# this will dramatically slow Plone. | |
# | |
# Add-on developers should turn deprecation warnings on | |
deprecation-warnings = off | |
# change verbose-security to "on" for useful security errors while developing | |
verbose-security = off | |
############################################ | |
# Parts Specification | |
#-------------------- | |
# Specifies the components that should be included in the buildout. | |
# Most are defined in the base.cfg extension; you may add your | |
# own if you need them at the end of this file. | |
parts = | |
instance | |
repozo | |
backup | |
zopepy | |
unifiedinstaller | |
plonesite | |
############################################ | |
# Major Parts | |
# ---------------------- | |
# These common parts make use of sane base settings from | |
# base.cfg. To customize a part, just add whatever options | |
# you need. Read base.cfg for common settings. | |
[instance] | |
<= instance_base | |
recipe = plone.recipe.zope2instance | |
http-address = 8080 | |
[plonesite] | |
recipe = collective.recipe.plonesite | |
instance = instance | |
site-id = senaite | |
profiles-initial = Products.CMFPlone:dependencies | |
after-install = | |
${buildout:directory}/bin/instance -O senaite run ${buildout:directory}/src/collective.initializer/src/collective/initializer/initialize.py | |
profiles = | |
senaite.lims:default | |
upgrade-portal = False | |
upgrade-all-profiles = False | |
enabled = False | |
[print] | |
recipe = mr.scripty | |
install = | |
print("Found in environ: PATH=${__environ__:PATH}") | |
############################################ | |
# Versions Specification | |
# ---------------------- | |
# Version information supplied here will "pin" Python packages to a particular | |
# version number, even when you use the "newest" flag running buildout. | |
# Specifying versions for all packages is a good idea and can prevent | |
# accidental changes when you add new packages to your buildout. | |
# Note that versions specified here will override those specified earlier | |
# in the configuration, including those from the Plone and Zope version | |
# config files. | |
# | |
[versions] | |
buildout.sanitycheck = 1.0.2 | |
collective.recipe.backup = 4.1.0 | |
plone.recipe.unifiedinstaller = 5.2b1 | |
Products.PloneHotfix20200121 = 1.1 | |
setuptools=42.0.2 | |
zc.buildout=2.13.3 | |
senaite.lims=2.0.0rc3 | |
senaite.jsonapi=2.0.0rc2 | |
Werkzeug = 1.0.0 | |
cryptography = 2.9.2 | |
# dm.xmlsec.binding = 2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from plone import api as ploneapi | |
from Products.CMFCore.utils import getToolByName | |
from bika.lims import PMF | |
from bika.lims import logger | |
from bika.lims.interfaces import ISetupDataImporter | |
from openpyxl import load_workbook | |
from pkg_resources import resource_filename | |
from zope.component import getAdapters | |
import traceback | |
import tempfile | |
import transaction | |
try: | |
from zope.component.hooks import getSite | |
except: | |
# Plone < 4.3 | |
from zope.app.component.hooks import getSite | |
class LoadSetupData(): | |
def __init__(self, context): | |
self.context = context | |
# dependencies to resolve | |
self.deferred = [] | |
def solve_deferred(self, deferred=None): | |
unsolved = [] | |
deferred = deferred if deferred else self.deferred | |
for d in self.deferred: | |
src_obj = d['src_obj'] | |
src_field = src_obj.getField(d['src_field']) | |
multiValued = src_field.multiValued | |
src_mutator = src_field.getMutator(src_obj) | |
src_accessor = src_field.getAccessor(src_obj) | |
tool = getToolByName(self.context, d['dest_catalog']) | |
try: | |
proxies = tool(d['dest_query']) | |
except: | |
continue | |
if len(proxies) > 0: | |
obj = proxies[0].getObject() | |
if multiValued: | |
value = src_accessor() | |
value.append(obj.UID()) | |
else: | |
value = obj.UID() | |
src_mutator(value) | |
else: | |
unsolved.append(d) | |
self.deferred = unsolved | |
return len(unsolved) | |
def load_data(self): | |
self.dataset_project = 'bika.lims' | |
self.dataset_name = 'test' | |
workbook = None | |
try: | |
workbook = load_workbook(filename='/home/enyachoke/Code/Mekom/senaite/senaitelims/src/collective.initializer/src/collective/initializer/configuration.xlsx') # , use_iterators=True) | |
except AttributeError: | |
print "" | |
print traceback.format_exc() | |
print "Error while loading " | |
adapters = [[name, adapter] | |
for name, adapter | |
in list(getAdapters((self.context, ), ISetupDataImporter))] | |
for sheetname in workbook.get_sheet_names(): | |
transaction.savepoint() | |
ad_name = sheetname.replace(" ", "_") | |
if ad_name in [a[0] for a in adapters]: | |
adapter = [a[1] for a in adapters if a[0] == ad_name][0] | |
adapter(self, workbook, self.dataset_project, self.dataset_name) | |
adapters = [a for a in adapters if a[0] != ad_name] | |
transaction.commit() | |
for name, adapter in adapters: | |
transaction.savepoint() | |
adapter(self, workbook, self.dataset_project, self.dataset_name) | |
transaction.commit() | |
check = len(self.deferred) | |
while len(self.deferred) > 0: | |
new = self.solve_deferred() | |
logger.info("solved %s of %s deferred references" % ( | |
check - new, check)) | |
if new == check: | |
raise Exception("%s unsolved deferred references: %s" % ( | |
len(self.deferred), self.deferred)) | |
check = new | |
logger.info("Rebuilding bika_setup_catalog") | |
bsc = getToolByName(self.context, 'bika_setup_catalog') | |
bsc.clearFindAndRebuild() | |
logger.info("Rebuilding bika_catalog") | |
bc = getToolByName(self.context, 'bika_catalog') | |
bc.clearFindAndRebuild() | |
logger.info("Rebuilding bika_analysis_catalog") | |
bac = getToolByName(self.context, 'bika_analysis_catalog') | |
bac.clearFindAndRebuild() | |
message = PMF("Changes saved.") | |
self.context.plone_utils.addPortalMessage(message) | |
def main(app): | |
loader = LoadSetupData(app.senaite) | |
loader.load_data() | |
if "app" in locals(): | |
main(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment