Quantcast
Viewing all articles
Browse latest Browse all 3535

UW Oshkosh How-To's: PloneFormGen scripts to store uploaded files in a folder

These scripts are part of the Communication Studies Jobs site, that use

  • a PloneFormGen form allows job applicants to upload between one and five files
  • another PloneFormGen form that allows references to upload a letter of recommendation

All files are automatically stored in a folder for each applicant, based on the applicant's email address.  The scripts check first if they need to create the folder.

The screencast showing the jobs site is at https://www.youtube.com/watch?v=1kuHmr84CoY&feature=youtu.be

Script for Applicant's Form

# Available parameters:
#  fields  = HTTP request form fields as key value pairs
#  request = The current HTTP request.
#            Access fields by request.form["myfieldname"]
#  ploneformgen = PloneFormGen object
#
# Return value is not processed -- unless you
# return a dictionary with contents. That's regarded
# as an error and will stop processing of actions
# and return the user to the form. Error dictionaries
# should be of the form {'field_id':'Error message'}

from Products.CMFPlone.utils import normalizeString

applicant_name = request.form['your-name']
applicant_email = request.form['replyto']
category = "Applicant Materials"

parent = context.aq_inner.aq_parent
grandparent = parent.aq_inner.aq_parent
folder = grandparent.get('all-materials')

id = normalizeString(applicant_email)
if hasattr(folder, id):
    applicant_folder = folder[id]
else:
    folder.invokeFactory(id=id, type_name='Folder')  
    applicant_folder = folder[id]

def handle_file(file):
	file_id = normalizeString(file.filename)
	if file_id and file_id != "":
		while (file_id in applicant_folder.objectIds()):
			file_id += "_1"
		newAttachmentId = applicant_folder.invokeFactory(id=file_id, type_name='File')
		newAttachment = getattr(applicant_folder, file_id)

		newAttachment.setTitle("%s applicant material %s" % (applicant_name, file.filename))
		newAttachment.setDescription(
		    "Uploaded by %s <%s>" 
		    % (applicant_name, applicant_email)
		    )
		newAttachment.setFile(file)
		newAttachment.setSubject(category)
		newAttachment.reindexObject()
		return newAttachmentId
	else:
		return ""

attachment_ids = []
attachment_ids.append(handle_file(request.form['file-to-upload_file']))
attachment_ids.append(handle_file(request.form['file-to-upload2_file']))
attachment_ids.append(handle_file(request.form['file-to-upload3_file']))
attachment_ids.append(handle_file(request.form['file-to-upload4_file']))
attachment_ids.append(handle_file(request.form['file-to-upload5_file']))

context.plone_utils.addPortalMessage("Uploaded attachment(s) %s" % attachment_ids, 'info')

# only useful if form users are logged in
#portal_state = ploneformgen.restrictedTraverse('@@plone_portal_state')
#url = portal_state.portal_url() + '/search'
#request.response.redirect('%s?Creator=%s&sort_on=created&sort_order=reverse' % (url, portal_state.member().getId()))

 

Script for Reference's Form

 

# Available parameters:
#  fields  = HTTP request form fields as key value pairs
#  request = The current HTTP request.
#            Access fields by request.form["myfieldname"]
#  ploneformgen = PloneFormGen object
#
# Return value is not processed -- unless you
# return a dictionary with contents. That's regarded
# as an error and will stop processing of actions
# and return the user to the form. Error dictionaries
# should be of the form {'field_id':'Error message'}

from Products.CMFPlone.utils import normalizeString

applicant_name = request.form['name-of-applicant']
applicant_email = request.form['applicants-email-address']
submitter_name = request.form['your-name']
submitter_email = request.form['replyto']
file = request.form['file-to-upload_file']
category = "Letter of Recommendation"

parent = context.aq_inner.aq_parent
grandparent = parent.aq_inner.aq_parent
folder = grandparent.get('all-materials')

id = normalizeString(applicant_email)
if hasattr(folder, id):
    applicant_folder = folder[id]
else:
    folder.invokeFactory(id=id, type_name='Folder')  
    applicant_folder = folder[id]

file_id = normalizeString(file.filename)
newAttachmentId = applicant_folder.invokeFactory(id=file_id, type_name='File')
newAttachment = getattr(applicant_folder, file_id)

newAttachment.setTitle("%s letter of recommendation %s" % (applicant_name, file.filename))
newAttachment.setDescription(
    "Submitted by %s <%s> for %s <%s>" 
    % (submitter_name, submitter_email, applicant_name, applicant_email)
    )
newAttachment.setFile(file)
newAttachment.setSubject(category)
newAttachment.reindexObject()

context.plone_utils.addPortalMessage("Uploaded an attachment with ID %s" % newAttachmentId, 'info')

# only useful if form users are logged in
#portal_state = ploneformgen.restrictedTraverse('@@plone_portal_state')
#url = portal_state.portal_url() + '/search'
#request.response.redirect('%s?Creator=%s&sort_on=created&sort_order=reverse' % (url, portal_state.member().getId()))

 


Viewing all articles
Browse latest Browse all 3535

Trending Articles