### 日历
显示记录为日历事件。他们的根元素是 `<calendar>` 他们最常见的属性是:
`color`
用于*颜色分割的字段的名称*。颜色是自动地分布在事件,但在相同的颜色段(有相同的值,为他们的“@色”领域的事件)将得到相同的颜色。
`date_start`
记录的字段为事件开始日期/时间
`date_stop` (optional)
记录的字段为事件的 结束日期/时间 字段(为每个日历事件定义标签)
~~~ xml
<calendar string="Ideas" date_start="invent_date" color="inventor_id">
<field name="name"/>
</calendar>
~~~
练习
日历视图
向用户添加一个日历视图,以便用户查看与开放学院相关的事件。
1. 添加一个` end_date `字段计算` start_date `和`时间`
>Tip
反函数进行现场写,并允许移动会议(通过拖放)在日历视图
2. 向*会话*模型添加日历视图
3. 并将日历视图添加到*会话*模型的操作
*openacademy/models.py*
~~~ python
# -*- coding: utf-8 -*-
from datetime import timedelta
from openerp import models, fields, api, exceptions
class Course(models.Model):
~~~
~~~ python
attendee_ids = fields.Many2many('res.partner', string="Attendees")
taken_seats = fields.Float(string="Taken seats", compute='_taken_seats')
end_date = fields.Date(string="End Date", store=True,
compute='_get_end_date', inverse='_set_end_date')
@api.depends('seats', 'attendee_ids')
def _taken_seats(self):
~~~
~~~ python
},
}
@api.depends('start_date', 'duration')
def _get_end_date(self):
for r in self:
if not (r.start_date and r.duration):
r.end_date = r.start_date
continue
# Add duration to start_date, but: Monday + 5 days = Saturday, so
# subtract one second to get on Friday instead
start = fields.Datetime.from_string(r.start_date)
duration = timedelta(days=r.duration, seconds=-1)
r.end_date = start + duration
def _set_end_date(self):
for r in self:
if not (r.start_date and r.end_date):
continue
# Compute the difference between dates, but: Friday - Monday = 4 days,
# so add one day to get 5 days instead
start_date = fields.Datetime.from_string(r.start_date)
end_date = fields.Datetime.from_string(r.end_date)
r.duration = (end_date - start_date).days + 1
@api.constrains('instructor_id', 'attendee_ids')
def _check_instructor_not_in_attendees(self):
for r in self:
~~~
*openacademy/views/openacademy.xml*
~~~ xml
</field>
</record>
<!-- calendar view -->
<record model="ir.ui.view" id="session_calendar_view">
<field name="name">session.calendar</field>
<field name="model">openacademy.session</field>
<field name="arch" type="xml">
<calendar string="Session Calendar" date_start="start_date"
date_stop="end_date"
color="instructor_id">
<field name="name"/>
</calendar>
</field>
</record>
<record model="ir.actions.act_window" id="session_list_action">
<field name="name">Sessions</field>
<field name="res_model">openacademy.session</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar</field>
</record>
<menuitem id="session_menu" name="Sessions"
~~~