Skip to content

Commit

Permalink
Merge a944e33 into e3e68d8
Browse files Browse the repository at this point in the history
  • Loading branch information
oEduardoAfonso committed May 5, 2021
2 parents e3e68d8 + a944e33 commit 2feeb09
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 13 deletions.
15 changes: 14 additions & 1 deletion app/model/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class Post(db.Model):
id_post = db.Column(db.Integer, nullable=False,
primary_key=True, autoincrement=True)
post_date = db.Column(db.Date, default=date.today(), nullable=False)
rating = db.Column(db.Float, nullable=False)
didactic = db.Column(db.Integer, nullable=False)
metod = db.Column(db.Integer, nullable=False)
avaliations = db.Column(db.Integer, nullable=False)
disponibility = db.Column(db.Integer, nullable=False)
content = db.Column(db.String(480), nullable=False)
is_anonymous = db.Column(db.Boolean, nullable=False)

Expand Down Expand Up @@ -70,3 +73,13 @@ def delete_reports(post_db):
for report in Report.query.filter_by(id_post=post_db.id_post).all():
db.session.delete(report)
db.session.commit()


@property
def rating(self):
sum = 0
sum += self.didactic
sum += self.metod
sum += self.avaliations
sum += self.disponibility
return sum / 4
40 changes: 40 additions & 0 deletions app/model/professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,43 @@ def rating(self):
for post in self.posts:
sum += post.rating
return sum / len(self.posts)

@property
def didactic(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.didactic
return sum / len(self.posts)

@property
def metod(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.metod
return sum / len(self.posts)

@property
def avaliations(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.avaliations
return sum / len(self.posts)

@property
def disponibility(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.disponibility
return sum / len(self.posts)
5 changes: 4 additions & 1 deletion app/schemas/post_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class Meta:
id_professor = fields.Integer(required=True, validate=validate.Range(min=0))
discipline_code = fields.String(required=True, validate=validate.Length(max=80), load_only=True)
content = fields.String(required=True, validate=validate.Length(min=1, max=480))
rating = fields.Float(required=True, validate=validate.Range(min=0, max=10))
didactic = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
metod = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
avaliations = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
disponibility = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
is_anonymous = fields.Boolean(required=True)
post_date = fields.Date()

Expand Down
2 changes: 1 addition & 1 deletion app/schemas/professor_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ProfessorSchema(ma.SQLAlchemySchema):
class Meta:
model = Professor
additional = ['rating']
additional = ['rating', 'didactic', 'metod', 'avaliations', 'disponibility']

id_professor = fields.Integer(validate=validate.Range(min=0))
reg_professor = fields.Integer(
Expand Down
2 changes: 1 addition & 1 deletion app/services/post_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def register_post(post):
return message, status_code

post_db = Post(reg_student=post.get('reg_student'), id_professor=post.get('id_professor'),
discipline_code=post.get('discipline_code'), content=post.get('content'), rating=post.get('rating'),
discipline_code=post.get('discipline_code'), content=post.get('content'), didactic=post.get('didactic'), metod=post.get('metod'), avaliations=post.get('avaliations'), disponibility=post.get('disponibility'),
is_anonymous=post.get('is_anonymous'))
db.session.add(post_db)
db.session.commit()
Expand Down
5 changes: 4 additions & 1 deletion database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ CREATE TABLE IF NOT EXISTS post (
id_professor int UNSIGNED NOT NULL,
content varchar(480) NOT NULL,
post_date date NOT NULL,
rating float NOT NULL,
didactic TINYINT UNSIGNED NOT NULL,
metod TINYINT UNSIGNED NOT NULL,
avaliations TINYINT UNSIGNED NOT NULL,
disponibility TINYINT UNSIGNED NOT NULL,
discipline_code varchar(80) NOT NULL DEFAULT '',
is_anonymous TINYINT(1) NOT NULL,
PRIMARY KEY (id_post),
Expand Down
31 changes: 24 additions & 7 deletions tests/tests_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ def valid_post(self):
"id_professor": self.professor['id_professor'],
"reg_student": self.student['reg_student'],
"discipline_code": self.discipline['discipline_code'],
"content": "Professor nota dez",
"rating": "5",
"content": "Professor nota cinco",
"didactic": "5",
"metod": "5",
"avaliations": "5",
"disponibility": "5",
"is_anonymous": True
}

Expand Down Expand Up @@ -67,30 +70,44 @@ def test_api_must_validate_empty_attributes(self):
post = {}
response = register_post(self, post=post)

expected_json_keys = ['content', 'discipline_code','id_professor', 'is_anonymous', 'rating', 'reg_student']
expected_json_keys = [
'avaliations', 'content', 'didactic', 'discipline_code', 'disponibility', 'id_professor', 'is_anonymous', 'metod', 'reg_student']
json_keys = list(response.json.keys())

self.assertEqual(response.status_code, 400)
self.assertEqual(json_keys, expected_json_keys)

def test_api_must_validate_attributes_min(self):
post = valid_post(self)
post['content'] = ""
post['rating'] = -1
post['didactic'] = 0
post['metod'] = 0
post['avaliations'] = 0
post['disponibility'] = 0
response = register_post(self, post=post)

self.assertEqual(response.status_code, 400)
self.assertIsNotNone(response.json['content'])
self.assertIsNotNone(response.json['rating'])
self.assertIsNotNone(response.json['didactic'])
self.assertIsNotNone(response.json['metod'])
self.assertIsNotNone(response.json['avaliations'])
self.assertIsNotNone(response.json['disponibility'])

def test_api_must_validate_attributes_max(self):
post = valid_post(self)
post['content'] = "a"*481
post['rating'] = 11
post['didactic'] = 6
post['metod'] = 6
post['avaliations'] = 6
post['disponibility'] = 6
response = register_post(self, post=post)

self.assertEqual(response.status_code, 400)
self.assertIsNotNone(response.json['content'])
self.assertIsNotNone(response.json['rating'])
self.assertIsNotNone(response.json['didactic'])
self.assertIsNotNone(response.json['metod'])
self.assertIsNotNone(response.json['avaliations'])
self.assertIsNotNone(response.json['disponibility'])

def test_api_must_validate_professor_relationship_not_found(self):
post = valid_post(self)
Expand Down
3 changes: 2 additions & 1 deletion tests/tests_professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def test_api_must_return_professors_by_name_substring(self):

json_attributes = list(response.json[0].keys())
expected_json_attributes = [
'disciplines', 'id_professor', 'name', 'rating']
'avaliations', 'didactic', 'disciplines', 'disponibility', 'id_professor', 'metod', 'name', 'rating']

self.assertEqual(response.status_code, 200)
self.assertEqual(json_attributes, expected_json_attributes)
self.assertEqual(response.json[0]['name'], self.professor['name'])
Expand Down

0 comments on commit 2feeb09

Please sign in to comment.