How can i get a warning pop up before saving - odoo-14

I'm on Odoo 14 and I wonder how can I get a warning message when some require fields are empty before saving.
This is what I try to do :
warning_message = {
'title': ('Attention!'),
'message': ('Certains champs requis sont vides, est tu sûr de vouloir sauvegarder?'),
}
#api.onsave
def onsave_check_fields(self):
for field in self.fields:
if field.require == True:
if field.value == empty:
if warning_message:
return save()
return save()

For Web-forms on your website (front office):
In your xml template (here : id="your_form_template_id") :
<t t-if="error" t-foreach="error.get('error_message', [])" t-as="err"><h4 class="text-danger" t-esc="err" /></t>
<form action="/process_form_inputs">
<input type="hidden" name="field_required" t-att-value="'phone,name'" />
<input type="hidden" name="submitted" value="1" />
<label for="name">Name</label><input type="text" name="name" value="" />
<label for="name">Email</label><input type="text" name="email" value="" />
<label for="name">Phone</label><input type="text" name="phone" value="" />
</form>
For Web-forms on your website (front office), the right place to check form inputs is not in your odoo-model file: your_(inherited)model/models/your_model.py.
But in your odoo-controller file : your_(inherited)model/controller/main.py
#http.route(['/process_form_inputs'], type='http', methods=['GET', 'POST'], auth="public", website=True, sitemap=False)
def process_form(self, **kw):
# form input fields,values are sent in kw
# IF POSTED
if 'submitted' in kw:
partner_fields = request.env['res.partner']._fields
pre_values = { k: (bool(v) and int(v)) if k in partner_fields and partner_fields[k].type == 'many2one' else v for k, v in kw.items() }
errors, error_msg = self.onsave_check_fields(kw, pre_values)
if not errors:
# YOUR CODE TO CREATE OR UPDATE THE MODEL-record : Partner.create(values) or Partner.browse(id).write(values)
return request.redirect('/success_page')
else:
errors['error_message'] = error_msg
values = kw
render_values={'form_values': values,'error': errors,}
return request.render("your_module.your_form_template_id", render_values)
def onsave_check_fields(self, all_form_values, data):
error = dict()
error_message = []
# Required fields from form
required_fields = [f for f in (all_form_values.get('field_required') or '').split(',') if f]
# error message for empty required fields
for field_name in required_fields:
if not data.get(field_name):
error[field_name] = 'missing'
return error, error_message

Related

prestashop 1.7 new module

I have a problem in prestashop 1.7, when I load the form.tpl in my module I can not do a setAction. What I need is that when I continue in the payment I open a new sale with a payment platform and in prestashop in platform carge the validation I leave the code. help please
main file of the prestashop module
public function hookPaymentOptions($params) {
if (!$this->active) {
return;
}
$this->smarty->assign(
$this->getPaymentApiVars()
);
$apiPayement = new PaymentOption();
$apiPayement->setModuleName($this->name)
->setLogo($this->context->link->getBaseLink().'/modules/hhpayment/views/img/pago.jpg')
// ->setCallToActionText($this->l(''))
//Définition d'un formulaire personnalisé
->setForm($this->fetch('module:hhpayment/views/templates/hook/payment_api_form.tpl'))
->setAdditionalInformation($this->fetch('module:hhpayment/views/templates/hook/displayPaymentApi.tpl'))
->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true));
return [$apiPayement];
}
this is the form.tpl that I charge this without the method but it is by tests
<form action="{$payment_url}" target="_blank" >
<div class="form-group">
{* choix du mode de carte *}
{l s='please choose your card type' mod='hhpayment'}
<div class="radio">
<label>
<input type="radio" name="cb_type" value="mastercard" id="cb_type1" checked="checked" /> Pago internacional
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="cb_type" id="cb_type2" value="visa"/> Pago Nacional
</label>
</div>
</div>
{* Informations pour l'api *}
<input type="hidden" name="success_url" value="{$success_url}" />
<input type="hidden" name="error_url" value="{$error_url}" />
<input type="hidden" name="id_cart" value="{$id_cart}" />
<input type="hidden" name="cart_total" value="{$cart_total}" />
<input type="hidden" name="id_customer" value="{$id_customer}" />
</form>
and this is the validation file
class hhpaymentvalidationModuleFrontController extends ModuleFrontController
{
/**
* Validation du paiement standard
* Puis redirection vers la page de succès de commande
*/
public function postProcess()
{
$cart = $this->context->cart;
$this->abrir("http://davivienda.com");
if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->module->active) {
Tools::redirect('index.php?controller=order&step=1');
}
$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
Tools::redirect('index.php?controller=order&step=1');
}
$currency = $this->context->currency;
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);
//La command passe directement en statut payé
$this->module->validateOrder((int)$cart->id, Configuration::get('PS_OS_PAYMENT'), $total, $this->module->displayName, null, array(), (int)$currency->id, false, $customer->secure_key);
Tools::redirect('index.php?controller=order-confirmation&id_cart='.(int)$cart->id.'&id_module='.(int)$this->module->id.'&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key);
}
public function abrir($param)
{
echo" <script> window.open(URL,'ventana1,'width=300,height=300,scrollbars=NO')</script> ";
}
}
I was able to find a solution to this problem, I don't know if it is the correct one, but it already works for me:
The postProcess method passes it to the main and the validation.php file passes it to the same folder where the main file is.
Then it is time to modify the validation.php file which was changed to the same directory as main, this file should be as follows.
It should be imported
require_once dirname(__FILE__) . '/config/config.inc.php';
require_once dirname(__FILE__) . '/main.php';
Then to avoid a kernel error the following code snippet must be implemented
global $kernel;
if(!$kernel){
require_once _PS_ROOT_DIR_.'/app/AppKernel.php';
$kernel = new \AppKernel('prod', false);
$kernel->boot();
}
After this, it is necessary to implement the logic and receive the parameters by get that the payment screen will return to us after the payment is made, once this data is received, the cart must be recovered and the data sent to the function that was migrated to the main file
ob_start();
$context = Context::getContext();
if (is_null($context->cart)) {
$context->cart = new Cart($context->cookie->id_cart);
}
if (is_null($context->cart->id_currency)) {
$context->cart->id_currency = $context->cookie->id_currency;
}
$cart = $context->cart;
$customer = new Customer($cart->id_customer);
$currency = $cart->id_currency;
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);
$object = new filemain();
$order = $object->methodCreateInMain($cart->id, Configuration::get('PS_OS_PAYMENT'), $total, $currency, $customer->secure_key);
Basically the previous code in validation.php retrieves the cart data and sends it by parameter to the function that was passed to main, where the order will be validated and created.
It should be noted that the return url after payment must be ulrCommerce/module/validation.php
The above worked perfectly for me and is a solution based on various blogs and forums viewed

Odoo : Make changes on Survey module

I am working on editing the survey module in odoo.
When I answer a survey which is composed of pages and then click on submit survey, it calculates the whole score of the survey and displays it. What I want is that it calculates the score of each page and display it when I click on submit survey for example:
It displays: Your score is: 200 points.
And I want it to display: Score of first page : 20 points, Second page: 50 points and so on...
Here is the code that I want to change but I don't know how should I change this function.
questionnaire.py (survey.py) : class survey_user_input
class survey_user_input(osv.Model):
''' Metadata for a set of one user's answers to a particular survey '''
_name = "questionnaire.user_input"
_rec_name = 'date_create'
_description = 'Survey User Input'
def _quizz_get_score(self, cr, uid, ids, name, args, context=None):
ret = dict()
for user_input in self.browse(cr, uid, ids, context=context):
ret[user_input.id] = sum([uil.quizz_mark for uil in user_input.user_input_line_ids] or [0.0])
return ret
_columns = {
'survey_id': fields.many2one('questionnaire.questionnaire', 'Questionnaire', required=True,
readonly=1, ondelete='restrict'),
'date_create': fields.datetime('Date de creation', required=True,
readonly=1, copy=False),
'deadline': fields.datetime("Date limite",
oldname="date_deadline"),
'type': fields.selection([('manually', 'Manuellement'), ('link', 'Lien')],
'Type de reponse', required=1, readonly=1,
oldname="response_type"),
'state': fields.selection([('new', 'Pas encore commence'),
('skip', 'Partiellement acheve'),
('done', 'Termine')],
'Statut',
readonly=True),
'test_entry': fields.boolean('Entree de test', readonly=1),
'token': fields.char("Piece d'identite", readonly=1, required=1, copy=False),
# Optional Identification data
'partner_id': fields.many2one('res.partner', 'Partenaire', readonly=1),
'email': fields.char("E-mail", readonly=1),
# Displaying data
'last_displayed_page_id': fields.many2one('questionnaire.page',
'Derniere page affichee'),
# The answers !
'user_input_line_ids': fields.one2many('questionnaire.user_input_line',
'user_input_id', 'Reponses', copy=True),
# URLs used to display the answers
'result_url': fields.related('survey_id', 'result_url', type='char',
string="Lien public aux resultats du sondage"),
'print_url': fields.related('survey_id', 'print_url', type='char',
string="Lien public au sondage vide"),
'quizz_score': fields.function(_quizz_get_score, type="float", string="Score pour le quiz", store=True)
}
_defaults = {
'date_create': fields.datetime.now,
'type': 'manually',
'state': 'new',
'token': lambda s, cr, uid, c: uuid.uuid4().__str__(),
'quizz_score': 0.0,
}
questionnaire.py (survey.py) : class survey_user_input_line
_name = 'questionnaire.user_input_line'
_description = 'Survey User Input Line'
_rec_name = 'date_create'
_columns = {
'user_input_id': fields.many2one('questionnaire.user_input', 'Entree de l\'utilisateur',
ondelete='cascade', required=1),
'question_id': fields.many2one('questionnaire.question', 'Question',
ondelete='restrict', required=1),
'page_id': fields.related('question_id', 'page_id', type='many2one',
relation='questionnaire.page', string="Page"),
'survey_id': fields.related('user_input_id', 'survey_id',
type="many2one", relation="questionnaire.questionnaire",
string='Questionnaire', store=True),
'date_create': fields.datetime('Date de creation', required=1),
'skipped': fields.boolean('Ignore'),
'answer_type': fields.selection([('text', 'Texte'),
('number', 'Nombre'),
('date', 'Date'),
('free_text', 'Texte Libre'),
('suggestion', 'Suggestion')],
'Type de reponse'),
'value_text': fields.char("Reponse texte"),
'value_number': fields.float("Reponse numerique"),
'value_date': fields.datetime("Reponse date"),
'value_free_text': fields.text("Reponse texte libre"),
'value_suggested': fields.many2one('questionnaire.label', "Reponse suggeree"),
'value_suggested_row': fields.many2one('questionnaire.label', "Reponse en ligne"),
'quizz_mark': fields.float("Score donne pour ce choix")
}
survey_templates.xml
<!-- "Thank you" message when the survey is completed -->
<template id="sfinished" name="Survey Finished">
<t t-call="website.layout">
<div class="wrap">
<div class="container">
<t t-call="questionnaire.back" />
<div class="jumbotron mt32">
<h1>Thank you!</h1>
<div t-field="questionnaire.thank_you_message" class="oe_no_empty" />
<div> You scored <t t-esc="user_input.quizz_score" /> points.</div>
<div>If you want you can <a t-att-href="'/questionnaire/print/%s/%s' % (slug(questionnaire), token)">review your answers</a>.</div>
</div>
</div>
</div>
</t>
</template>
One alternative way is to use pure qweb rather than function field; like this:
<!-- First finding which page is related to the survey. This is a bit odd but I couldn't find any other way!! -->
<t t-set="pages" t-value="dict((l.id, l.page_id.id) for l in user_input.user_input_line_ids).values()" />
<!-- Then print the score per each page -->
<t t-foreach='pages' t-as='p'>
<t t-set="page_score" t-value="sum([(uil.quizz_mark or 0.0) if uil.page_id.id == p else 0.0 for uil in user_input.user_input_line_ids])" />
<div> You scored <t t-esc="page_score" /> points in page <t t-esc="p" />.</div>
</t>
I have not tested this code but logically it seems to work fine.
Thank you so much #MICROCOM .. The line that display the score for each page is diplayed more than once ..
The result Of The code - 1
So i added a condition to just display the score of a certain page once (I don't know if it's correct but it works :p ) .. Here is the code :
<!-- First finding which page is related to the survey. This is a bit odd but I couldn't find any other way!! -->
<t t-set="pages" t-value="dict((l.id, l.page_id.title) for l in user_input.user_input_line_ids).values()" />
<!-- Then print the score per each page -->
<t t-set="previous" t-value="void" />
<t t-foreach='pages' t-as='p'>
<t t-if="p != previous">
<t t-set="page_score" t-value="sum([(uil.quizz_mark or 0.0) if uil.page_id.title == p else 0.0 for uil in user_input.user_input_line_ids])" />
<div> You scored <t t-esc="page_score" /> points in page <t t-esc="p" />.</div>
</t>
<t t-set="previous" t-value="p" />
</t>
The result Of The code - 2
Thank you so much for your help #MICROCOM ^^

How to disable products by default in Prestashop?

When Adding new product in Prestashop, you have to be careful to disable it if it's information not completed.
I tried to find a key in ps_configuration table, but there was nothing related to it, or at least I couldn't find it.
Now the question is how to disable product in Prestashop by default?
If you are using v1.7, there is the option "Default activation status" in Shop Parameters -> Product Settings.
If you are using an older version (1.6.x), try this override:
public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
{
parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
if($id_product == null)
$this->active = false;
}
When you add a new product the id_product is only set when saving.
EDIT: The above override doesn't always work because in the tpl, it checks if its associated to the current shop context, and it will always return false, because the product is not yet saved.
Instead, you can change/override the admin template file /admin/themes/default/template/controllers/products where the active switch is set (about line 196) change to:
<span class="switch prestashop-switch fixed-width-lg">
<input onclick="toggleDraftWarning(false);showOptions(true);showRedirectProductOptions(false);" type="radio" name="active" id="active_on" value="1" {if $product->id != null && ( $product->active || !$product->isAssociatedToShop())}checked="checked" {/if} />
<label for="active_on" class="radioCheck">
{l s='Yes'}
</label>
<input onclick="toggleDraftWarning(true);showOptions(false);showRedirectProductOptions(true);" type="radio" name="active" id="active_off" value="0" {if $product->id == null || (!$product->active && $product->isAssociatedToShop())}checked="checked"{/if} />
<label for="active_off" class="radioCheck">
{l s='No'}
</label>
<a class="slide-button btn"></a>
</span>
Adding the check for the $product->id.

ValueError at /sup/ invalid literal for int() with base 10: ''

i'm new on stackoverflow and in django
I know this has been asked before, but for my circumstance, I can't seem to figure out why this is being thrown
When I try to delete a user, I am given this error from my console:
ValueError: invalid literal for int() with base 10: ''
Here is my view.py where that code is located:
class Delete_user(DeleteView):
model = AuthUser
template_name = 'delUser.html'
def get_success_url(self):
return reverse('ListUserAdmin')
the templete :
<form action="{% url 'Sup-User' pk=AuthUser.id %}" method="POST">
{% csrf_token %}
<input type="submit" value="Yes, delete." />
No, cancel.
</form>
and this is the url :
url(r'^sup/(?P<pk>.*)', log.views.Delete_user.as_view(),
name='Sup-User',),

How to get include preg_match_all results in header location?

I have the following function written in function.php in a wordpress theme
function get_mms($my_post) {
$post_id = $my_post; //$_GET["p"];
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
preg_match_all('#\[mms\](.+)\[\/mms\]#', $queried_post->post_content, $matches);
echo 'EN: ' . $matches[1][0];
echo '<br><br><form action= " ' . get_permalink( $id ) . ' " method="post">
Numar: <input type="text" name="fname" />
<input name="submito" type="submit" />
</form>';
$numero = $_POST["fname"];
if(isset($_POST['submito'])&& $_POST['fname']){
$numero = $_POST["fname"];
header("Location: http://server/&to=$numero&text='.$matches[1][0].'&from=Moldcell");
}
When I submit the form instead of getting the value of $matches[1][0] I get Array[0].
Is there someting I did wrong? What can I do to make it grab the value of preg_match_all results?