Edit Assessments Code¶
Planning¶
The code is going to react to two events:
button_edit click
button_save click
So, lets go ahead and create those two event handlers.
Open the AssessmentPanel in Design mode:
click on each button
select on click event
Code¶
Testing¶
You should be able to change the values of your assessments. Launch your webapp, and try to change the details. Make sure that you test all values (subject, details, start date and due date).
Final code state¶
By the end of this tutorial your code should be the same as below:
AssessmentPanel¶
1from ._anvil_designer import AssessmentPanelTemplate
2from anvil import *
3import anvil.server
4import anvil.users
5import anvil.tables as tables
6import anvil.tables.query as q
7from anvil.tables import app_tables
8import datetime
9
10
11class AssessmentPanel(AssessmentPanelTemplate):
12 def __init__(self, **properties):
13 # Set Form properties and Data Bindings.
14 self.init_components(**properties)
15
16 # Any code you write here will run before the form opens.
17 self.check_box_completed.checked = self.item['completed']
18 self.label_subject.text = self.item['subject']
19 self.label_details.text = self.item['details']
20 self.label_start.text = self.item['start_date'].strftime('%d/%m/%Y')
21 self.label_due.text = self.item['due_date'].strftime('%d/%m/%Y')
22
23 def check_box_completed_change(self, **event_args):
24 new_value = self.check_box_completed.checked
25 anvil.server.call('update_assessment_completed', self.item.get_id(),new_value)
26
27 def button_edit_click(self, **event_args):
28 self.text_box_subject.text = self.item["subject"]
29 self.text_box_details.text = self.item["details"]
30 self.date_picker_start.date = self.item["start_date"]
31 self.date_picker_due.date = self.item["due_date"]
32 self.switch_components()
33
34 def button_save_click(self, **event_args):
35 # write to server
36 anvil.server.call('update_assessment',
37 self.item.get_id(),
38 self.text_box_subject.text,
39 self.text_box_details.text,
40 self.date_picker_start.date,
41 self.date_picker_due.date,
42 self.check_box_completed.checked)
43
44 # update display
45 self.label_subject.text = self.text_box_subject.text
46 self.label_details.text = self.text_box_details.text
47 self.label_start.text = self.date_picker_start.date.strftime('%d/%m/%Y')
48 self.label_due.text = self.date_picker_due.date.strftime('%d/%m/%Y')
49 self.switch_components()
50
51 def switch_components(self):
52 # display elements
53 self.label_subject.visible = not self.label_subject.visible
54 self.label_details.visible = not self.label_details.visible
55 self.label_start.visible = not self.label_start.visible
56 self.label_due.visible = not self.label_due.visible
57 self.button_edit.visible = not self.button_edit.visible
58
59 # edit elements
60 self.text_box_subject.visible = not self.text_box_subject.visible
61 self.text_box_details.visible = not self.text_box_details.visible
62 self.date_picker_start.visible = not self.date_picker_start.visible
63 self.date_picker_due.visible = not self.date_picker_due.visible
64 self.button_save.visible = not self.button_save.visible
assessment_service¶
1import anvil.users
2import anvil.tables as tables
3import anvil.tables.query as q
4from anvil.tables import app_tables
5import anvil.server
6
7@anvil.server.callable
8def add_assessment(subject, details, start_date, due_date):
9 user = anvil.users.get_user()
10
11 app_tables.assessments.add_row(user= user,
12 subject= subject,
13 details=details,
14 start_date=start_date,
15 due_date=due_date,
16 completed=False)
17
18@anvil.server.callable
19def get_assessment():
20 user = anvil.users.get_user()
21
22 return app_tables.assessments.search(tables.order_by('due_date'),
23 user=user,
24 completed=False)
25
26@anvil.server.callable
27def update_assessment_completed(assessment_id, completed):
28 assessment = app_tables.assessments.get_by_id(assessment_id)
29 if assessment:
30 assessment["completed"] = completed
31
32@anvil.server.callable
33def update_assessment(assessment_id, subject, details, start_date, due_date, completed):
34 assessment = app_tables.assessments.get_by_id(assessment_id)
35 if assessment:
36 assessment['subject'] = subject
37 assessment['details'] = details
38 assessment['start_date'] = start_date
39 assessment['due_date'] = due_date
40 assessment['completed'] = completed