Skip to content

Commit 0b1a073

Browse files
author
jfernandez_ccktlbs
committed
[#72] Created resources to work with the options of disciplinary notes items and improve some parts of project.
1 parent a69eef0 commit 0b1a073

25 files changed

+693
-151
lines changed

SMS-Back-End/apigms/api/scms_segment/disciplinary_notes_api_segment.py

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
disciplinary_notes_api = Blueprint('disciplinary_notes_api', __name__)
1919

2020

21+
@disciplinary_notes_api.route('/disciplinarynote/schema', methods=['GET', 'PUT'])
22+
def CRU_disciplinary_note_schema():
23+
"""
24+
It call to SCmS to get a schema with options to ve showed in UI form to do a disciplinary note.
25+
:return: A dict
26+
"""
27+
if request.method == 'GET':
28+
return CRUD.get(service='scms', resource='disciplinarynote/schema')
29+
30+
if request.method == 'PUT':
31+
return CRUD.put(service='scms', resource='disciplinarynote/schema', id=None, json=request.get_json())
32+
33+
2134
@disciplinary_notes_api.route('/disciplinarynote', methods=['GET'])
2235
@disciplinary_notes_api.route('/disciplinarynote/<int:dn_id>', methods=['GET'])
2336
def get_disciplinary_note(dn_id=None):

SMS-Back-End/apigms/api/services.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@ def put(cls, service, resource, id, json):
6161
6262
:param service: The name of the service (used for google services auto discovering).
6363
:param resource: The url segment of the service.
64-
:param id: The id of the request item.
64+
:param id: The id of the request item. **CAN BE NONE TO SINGLETON RESOURCES**
6565
:param json: The payload where are the data to put in a dict format.
6666
:return: Exactly the same response of the service.
6767
"""
68-
response = requests.put(
69-
url='http://{}/{}/{}'.format(modules.get_hostname(module=service),resource, id),
70-
json=json)
68+
69+
# For singleton resources.
70+
url = 'http://{}/{}'.format(modules.get_hostname(module=service), resource)
71+
72+
if id:
73+
url += '/{}'.format(id)
74+
75+
response = requests.put(url=url, json=json)
7176
response.headers['Access-Control-Allow-Origin'] = "*"
7277
return make_response(response.content, response.status_code)
7378

SMS-Back-End/scms/api/attendance_controls_api_segment.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@
5858
5959
Example:
6060
61-
>>> curl -H "Content-Type: application/json" -X POST -d '...' localhost:8003/association
61+
>>> curl -H "Content-Type: application/json" -X POST -d '...' localhost:8003/ac
6262
6363
Post with example file:
6464
6565
>>> curl -i -H "Content-Type: application/json" -X POST -d @SMS-Back-End/scms/test/AC_example_1.json localhost:8003/ac
6666
{"acId": 6333186975989760}j
6767
6868
An example of the data is:
69-
69+
7070
.. code-block:: json
7171
7272
{

SMS-Back-End/scms/api/disciplinary_notes_api_segment.py

+58
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,57 @@
6565
"description": "A little problem with new boy."
6666
}
6767
68+
69+
/disciplinarynote/schema
70+
71+
**GET**
72+
73+
Return the schema with data necesary to buil the form in the UI to make a disciplinary note.
74+
75+
Example:
76+
77+
>>> curl -i -X GET localhost:8003/disciplinarynote/schema
78+
79+
{
80+
"gravities": [
81+
{
82+
"code": 1,
83+
"meaning": 1
84+
},
85+
{
86+
"code": 2,
87+
"meaning": 2
88+
},
89+
{
90+
"code": 3,
91+
"meaning": 3
92+
},
93+
{
94+
"code": 4,
95+
"meaning": 4
96+
},
97+
{
98+
"code": 5,
99+
"meaning": 5
100+
}
101+
],
102+
"kinds": [
103+
{
104+
"code": 1,
105+
"meaning": "Acoso"
106+
},
107+
{
108+
"code": 2,
109+
"meaning": "Falta de respeto"
110+
},
111+
{
112+
"code": 2,
113+
"meaning": "Violencia de genero"
114+
}
115+
]
116+
}
117+
118+
68119
/disciplinarynote/{id}
69120
--------
70121
@@ -146,6 +197,13 @@
146197
disciplinary_notes_api = Blueprint('disciplinary_notes_api', __name__)
147198

148199

200+
@disciplinary_notes_api.route('/disciplinarynote/schema', methods=['GET', 'PUT'])
201+
def get_disciplinary_note_schema():
202+
203+
return process_response(DisciplinaryNotesManager.cru_dn_schema(schema=request.get_json(),
204+
action=request.method))
205+
206+
149207
@disciplinary_notes_api.route('/disciplinarynote', methods=['POST'])
150208
def post_disciplinary_note():
151209
"""

SMS-Back-End/scms/scm/models/discipline_models.py

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
# the api manage and the conversion to json is directly using the same name used here.
1515

1616

17+
class OptionItem(ndb.Model):
18+
id = ndb.IntegerProperty()
19+
meaning = ndb.StringProperty()
20+
21+
22+
# This is a singleton MODEL
23+
class DNOptions(ndb.Model):
24+
kinds = ndb.StructuredProperty(OptionItem, repeated=True)
25+
gravities = ndb.StructuredProperty(OptionItem, repeated=True)
26+
modifiedBy = ndb.IntegerProperty()
27+
modifiedAt = ndb.DateTimeProperty()
28+
29+
1730
class DisciplinaryNote(ndb.Model):
1831
"""
1932
Model of a disciplinary note. We save only the id of students and teacher. One disciplinary note

SMS-Back-End/scms/scm/scm.py

+43-11
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ def get_item_from_tdbms(kind, id, params):
5959
'time': '{} sec'.format((time_after-time_before).total_seconds())
6060
}
6161

62-
print colored(info, 'red', 'on_grey')
63-
64-
6562
status = response.status_code
6663

6764
if status == 200:
@@ -194,7 +191,7 @@ def get_ac(cls, ac_id=None):
194191
student_info = get_item_from_tdbms('student',
195192
student['studentId'], ['name', 'surname', 'profileImageUrl'])
196193
del (student['studentId'])
197-
student['student'] = student_info
194+
student.update(student_info)
198195

199196
populated_students.append(student)
200197

@@ -487,8 +484,6 @@ def post_mark(cls, mark):
487484
# If mark has the required format:
488485
if cls.validate_mark(mark):
489486

490-
print colored('Posted mark')
491-
print colored(mark, 'red')
492487

493488
# Is created the object
494489
enrollment = mark['enrollment']
@@ -531,8 +526,6 @@ def update_mark(cls, mark_id, received_mark):
531526

532527
if cls.validate_mark(received_mark):
533528

534-
print colored('Posted mark')
535-
print colored(received_mark, 'red')
536529

537530
# Is created the object
538531
enrollment = received_mark['enrollment']
@@ -595,8 +588,8 @@ def validate_dn(cls, disciplinary_note):
595588
"""
596589
Validate the format of item based of data store model.
597590
598-
:param disciplinary_note: Item dict.
599-
:return: True if format is ok and false in other hand.
591+
:param disciplinary_note: data block, dict.
592+
:return: True if format is ok and false in other hand, boolean.
600593
"""
601594
# TODO: Implement.
602595
return True
@@ -768,4 +761,43 @@ def delete_dn(cls, disciplinary_note):
768761
return {'status': 200, 'data': None, 'log': None}
769762

770763
else: # If it doesn't exists:
771-
return {'status': 404, 'data': None, 'log': 'Disciplinary Note required seem like doesn\'t exists or was deleted.'}
764+
return {'status': 404, 'data': None, 'log': 'Disciplinary Note required seem like doesn\'t exists or was deleted.'}
765+
766+
@classmethod
767+
def cru_dn_schema(cls, schema, action):
768+
"""
769+
Do all functionality about schemas (all together because is very simple).
770+
:param schema: Dict, data block to work with it.
771+
:param action: String, action (HTTP verb) to realise with the singleton shema
772+
in the database.
773+
:return: Standard info dict (without data, only status code)
774+
"""
775+
776+
if action == 'GET' and not schema:
777+
dn_options_saved = DNOptions.get_by_id('dn_options_saved')
778+
if not dn_options_saved:
779+
DNOptions.get_or_insert('dn_options_saved',
780+
kinds=[OptionItem(id=1, meaning='Tipo base')],
781+
gravities=[OptionItem(id=1, meaning='Gravedad base')])
782+
dn_options_saved = DNOptions.get_by_id('dn_options_saved')
783+
return {'status': 200, 'data': dn_options_saved.to_dict(), 'log': None }
784+
785+
if action == 'PUT' and schema:
786+
787+
kinds = []
788+
gravities = []
789+
for kind in schema['kinds']:
790+
kinds.append(OptionItem(id=kind['id'], meaning=kind['meaning']))
791+
792+
for gravity in schema['gravities']:
793+
gravities.append(OptionItem(id=gravity['id'], meaning=gravity['meaning']))
794+
795+
dn_options_saved = DNOptions.get_or_insert('dn_options_saved')
796+
797+
dn_options_saved.kinds = kinds
798+
dn_options_saved.gravities = gravities
799+
dn_options_saved.modifiedAt = time_now()
800+
801+
dn_options_saved.put()
802+
803+
return {'status': 200, 'data': dn_options_saved.to_dict(), 'log': None}

SMS-Back-End/scms/test/disciplinary_note_example_1.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"dateTime": "2000-12-03 10:30",
77
"kind": 1,
88
"gravity": 5,
9-
"description": "A little problem with new boy."
9+
"description": "A little problem with new boy. María"
1010
}

SMS-Back-End/scms/test/scms_api_rest_disciplinary_notes_segment_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,42 @@ def test_update(self, port):
9696

9797
sleep(0.5)
9898

99+
100+
class TestClassOptions(object):
101+
102+
scenarios = [scenario2, scenario1]
103+
104+
def test_get_initial_options(self, port):
105+
"""
106+
$ pytest scms_api_rest_disciplinary_notes_segment_test.py::TestClassOptions::test_get_options -vv -s
107+
"""
108+
109+
url = 'http://localhost:{}/disciplinarynote/schema'.format(port)
110+
result = requests.get(url)
111+
assert result.status_code == 200
112+
113+
def test_update_options(self, port):
114+
"""
115+
$ pytest scms_api_rest_disciplinary_notes_segment_test.py::TestClassOptions::test_get_options -vv -s
116+
"""
117+
118+
url = 'http://localhost:{}/disciplinarynote/schema'.format(port)
119+
120+
data = {
121+
'kinds': [
122+
{'id': 1, 'meaning': 'Tipo 1'},
123+
{'id': 2, 'meaning': 'Tipo 2'},
124+
{'id': 3, 'meaning': 'Tipo 3'}
125+
],
126+
'gravities': [
127+
{'id': 1, 'meaning': 'Gravedad 1'},
128+
{'id': 2, 'meaning': 'Gravedad 2'},
129+
{'id': 3, 'meaning': 'Gravedad 3'},
130+
]
131+
}
132+
response = requests.put(url=url, json=data)
133+
assert response.status_code == 200
134+
99135
"""
100136
101137

SMS-Back-End/tdbms/dbapi/entities_manager.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
import datetime
3+
from datetime import datetime
44
import pytz
55

66
import db_params
@@ -167,13 +167,22 @@ def post(cls, kind, data):
167167
if v:
168168
print colored(locals(), 'blue')
169169

170+
170171
return_dic = {}
171172

172-
now = datetime.datetime.utcnow()
173+
# Datetime adjusts
174+
175+
# Adjusting the right format to the datetime which data is saved.
176+
now = datetime.utcnow()
173177
tz = pytz.timezone('Europe/Madrid')
174178
tzoffset = tz.utcoffset(now)
175179
mynow = now + tzoffset
176180

181+
# Adjusting the right format to the data object that is received form request.
182+
if data.get('birthdate', None):
183+
data['birthdate'] = datetime.strptime(data['birthdate'], '%Y-%m-%dT%H:%M:%S.%fZ')
184+
print colored(type(data['birthdate']), 'blue')
185+
177186
control_fields = {'createdBy': 1, 'createdAt': mynow, 'deleted': 0}
178187

179188
query = 'insert into {0} ({0}Id, '.format(kind)

SMS-Back-End/tdbms/tdbms_api.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ def process_response(response):
6161
:return:
6262
"""
6363

64-
print 'In process response'
65-
print response
66-
6764
"""
6865
Function to build a response with sense.
6966
:param self:
@@ -207,18 +204,11 @@ def post_entity(kind):
207204
abort(400) # Bad request.
208205

209206
else:
210-
for key, value in received_json.iteritems():
211-
if type(value) is not int and type(value) is not list: # Because we accept a list of associationIds in enrollment resource.
212-
received_json[key] = value.encode('utf-8')
213-
214-
# When are really saved data in database
215-
216-
217-
# Special multiple enrollment checking:
218-
if 'associationsIds' in received_json:
219-
return process_response(EntitiesManager.multiple_enrollment(kind, received_json))
220-
else:
221-
return process_response(EntitiesManager.post(kind, received_json))
207+
# Special multiple enrollment checking:
208+
if 'associationsIds' in received_json:
209+
return process_response(EntitiesManager.multiple_enrollment(kind, received_json))
210+
else:
211+
return process_response(EntitiesManager.post(kind, received_json))
222212

223213

224214
@app.route('/entities/<string:kind>', methods=['GET']) # Si pedimos todas las entidades de un tipo

0 commit comments

Comments
 (0)