My first python web development framework (37) - job management functions

Keywords: Python JSON

For position management, we can understand it as the management of role permissions. As mentioned before, with position management, the background management system binds the corresponding permissions, new employees, resignation or post adjustment, the administrator can operate very conveniently, just need to rebind the corresponding position to do a good job of switching permissions.

To facilitate job management, we can design the page as follows

Display the list of parts on the left, click on one of the items, and display the positions of the corresponding departments on the right, then add, change, delete the positions, etc.

On the left, we can directly use the Department list interface in the previous chapter, and then modify the jqGrid front-end component to display only a list of names.

Then, in the jqGrid component of the front-end code, add the onSelectRow method and perform the right-hand list query and refresh when clicking on the list item.

On the right side, we need to add a job list query interface. When querying, we need to submit the Department id as the query condition. The specific interface code is as follows:

 1 @get('/system/positions/')
 2 def callback():
 3     """
 4     Get list data
 5     """
 6     # department id
 7     department_id = convert_helper.to_int0(web_helper.get_query('department_id', 'department id'))
 8     # Page index
 9     page_number = convert_helper.to_int1(web_helper.get_query('page', '', is_check_null=False))
10     # Page Number and Number of Display Records
11     page_size = convert_helper.to_int0(web_helper.get_query('rows', '', is_check_null=False))
12     sidx = web_helper.get_query('sidx', '', is_check_null=False)
13     sord = web_helper.get_query('sord', '', is_check_null=False)
14     # Initialize sort field
15     order_by = 'id asc'
16     if sidx:
17         order_by = sidx + ' ' + sord
18 
19     _positions_logic = positions_logic.PositionsLogic()
20     # Read record
21     wheres = ''
22     if department_id:
23         wheres = 'department_id=' + str(department_id)
24     result = _positions_logic.get_list('*', wheres, page_number, page_size, order_by)
25     if result:
26         # Direct output json
27         return json.dumps(result)
28     else:
29         return web_helper.return_msg(-1, "Query failed")

Does the code look familiar, similar to the code for front-end list queries, except that it receives parameters, query conditions, and the class invoked is different? So we only need to be familiar with tool functions, underlying ORM methods and invocation methods. It is very convenient to write the code, and the readability of the code is greatly enhanced.

 

Next, we will deal with the operation of new posts. When we add new posts, the only content we need to fill in is the title of the post, because the codes of the departments and departments can be brought directly to the page. Another operation is to set access rights, which is also the most important part of our whole authority management.

As can be seen from the figure above, setting access rights is a tree list. It uses ztree control. According to the format it requires, it can automatically generate such a tree list by passing the list value to ztree control. Then the front-end code obtains the menu item id corresponding to all the check options through the onCheck method of ztree control, combines the right string to submit to the interface, and updates to the number. According to the table, this position has all the permissions we have chosen.

For the display of access tree list, firstly, it needs to read menu list and output the format required by ztree control; secondly, when we edit position permissions, we need to automatically tick the items that have permission, which we can also output the corresponding parameters directly in the interface to set.

So in the menu_info.py file of the previous chapter, we can add the following interface to process. We can get the privileges of the position by submitting the post id. Then we can judge whether the tick is ticked by judging the relationship between the menu item and the privilege item by item.

 1 @get('/api/system/menu_info/positions/<id:int>/')
 2 def callback(id):
 3     """
 4     Get list data (tree list) according to user position privileges and assign values to data with existing privileges
 5     """
 6     _menu_info_logic = menu_info_logic.MenuInfoLogic()
 7     # Read records( ztree Control needs to output records id,father id,The name of the tree node and whether the node expands these parameters)
 8     result = _menu_info_logic.get_list('id, parent_id, name, not is_leaf as open, false as checked')
 9     if result and result.get('rows'):
10         # Obtain appointed job records
11         _positions_logic = positions_logic.PositionsLogic()
12         positions_logic_model = _positions_logic.get_model_for_cache(id)
13         if positions_logic_model:
14             # Read the post permission string
15             page_power = positions_logic_model.get('page_power', '')
16             # Judge the current menu item id Does it exist in the permission string of the position?
17             for model in result.get('rows'):
18                 # If it exists, it means that the current position has the privileges of the menu item, i.e. it needs to be ticked in the menu privilege list.
19                 if ',' + str(model.get('id', 0)) + ',' in page_power:
20                     model['checked'] = True
21 
22         return web_helper.return_msg(0, "Success", {'tree_list': result.get('rows')})
23     else:
24         return web_helper.return_msg(-1, "Query failed")

Let's see how the new job record interface code is implemented after clicking save.

 1 @post('/system/positions/')
 2 def callback():
 3     """
 4     adding record
 5     """
 6     name = web_helper.get_form('name', 'Role name')
 7     department_id = convert_helper.to_int0(web_helper.get_form('department_id', 'department id'))
 8     page_power = web_helper.get_form('page_power', 'Permission list', is_check_null=False)
 9 
10     _department_logic = department_logic.DepartmentLogic()
11     # Read the corresponding department records
12     department_result = _department_logic.get_model_for_cache(department_id)
13     if not department_result:
14         return web_helper.return_msg(-1, "Departments do not exist")
15 
16     _positions_logic = positions_logic.PositionsLogic()
17     # Combination update field
18     fields = {
19         'name': string(name),
20         'department_id': department_id,
21         'department_code': string(department_result.get('code', '')),
22         'department_name': string(department_result.get('name', '')),
23         'page_power': string(page_power),
24     }
25     # Read record
26     result = _positions_logic.add_model(fields)
27     if result:
28         # Direct output json
29         return web_helper.return_msg(0, 'Submit successfully')
30     else:
31         return web_helper.return_msg(-1, "Failure to submit")

This code is similar to the previous additions. First, it receives the submitted parameters, then combines and updates the dictionary, and then calls the submitted data table.

 

Modifying post permission functions are similar to those above, but an interface for obtaining post record entities needs to be added to the interface.

 1 @get('/system/positions/<id:int>/')
 2 def callback(id):
 3     """
 4     Gets the specified record
 5     """
 6     _positions_logic = positions_logic.PositionsLogic()
 7     # Read record
 8     result = _positions_logic.get_model_for_cache(id)
 9     if result:
10         # Direct output json
11         return web_helper.return_msg(0, 'Success', result)
12     else:
13         return web_helper.return_msg(-1, "Query failed")
14 
15 
16 @put('/system/positions/<id:int>/')
17 def callback(id):
18     """
19     Modification record
20     """
21     name = web_helper.get_form('name', 'Role name')
22     department_id = convert_helper.to_int0(web_helper.get_form('department_id', 'department id'))
23     page_power = web_helper.get_form('page_power', 'Permission list', is_check_null=False)
24     if page_power == ',':
25         page_power = ''
26 
27     _positions_logic = positions_logic.PositionsLogic()
28     positions_result = _positions_logic.get_model_for_cache(id)
29     if department_id != positions_result.get('department_id'):
30         return web_helper.return_msg(-1, 'This role belongs to the wrong department, please contact the administrator.')
31 
32     # Combination update field
33     fields = {
34         'name': string(name),
35         'page_power': string(page_power),
36     }
37 
38     # Read record
39     result = _positions_logic.edit_model(id, fields)
40     if result:
41         # Direct output json
42         return web_helper.return_msg(0, 'Submit successfully', result)
43     else:
44         return web_helper.return_msg(-1, "Failure to submit")

 

The last is the deletion interface. When we perform the deletion operation, we must determine whether it has bound the administrator's account. Otherwise, the administrator will not have any privileges to do any operation after logging into the background system.

 1 @delete('/system/positions/<id:int>/')
 2 def callback(id):
 3     """
 4     Delete the specified record
 5     """
 6     # Judging whether the record to be deleted is referenced, yes, it cannot be deleted
 7     _manager_logic = manager_logic.ManagerLogic()
 8     if _manager_logic.exists('positions_id=' + str(id)):
 9         return web_helper.return_msg(-1, "The current position is bound to the relevant administrator and cannot be deleted directly.")
10 
11     # Delete record
12     _positions_logic = positions_logic.PositionsLogic()
13     result = _positions_logic.delete_model(id)
14     if result:
15         # Direct output json
16         return web_helper.return_msg(0, 'Delete successful')
17     else:
18         return web_helper.return_msg(-1, "Delete failed")

 

 

  The corresponding source code download 

 

Copyright Statement: This article was originally published in ___________. Blog Garden The author is AllEmpty Reproduction is welcomed in this article, but the statement must be retained without the author's consent, and the link to the original text should be given clearly on the page of the article, otherwise it will be regarded as infringement.

python develops QQ group: 669058475 Author's blog: http://www.cnblogs.com/EmptyFS/

Posted by jaquito13 on Fri, 10 May 2019 13:47:43 -0700