clinic/netforce_clinic/models/department.py

126 lines
5.0 KiB
Python
Raw Normal View History

2015-01-14 13:36:23 +00:00
from netforce.model import Model, fields, get_model
2014-10-26 04:28:08 +00:00
from netforce.access import get_active_company
2014-10-01 08:48:58 +00:00
class Department(Model):
_name="clinic.department"
_string="Department"
2014-11-29 14:56:15 +00:00
_key=['code']
2014-10-01 08:48:58 +00:00
_fields={
"name": fields.Char("Name",required=True,search=True),
2015-03-11 14:16:22 +00:00
"code": fields.Char("Code",required=True,search=True),
2014-10-01 08:48:58 +00:00
"parent_id": fields.Many2One("clinic.department", "Parent"),
2015-01-11 06:09:40 +00:00
'company_id': fields.Many2One("company","Company"),
2015-01-14 13:36:23 +00:00
'branch_id': fields.Many2One("clinic.branch","Branch"),
2015-01-13 08:58:47 +00:00
"pick_out_journal_id": fields.Many2One("stock.journal","Stock Journal",required=True),
2015-01-15 00:59:27 +00:00
'patients': fields.One2Many("clinic.patient","department_id","Patients"),
2015-01-18 10:31:54 +00:00
'staffs': fields.One2Many("clinic.staff","department_id","Staffs"),
2015-01-19 09:00:01 +00:00
'active': fields.Boolean("Active"),
2014-10-01 08:48:58 +00:00
}
2015-01-14 13:36:23 +00:00
def _get_branch(self,context={}):
b_ids=get_model('clinic.branch').search([])
if b_ids:
return b_ids[0]
2014-10-01 08:48:58 +00:00
2014-10-26 04:28:08 +00:00
_defaults={
'company_id': lambda *a: get_active_company(),
2015-01-14 13:36:23 +00:00
'branch_id': _get_branch,
2015-01-19 09:00:01 +00:00
'active': True,
2014-10-26 04:28:08 +00:00
}
2015-02-18 01:31:12 +00:00
_order="code"
2015-03-11 01:44:02 +00:00
2015-03-11 14:16:22 +00:00
def create_profile(self,ids,context={}):
for obj in self.browse(ids):
code=obj.code or ''
pf_ids=get_model('profile').search(['code','=',code])
if pf_ids:
raise Exception("Profile %s is already exist"%(code))
return
except_perms=['Clinic Menu Settings','Clinic Staff Tab Accounting']
other_perms=[]
2015-03-11 14:32:16 +00:00
for otp in get_model('permission').search_read(['name','ilike','clinic'],['name']):
2015-03-11 14:16:22 +00:00
name=otp['name']
if name in except_perms:
continue
other_perms.append(otp['id'])
except_model=['clinic.cycle','clinic.department']
perms=[]
2015-03-11 14:32:16 +00:00
for model in get_model('model').search_read([['name','ilike','clinic']],['name']):
vals={
'model_id': model['id'],
'perm_read': True,
'perm_create': True,
'perm_write': True,
'perm_delete': True,
}
name=model['name']
if name in except_model:
vals['perm_create']=False
vals['perm_write']=False
vals['perm_delete']=False
perms.append(('create', vals))
profile_id=get_model('profile').create({
2015-03-11 14:32:16 +00:00
'perms': perms,
2015-03-11 14:16:22 +00:00
'code': code,
'name': code,
'perms': perms,
2015-03-11 14:16:22 +00:00
'other_perms': [('set',other_perms)],
'login_company_id': get_active_company(),
2015-03-11 14:16:22 +00:00
})
print("create profile %s"%(code))
return profile_id
def create_sharing(self,ids,context={}):
#rule: branch_code-deparment_code
profile_id=context.get('profile_id')
if not profile_id:
raise Exception("Profile not found")
models=[
['clinic.patient','department_id.code','='],
['clinic.patient.cycle','department_id.code','='],
['clinic.staff','location','ilike'],
['clinic.staff.rotation','staff_id.department_id.code','='],
['clinic.visit','department_id.code','='],
['clinic.hd.case','department_id.code','='],
['clinic.cycle.item','department_id.code','='],
['clinic.sickbed','department_id.code','='],
['clinic.shop','department_id.code','='],
#['clinic.department','code','='],
['clinic.dialyzer','department_id.code','='],
['clinic.schedule','department_id.code','='],
]
for obj in self.browse(ids):
for sa in get_model('share.access').search_browse([]):
if obj.code in sa.domain or not sa.profiles:
print("profile %s is deleted"%(obj.code))
sa.delete()
for model,field_dom, op in models:
for model_id in get_model("model").search(['name','=',model]):
get_model('share.access').create({
'model_id': model_id,
'profiles': [['set',[profile_id]]], #need to include another profile ex: profile of branch
'default_access': 'custom',
'filter_type': 'rw',
'select_profile': 'include',
'domain': '[["%s","%s","%s"]]'%(field_dom,op,obj.code),
})
print("Done!")
2015-03-11 14:16:22 +00:00
2015-03-11 01:44:02 +00:00
def share_access(self,ids,context={}):
obj=self.browse(ids)[0]
context['profile_id']=obj.create_profile()
obj.create_sharing(context=context)
# create sharing setting
2015-03-11 01:44:02 +00:00
return {
'next': {
2015-03-11 14:16:22 +00:00
'name': 'clinic_department',
2015-03-11 01:44:02 +00:00
'active_id': obj.id,
'mode': 'form',
},
2015-03-11 14:32:16 +00:00
'flash': 'Department %s is shared sucessfully'%obj.code,
2015-03-11 01:44:02 +00:00
}
2015-01-18 10:31:54 +00:00
2014-10-01 08:48:58 +00:00
Department.register()