Search code examples
odoo

How to disable days in odoo calendar?


I have an entity "appointment" with a date_appointment attribute. I have also created a Calendar view for that appointment. An appointment should never be scheduled on weekends, so I have disable those days in odoo datepicker (in the doem view). But when I clic on any Satursday or sunday in the Calendar view it shows the form view where I can create an appointment for that day. Is there any way I can disable those days in Odoo Calendar, so the user cannot clic on any weekend an create appointments. I have already disable the quick_add option

<record id="appointment_view_calendar" model="ir.ui.view">
    <field name="name">appointment.view.calendar</field>
    <field name="model">tico_hospital.appointment</field>
    <field name="arch" type="xml">
        <calendar string="Consultas programadas" mode="week" date_start="date_appointment" quick_add="False" color="patient_id">
            <field name="patient_id"/>
            <field name="reason"/>         
        </calendar>
    </field>
</record>  

Solution

  • According to the documentation, there is no option to disable the click on a specific day in the calendar view.

    You can add a constraint to alert users when they try to create an appointment for weekdays.

    If you need to avoid opening the form view when clicking on weekdays, override the _onOpenCreate method in the calendar controller.

    The following example checks if the user has clicked on a weekday, if yes it shows an alert and avoids opening the form view.

    odoo.define('web.CustomCalendar', function (require) {
    "use strict";
        var core = require('web.core');
        var Dialog = require('web.Dialog');
        var _t = core._t;
        var WebCalendarController = require('web.CalendarController');
    
        WebCalendarController.include({
            _onOpenCreate: function (event) {
                if(event.target.model==='tico_hospital.appointment' && [0, 6].includes(event.data.start.day())) {
                    Dialog.alert(this, _t("You cannot create an appointment for Sunday or Saturday!"));
                    return;
                }
                return this._super(event);
            },
        });
    });  
    

    Edit:

    To add the files in an asset bundle, you need to:

    • add an assets.xml file in the views/ folder
    • add the string views/assets.xml in the data key in the manifest file
    • create an inherited view of the desired bundle (here web.assets_backend), and add the file(s) with an XPath expression. For example,

       <template id="assets_backend" name="stack_overflow assets" inherit_id="web.assets_backend">
          <xpath expr="." position="inside">
              <script type="text/javascript" src="/stack_overflow/static/src/js/script.js"></script>
          </xpath>
      </template>
      

    There are many different reasons why a file may not be properly loaded. Here are a few things you can try to solve the issue:

    • once the server is started, it does not know if an asset file has been modified. So, you can simply restart the server to regenerate the assets.
    • check the console (in the dev tools, usually opened with F12) to make sure there are no obvious errors
    • try to add a console.log at the beginning of your file (before any module definition), so you can see if a file has been loaded or not
    • in the user interface, in debug mode (INSERT LINK HERE TO DEBUG MODE), there is an option to force the server to update its assets files.
    • use the debug=assets mode. This will actually bypass the asset bundles (note that it does not actually solve the issue. The server still uses outdated bundles)
    • finally, the most convenient way to do it, for a developer, is to start the server with the –dev=all option. This activates the file watcher options, which will automatically invalidate assets when necessary. Note that it does not work very well if the OS is Windows.
    • remember to refresh your page!
    • or maybe to save your code file…