Skip to content

Commit

Permalink
added new code to move visible to hide files and viceversa
Browse files Browse the repository at this point in the history
  • Loading branch information
robdayz committed Jul 11, 2018
1 parent e2b856a commit cc69923
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
128 changes: 128 additions & 0 deletions genweb/organs/browser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import json
import transaction
from Products.statusmessages.interfaces import IStatusMessage
from genweb.organs.content.sessio import ISessio
from plone.namedfile.file import NamedBlobFile


class changeMigrated(grok.View):
Expand Down Expand Up @@ -184,3 +186,129 @@ def render(self):

results.append(element)
return json.dumps(results, indent=2, sort_keys=True)


class MovePublicfilestoPrivate(grok.View):
# After migrating data, some files came with an error.
# They were located as public files, but must be Private.
# This view change all files from public to private and viceversa
# in a given session

grok.context(ISessio)
grok.name('movefilestoprivateorpublic')
grok.require('cmf.ManagePortal')
grok.layer(IGenwebOrgansLayer)

def showfiles(self):
all_brains = api.content.find(portal_type='genweb.organs.file', path=self.context.absolute_url_path())
results = []
for brain in all_brains:
obj = brain.getObject()
if obj.visiblefile:
visible = obj.visiblefile.filename
else:
visible = 'Empty'
if obj.hiddenfile:
hidden = obj.hiddenfile.filename
else:
hidden = 'Empty'
element = {
'visiblefile': visible,
'hiddenfile': hidden,
'path': obj.absolute_url()
}

results.append(element)
return json.dumps(results, indent=2, sort_keys=True)

def movetoPrivate(self):
all_brains = api.content.find(portal_type='genweb.organs.file', path=self.context.absolute_url_path())
results = []
for brain in all_brains:
obj = brain.getObject()
# Show initial values
if getattr(obj, 'visiblefile', False):
initial_visible = obj.visiblefile.filename
else:
initial_visible = 'Empty'
if getattr(obj, 'hiddenfile', False):
initial_hidden = obj.hiddenfile.filename
else:
initial_hidden = 'Empty'
final_visible = 'Not modified'
final_hidden = 'Not modified'
if not obj.hiddenfile and obj.visiblefile:
initial_visible = obj.visiblefile.filename
initial_hidden = 'Empty'
# Move public to private
obj.hiddenfile = NamedBlobFile(
data=obj.visiblefile.data,
contentType=obj.visiblefile.contentType,
filename=obj.visiblefile.filename
)
transaction.commit()
del obj.visiblefile
final_visible = 'Empty (Deleted)'
final_hidden = obj.hiddenfile.filename
element = {
'original-visiblefile': initial_visible,
'original-hiddenfile': initial_hidden,
'path': obj.absolute_url(),
'final-visiblefile': final_visible,
'final-hiddenfile': final_hidden,
}
results.append(element)
return json.dumps(results, indent=2, sort_keys=True)

def movetoPublic(self):
all_brains = api.content.find(portal_type='genweb.organs.file', path=self.context.absolute_url_path())
results = []
for brain in all_brains:
obj = brain.getObject()
# Show initial values
if getattr(obj, 'visiblefile', False):
initial_visible = obj.visiblefile.filename
else:
initial_visible = 'Empty'
if getattr(obj, 'hiddenfile', False):
initial_hidden = obj.hiddenfile.filename
else:
initial_hidden = 'Empty'
final_visible = 'Not modified'
final_hidden = 'Not modified'
if obj.hiddenfile and not obj.visiblefile:
initial_visible = 'Empty'
initial_hidden = obj.hiddenfile.filename
# Move private to public
obj.visiblefile = NamedBlobFile(
data=obj.hiddenfile.data,
contentType=obj.hiddenfile.contentType,
filename=obj.hiddenfile.filename
)
transaction.commit()
del obj.hiddenfile
final_visible = obj.visiblefile.filename
final_hidden = 'Empty (Deleted)'
element = {
'original-visiblefile': initial_visible,
'original-hiddenfile': initial_hidden,
'path': obj.absolute_url(),
'final-visiblefile': final_visible,
'final-hiddenfile': final_hidden,
}
results.append(element)
return json.dumps(results, indent=2, sort_keys=True)

def render(self):
""" Execute /movefilestoprivateorpublic """
if 'showfiles' in self.request.form:
return self.showfiles()
if 'move2Private' in self.request.form:
return self.movetoPrivate()
if 'move2Public' in self.request.form:
return self.movetoPublic()

return 'DANGER: This code moves files from public to private and viceversa. <br/>\
<br/>usage: /movefilestoprivateorpublic? <b><a href="?showfiles">showfiles</a></b>\
| <b><a href="?move2Private">move2Private</a></b> \
| <b><a href="?move2Public">move2Public</a></b>'
4 changes: 2 additions & 2 deletions genweb/organs/locales/ca/LC_MESSAGES/genweb.organs.po
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,11 @@ msgstr "No assistents"

#: ./genweb/organs/hooks.py:77
msgid "Modified acord"
msgstr "S'ha modificat un acord."
msgstr "S'ha modificat un acord"

#: ./genweb/organs/hooks.py:83
msgid "Modified acta"
msgstr "S'ha modificat una acta."
msgstr "S'ha modificat una acta"

#: ./genweb/organs/search.pt:233
msgid "Modified dates"
Expand Down

0 comments on commit cc69923

Please sign in to comment.