I have a database named Account and I want to list the Account records of the current user in a select list in this database. But this list must be up to date and list all Accounts currently available. But the code I wrote does not work that way. For example, I add an Account, but I have to close and reopen the script for it to appear up to date. if I don’t do this the accounts are never listed. How can I solve this problem?
I want to show error/warning or success message on the form depending on the situation. What code can I do this with? I tried form.errors[‘general’] but I was not successful.
I would be very glad if you could help me, thank you.
class JobView(MyModelView):
column_list = ('id', 'user_id','account_name', 'api_key', 'keyword_list', 'instructions', 'language', 'product', 'search_tweet_limit', 'num_tweets', 'sleep_time')
form_columns = ['account_name', 'api_key', 'keyword_list', 'instructions', 'language', 'product', 'search_tweet_limit', 'num_tweets', 'sleep_time']
form_choices = {
'account_name': [], # Boş bırakıyoruz, dinamik olarak dolduracağız
'language': [], # Boş bırakıyoruz, dinamik olarak dolduracağız
'product': [('Latest', 'Latest'), ('Top', 'Top')]
}
def on_model_change(self, form, model, is_created):
if is_created:
model.user_id = current_user.id
super(JobView, self).on_model_change(form, model, is_created)
def is_accessible(self):
return current_user.is_authenticated
with app.app_context():
accounts = Account.query.all()
for account in accounts:
form_choices['account_name'].append((account.auth_info_1, account.auth_info_1))
languages = ['english', 'turkish', 'french', 'german', 'spanish', 'italian', 'portuguese', 'dutch', 'russian', 'japanese', 'chinese', 'arabic', 'korean']
for lang in languages:
form_choices['language'].append((lang, lang))
def create_form(self, obj=None):
form = super(JobView, self).create_form(obj)
# Populate account_name choices dynamically
form.account_name.choices = [(account.auth_info_1, account.auth_info_1) for account in Account.query.all()]
form.keyword_list.description = "Each keyword should be separated by a comma."
form.instructions = TextAreaField('Instructions', validators=[DataRequired()])
form.language.choices = [(lang, lang) for lang in nltk.corpus.stopwords.fileids()]
form.product = SelectField('Tweet Type', choices=[('Latest', 'Latest'), ('Top', 'Top')], widget=Select2Widget(), validators=[DataRequired()])
form.search_tweet_limit = IntegerField('Search Tweet Limit', validators=[NumberRange(min=20)], default=20)
form.num_tweets = IntegerField('Num Tweets', validators=[NumberRange(min=1, max=10)], default=5) # num_tweets max 10 olarak değiştirildi
form.sleep_time = IntegerField('Sleep Time', validators=[NumberRange(min=600)], default=600)
return form
def update_form(self, obj=None):
form = super(JobView, self).update_form(obj)
# Populate account_name choices dynamically
form.account_name.choices = [(account.auth_info_1, account.auth_info_1) for account in Account.query.filter_by(user_id=current_user.id)]
form.keyword_list.description = "Each keyword should be separated by a comma."
form.instructions = TextAreaField('Instructions', validators=[DataRequired()])
form.language.choices = [(lang, lang) for lang in nltk.corpus.stopwords.fileids()]
form.product = SelectField('Tweet Type', choices=[('Latest', 'Latest'), ('Top', 'Top')], widget=Select2Widget(), validators=[DataRequired()])
form.search_tweet_limit = IntegerField('Search Tweet Limit', validators=[NumberRange(min=20)], default=20)
form.num_tweets = IntegerField('Num Tweets', validators=[NumberRange(min=1, max=10)], default=5) # num_tweets max 10 olarak değiştirildi
form.sleep_time = IntegerField('Sleep Time', validators=[NumberRange(min=600)], default=600)
return form
def create_model(self, form):
try:
model = self.model()
form.populate_obj(model)
account = Account.query.filter_by(auth_info_1=model.account_name).first()
if not account:
form.errors['account_name'] = 'Account not found'
return False
model.user_id = current_user.id
self.session.add(model)
self.session.commit()
# Start job schedule
schedule_job(model.account_name, model.api_key, model.keyword_list, model.instructions, model.language, model.num_tweets, model.id, model.sleep_time, model.search_tweet_limit, model.product)
return model
except Exception as e:
if not self.handle_view_exception(e):
raise
self.session.rollback()
form.errors['general'] = str(e)
return False
def update_model(self, form, model):
try:
form.populate_obj(model)
account = Account.query.filter_by(auth_info_1=model.account_name).first()
if not account:
form.errors['account_name'] = 'Account not found'
return False
self.session.commit()
return model
except Exception as e:
if not self.handle_view_exception(e):
raise
self.session.rollback()
form.errors['general'] = str(e)
return False
def delete_model(self, model):
try:
job_id = model.id
schedule.clear(job_id)
self.session.delete(model)
self.session.commit()
return True
except Exception as e:
if not self.handle_view_exception(e):
raise
self.session.rollback()
form.errors['general'] = str(e)
return False
I expect all User’s Accounts to be listed as up-to-date when the form is loaded. However, when a new Account is added, the script must be closed and run again for it to appear.
neuralabz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.