sharing profile (need to update after select department)

conv_bal
watcha.h 2015-03-11 23:41:46 +07:00
parent c2a1e047f4
commit e28bec6514
4 changed files with 118 additions and 6 deletions

View File

@ -1,4 +1,9 @@
<form model="clinic.branch" show_company="1"> <form model="clinic.branch" show_company="1">
<head>
<button string="Options" dropdown="1">
<item string='Share Access' method="share_access" confirm="Are you sure?"/>
</button>
</head>
<field name="name"/> <field name="name"/>
<field name="code"/> <field name="code"/>
<field name="parent_id"/> <field name="parent_id"/>

View File

@ -35,8 +35,8 @@
</group> </group>
</tab> </tab>
<tab string="Working Location"> <tab string="Working Location">
<field name="branch_id" required="1"/> <!--<field name="branch_id" required="1"/>-->
<field name="department_id" domain='[["branch_id","=",branch_id]]' required="1"/> <!--<field name="department_id" domain='[["branch_id","=",branch_id]]' required="1"/>-->
<field name="departments"/> <field name="departments"/>
</tab> </tab>
<tab string="Professional Information"> <tab string="Professional Information">

View File

@ -1,4 +1,4 @@
from netforce.model import Model, fields from netforce.model import Model, fields, get_model
from netforce.access import get_active_company from netforce.access import get_active_company
class Branch(Model): class Branch(Model):
@ -19,4 +19,56 @@ class Branch(Model):
'active': True, 'active': True,
} }
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=[]
for otp in get_model('permission').search_read(['name','ilike','clinic'],['name']):
name=otp['name']
if name in except_perms:
continue
other_perms.append(otp['id'])
except_model=['clinic.cycle','clinic.department']
perms=[]
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))
get_model('profile').create({
'perms': perms,
'code': code,
'name': code,
'perms': perms,
'other_perms': [('set',other_perms)],
'login_company_id': get_active_company(),
})
print("create profile %s"%(code))
def share_access(self,ids,context={}):
obj=self.browse(ids)[0]
obj.create_profile()
return {
'next': {
'name': 'clinic_branch',
'active_id': obj.id,
'mode': 'form',
},
'flash': 'Branch %s is shared sucessfully'%obj.code,
}
Branch.register() Branch.register()

View File

@ -45,19 +45,74 @@ class Department(Model):
if name in except_perms: if name in except_perms:
continue continue
other_perms.append(otp['id']) other_perms.append(otp['id'])
except_model=['clinic.cycle','clinic.department']
perms=[]
for model in get_model('model').search_read([['name','ilike','clinic']],['name']): for model in get_model('model').search_read([['name','ilike','clinic']],['name']):
pass vals={
get_model('profile').create({ '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({
'perms': perms, 'perms': perms,
'code': code, 'code': code,
'name': code, 'name': code,
'perms': perms,
'other_perms': [('set',other_perms)], 'other_perms': [('set',other_perms)],
'login_company_id': get_active_company(),
}) })
print("create profile %s"%(code)) 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!")
def share_access(self,ids,context={}): def share_access(self,ids,context={}):
obj=self.browse(ids)[0] obj=self.browse(ids)[0]
obj.create_profile() context['profile_id']=obj.create_profile()
obj.create_sharing(context=context)
# create sharing setting
return { return {
'next': { 'next': {
'name': 'clinic_department', 'name': 'clinic_department',