3
\$\begingroup\$

I am developing an API and I have some questions about variables from another class. Here is an example (reduced code to be more assertive):

File user.py

from ..db_func import *
from flaks import jsonify

class User():
    def __init__ (self, P_id_user = None, P_login = None, P_nome = None):
        self.id_user = P_id_user
        self.login = P_login
        self.nome = P_nome

    @property
    def id_user(self):
        return self._id_user
    
    @id_user.setter
    def id_user(self, value):
        self._id_user = value 

    @property
    def login(self):
        return self._login
    
    @login.setter
    def login(self, value):
        self._login = value 

    @property
    def nome(self):
        return self._nome
    
    @nome.setter
    def nome(self, value):
        self._nome = value 

    #user functions here

File campaign.py

from ..db_func import *
from .vote import *
from ..uac.classes.groups import *

class Campaign():
    def __init__ (self, P_id_campaign = None, P_name_campaign = None, P_description = None):
        self.id_campaign = P_id_campaign
        self.name_campaign = P_name_campaign
        self.description = P_description

    @property
    def id_campaign(self):
        return self._id_campaign
    
    @id_campaign.setter
    def id_campaign(self, value):
        self._id_campaign = value 

    @property
    def name_campaign(self):
        return self._name_campaign
    
    @name_campaign.setter
    def name_campaign(self, value):
        self._name_campaign = value 

    @property
    def description(self):
        return self._description
    
    @description.setter
    def description(self, value):
        self._description = value 

    #campaign functions here

File vote.py

from .campaign import *
from ..db_func import *

class Vote(Campaign):
    def __init__ (self, P_id_vote = None, P_description = None):
        self.id_vote= P_id_vote
        self.description = P_description

    @property
    def id_vote(self):
        return self._id_vote
    
    @id_vote.setter
    def id_vote(self, value):
        self._id_vote = value  

    @property
    def description(self):
        return self._description
    
    @description.setter
    def description(self, value):
        self._description = value 

    def insert_vote(self, id_campaign, vote): #vote is a JSON
        self.id_campaign = id_campaign
        self.description = vote['description_vote']
        id_user = vote['user']

        self.id_vote = db().execute_query('insert into tb_vote (id_campaign, desc, id_user) values (%s, %s, %s)'), [self.id_campaign, self.description, id_user]
        return jsonify({'mensagem' : 'Success!', 'data' : 'Vote computed!', 'id_vote' : self.id_vote}), 201

    # other vote functions here

Routes.py

from .classes.campaign import *
from .classes.vote import *
from ...uac.classes.user import *
from flask_jwt_extended import jwt_required
from . import cadastro
from flask import request

@cadastro.get('/campaign')
@jwt_required()
def get_campaigns(subdomain):
    return Campaign().get_campaigns()
#example of route

@cadastro.post('/campaign/<id_campaign>/votes')
@jwt_required()
def create_campaign_vote(id_campaign):
    json_vote = request.get_json()
    if not json_vote:
        return jsonify({'mensagem' : 'Error!', 'data' : 'Invalid payload!'}), 400
    return Vote().insert_vote(id_campaign, json_vote)
# The route of the question

I have three classes: User, Campaign and Vote. With these three classes, I call an endpoint(/api/campaigns/<id_campaign>/votes) to insert the user's vote in a campaign; the id_user is sent in the application generated JSON.

Question: Is using id_user = vote['user'] (without self) in the insert_vote function a good practice in OOP? Do I need to extend User or simply declare the self.id_user in Vote class?

I am learning OOP and declarations and references of this type are new to me. I didn't found any example like this to use as reference or help.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

I would do neither but rather go with an MVC architecture if you are using Flask.

Here's an example I wrote a while back that's dated but has the pattern: https://github.com/cdennison/flask-rest-example/blob/master/flask_rest_api/problems/views.py

Or Google MVC architecture and get something like this: https://plainenglish.io/blog/flask-crud-application-using-mvc-architecture

\$\endgroup\$
2
  • 1
    \$\begingroup\$ It might be a good idea to explain why a Model View Controller design pattern would help, since the user is a beginner \$\endgroup\$
    – pacmaninbw
    Jan 3, 2023 at 21:54
  • \$\begingroup\$ MVC architecture will solve my problem since SQLAlchemy will handle all classes and functions. I'll test it, if solved, I'll accept as answer. \$\endgroup\$
    – Arcyde
    Jan 4, 2023 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.