Restrict upload by filetype or mimetype using Dexterity on Plone - plone

I have a custom content type, built with dexterity. In the schema (The schema is listed below), I use 'plone.namedfile.field.NamedFile' for attachements/uploads.
I would like to restrict uploads so that only mp3 files can be attached to my content type. What is the best approach for achieving this?
Here is the full schema/model for my content type:
<model xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>
<field name="date" type="zope.schema.Date">
<description />
<title>Date</title>
</field>
<field name="speaker" type="zope.schema.TextLine">
<description />
<title>Speaker</title>
</field>
<field name="service" type="zope.schema.Choice">
<description />
<title>Service</title>
<values>
<element>1st Service</element>
<element>2nd Service</element>
</values>
</field>
<field name="audio_file" type="plone.namedfile.field.NamedFile">
<description />
<title>Audio File</title>
</field>
</schema>
</model>
I shall begin my search here: http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors

I've decided to use javascript for my first line of validation.
I've based my solution on information found at <input type="file"> limit selectable files by extensions
Based on the advice my script looks something like this:
$(document).ready( function() {
function checkFile(event) {
var fileElement = document.getElementById("form-widgets-audio_file-input");
var fileExtension = "";
if (fileElement.value.lastIndexOf(".") > 0) {
fileExtension = fileElement.value.substring(fileElement.value.lastIndexOf(".") + 1, fileElement.value.length);
}
if (fileExtension == "mp3") {
return true;
}
else {
alert("You must select a mp3 file for upload");
return false;
}
}
$("form#form").bind("submit",checkFile);
});
This is half the solution, next I'll need to add validation on the server side.

Related

How to call a FORM VIEW via a button? (Odoo 13 Enterprise)

I defined a button via "Server Actions" in this FORM VIEW:
And created another FORM VIEW from the submenu.
Then I'd tried to call this FORM VIEW via the button, but it's not worked.
So how to call this FORM VIEW via the button?
Please help!
Thank you!
Try to give
"view_mode" : "form"
in xml :
<record id="account_common_report_view" model="ir.ui.view">
<field name="name">Common Report</field>
<field name="model">account.common.report</field>
<field name="arch" type="xml">
<form string="Report Options">
<group col="4">
<header>
<button name="check_report" string="Print" type="object"
default_focus="1" class="oe_highlight"/>
<button string="Cancel" class="btn btn-secondary" />
</header>
</form>
</field>
</record>
python :
call every things call
def check_report(self):
self.ensure_one()
data = {}
data['ids'] = self.env.context.get('active_ids', [])
data['model'] = self.env.context.get('active_model', 'ir.ui.menu')
data['form'] = self.read(['date_from', 'date_to', 'journal_ids', 'target_move', 'company_id'])[0]
used_context = self._build_contexts(data)
data['form']['used_context'] = dict(used_context, lang=get_lang(self.env).code)
return self.with_context(discard_logo_check=True)._print_report(data)

Try to override(noupdate=1) email template in Odoo 12

I am trying override base email template(noupdate=1) but, unable to override. Also, search for my issue but didn't get proper solution.
So, anybody can help me for this issue.
my code is like:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="0">
<!-- Email template for reset password -->
<delete id="auth_signup.reset_password_email" model="mail.template"/>
<record id="reset_password_email" model="mail.template">
. . .
</record>
<!-- Email template for new users -->
<delete id="auth_signup.set_password_email" model="mail.template"/>
<record id="set_password_email" model="mail.template">
. . .
</record>
</data>
</odoo>
This error comes when create new user:
ValueError: External ID not found in the system: auth_signup.reset_password_email
Thanks in advance
well, you don't need to override the existing email template. you may need a new one. you could also delete the old one
<record id="reset_password_email" model="mail.template">
<field name="name">Auth Signup: Reset Password</field>
<field name="model_id" ref="base.model_res_users"/>
<field name="subject">Password reset</field>
<field name="email_from">"${object.company_id.name | safe}" <${(object.company_id.email or user.email) | safe}></field>
<field name="email_to">${object.email_formatted | safe}</field>
<field name="body_html" type="html">
<p>whatever email template you want & remember you could use OBJECT AS FOLLOWING</p>
<span style="font-size: 20px; font-weight: bold;">
${object.name}
</span>
</field>
<field name="lang">${object.lang}</field>
<field name="auto_delete" eval="True"/>
<field name="user_signature" eval="False"/>
</record>
please note that you custom template id would be names as custom_module.reset_password_email & it will replace auth_signup.reset_password_email.
or you could follow:
Odoo - How to update non updateable records by XML

How to add custom action on workflow form in alfresco to rename document name

I want to add document name rename action on workflow form in alfresco share.
Is there any way to do this.
Please provide the required steps and sample code snipet if possible.
Please reply is somebady did this before.
Thanks in advance.
The first thing which you need to do is to define the form configuration for the workflow task form for which you have taken the screenshot.You can define it inside the share-config-custom.xml. Form configuration should be something like below.
<config evaluator="task-type" condition="**NAME OF YOUR TASK**">
<forms>
<form>
<field-visibility>
<!-- **FIELDS WHICH YOU WANT TO MAKE VISIBLE** -->
<show id="wf:requiredApprovePercent" />
<show id="bpm:workflowDueDate" />
<show id="bpm:workflowPriority" />
<show id="packageItems" />
<show id="bpm:sendEMailNotifications" />
<show id="bpm:comment" />
</field-visibility>
<appearance>
<!-- **FIELDS WHICH FOR WHICH YOU WANT TO CUSTOMIZE TEMPLATE** -->
<field id="bpm:workflowPriority" label-id="workflow.field.priority">
<control template="/org/alfresco/components/form/controls/workflow/priority.ftl" />
</field>
<field id="bpm:sendEMailNotifications">
<control template="/org/alfresco/components/form/controls/workflow/email-notification.ftl" />
</field>
<field id="bpm:comment" label-id="workflow.field.comment">
<control template="/org/alfresco/components/form/controls/textarea.ftl" />
</field>
</appearance>
</form>
</forms>
</config>
Once you define this you need to customize the template for the packageItems field and create a new field template for it.You can take a reference from existing template "org\alfresco\components\form\controls\workflow\packageitems.ftl"
Above ftl template include one more ftl library, named as association.ftl.You need to create a new association.ftl and include it inside this file.
You need to make change in that file as well.Let's come to that later.
Now you need to create one javascript file which should extend object-finder.js.How to extend that file is very well explained in below link.
http://alfrescoblog.com/2014/05/28/alfresco-share-custom-object-finder-js/
Once you create the extended file , it should have content similar to below.You still need to customize below function as per your requirement.You need to add the coded for adding pencil icon and handling onclick event for it.
(function() {
var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event;
var $html = Alfresco.util.encodeHTML,
$hasEventInterest = Alfresco.util.hasEventInterest,
$combine = Alfresco.util.combinePaths;
Alfresco.PackageItemsObjectFinder = function Alfresco_PackageItemsObjectFinder(
htmlId, currentValueHtmlId) {
Alfresco.PackageItemsObjectFinder.superclass.constructor.call(this,
htmlId, currentValueHtmlId);
// Re-register with our own name
this.name = "Alfresco.PackageItemsObjectFinder";
Alfresco.util.ComponentManager.reregister(this);
return this;
};
YAHOO.extend(Alfresco.PackageItemsObjectFinder, Alfresco.ObjectFinder, {
fnRenderCellListItemName: function ObjectFinder_fnRenderCellListItemName()
{
var scope = this;
return function ObjectFinder_fnRenderCellListItemName(elCell, oRecord, oColumn, oData)
{
var item = oRecord.getData(),
titles = item.title ? $html(item.title) : scope.msg("label.none"),
modifiedOn = item.modified ? Alfresco.util.formatDate(Alfresco.util.fromISO8601(item.modified)) : null,
title = $html(item.name);
if (scope.options.showLinkToTarget && scope.options.targetLinkTemplate !== null)
{
var link;
if (YAHOO.lang.isFunction(scope.options.targetLinkTemplate))
{
link = scope.options.targetLinkTemplate.call(scope, oRecord.getData());
}
else
{
//Discard template, build link from scratch
var linkTemplate = (item.site) ? Alfresco.constants.URL_PAGECONTEXT + "site/{site}/document-details?nodeRef={nodeRef}" : Alfresco.constants.URL_PAGECONTEXT + "document-details?nodeRef={nodeRef}";
link = YAHOO.lang.substitute(linkTemplate,
{
nodeRef : item.nodeRef,
site : item.site
});
}
title = '' + $html(item.displayName?item.displayName:item.name) + '';
}
var template = '<h3 class="name">' + title + '</h3>';
template += '<div class="description">' + "Title" + ': ' + titles + '</div>';
template += '<div class="viewmode-label">' + scope.msg("form.control.object-picker.modified-on") + ': ' + (modifiedOn ? modifiedOn : scope.msg("label.none")) + '</div>';
elCell.innerHTML = template;
};
}
});
})();
Once you extend this, than you need give the reference of this extended class in the association ftl file.
The change in assiciation file will be something like below.
var ${picker} = new Alfresco.PackageItemsObjectFinder("${controlId}", "${fieldHtmlId}")
Instead of
var ${picker} = new Alfresco.ObjectFinder("${controlId}", "${fieldHtmlId}")

Alfresco share worflow form validation

I have been trying to validate my alfresco share workflow form for several days without success. This is what i have done.
Configured my workflow in the share-config-custom.xml located in %TOMCAT_HOME%tomcat\shared\classes\alfresco\web-extension
set my contraint handler as follows.
<constraint-handlers>
<constraint type="MANDATORY"
validation-handler="Alfresco.forms.validation.examplestaffnumber"
event="keyup" />
</constraint-handlers>
</field>
This field i have set to mandatory
< label-id="Staff Number" id="leave:staffnumber" mandatory="true">
I have created the contraint hanlder javascript and placed it at %ALFRESCO_HOME%\tomcat\webapps\share\js folder. This is both js and min.js
Finaly added the js in form.get.head.ftl located at %ALFRESCO_HOME%tomcat\webapps\share\WEB-INF\classes\alfresco\site-webscripts\org\alfresco\components\form
folder like this
<#script type="text/javascript" src="${page.url.context}/res/js/examplevalidation.js">
When I select my worflow form and key values in the staff number form nothing happens. I have checked in the firebug but there is no any call to the js.
Where could i have gone wrong?
I think you have not added dependencies for your java script. To do that add below code in your share-config-custom.xml located in %ALFRESCO_HOME%tomcat\shared\classes\alfresco\web-extension
<config>
<forms>
<dependencies>
<js src="/js/examplevalidation.js" />
</dependencies>
</forms>
</config>
And your constrains handler should be like
<field id="leave:staffnumber" label-id="Staff Number" mandatory="true">
<control template="/org/alfresco/components/form/controls/textfield.ftl" />
<constraint-handlers>
<constraint type="MANDATORY" validation-handler="Alfresco.forms.validation.examplestaffnumber" event="blur"/>
</constraint-handlers>
</field>
And function in your js should be like this:
Alfresco.forms.validation.examplestaffnumber = function examplestaffnumber(
field, args, event, form, silent, message) {
// your code with return statement
}
Hope this helps!!!

Castor Hashtable polymorphism

Good Day
I am attempting to use castor to construct a HashTable that has multiple implementations of an abstract class.
here is the parent "config"
<class name="com.Config">
<map-to xml="config" />
<field name="rulesMap" collection="hashtable">
<bind-xml auto-naming="deriveByClass" >
<class name="org.exolab.castor.mapping.MapItem">
<field name="key" type="java.lang.String">
<bind-xml name="name" node="attribute" />
</field>
<field name="value" type=com.Rule">
</field>
</class>
</bind-xml>
</field>
</class>
'com.Rule' is an Abstract Class and
at the end of the day i would like an xml struct that looks like this
<config>
<rule-impl1 name="ruleType1Instance1" ruleField="field" />
<rule-impl2 name="ruleType2Instance2" ruleField="field" ruleImpl2Field1="..." />
</config>
I'm not sure there is enough detail or a question that is well formed here to give an accurate answer, but I was doing something pretty similar and ran into some roadblocks. Thought I'd provide my 2 cents. I'm not as familiar with Castor as I am some other XML frameworks and in my case Castor is doing it's automatic marshalling/unmarshalling instead of us manually writing the code to decide when we want it to be done. If we were manually doing that piece I thought we would have been able to make decisions to unmarshall to specific classes that extend the abstract class.
With all my disclaimers out of the way, what you could do.
**If you can add a field to the request/response then create something like this:
public class RuleContainer {
private RuleType ruleType; // possibly build enum or other non-java equivalent
private RuleImpl1 ruleImpl1;
private RuleImpl2 ruleImpl2;
private RuleImpl...N ruleImpl...N;
// getters & setters, etc
}
Then the value of your table is changed to
<field name="value" type="com.RuleContainer"></field>
and include your mapping of the RuleContainer
<class name="com.RuleContainer">
<field name="ruleType" type="com.RuleType"
<field name="ruleImpl1" type="com.RuleImpl1">
<field name="ruleImpl2" type="com.RuleImpl2">
<field name="ruleImpl...N" type="com.RuleImpl...N">
</class>
also include mappings of each implementation whatever those may look like. In my case I've broken each implementation mapping out into a separate file and used the
<include href="" />
tag to include those extraneous mappings in the parent file.
All of this sets you up to use that RuleType field to know which rule in the RuleContainer is valid (the rest will be null as the Castor default is required="false"). The logic to work with each implementation of a Rule is simple to write from there.
Hope this helps.

Resources