-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored ahmadfikrimasyhur/flask-admin #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,9 +108,11 @@ class MyAdminIndexView(admin.AdminIndexView): | |
|
||
@expose('/') | ||
def index(self): | ||
if not login.current_user.is_authenticated: | ||
return redirect(url_for('.login_view')) | ||
return super(MyAdminIndexView, self).index() | ||
return ( | ||
super(MyAdminIndexView, self).index() | ||
if login.current_user.is_authenticated | ||
else redirect(url_for('.login_view')) | ||
) | ||
|
||
@expose('/login/', methods=('GET', 'POST')) | ||
def login_view(self): | ||
|
@@ -201,8 +203,14 @@ def build_sample_db(): | |
user.first_name = first_names[i] | ||
user.last_name = last_names[i] | ||
user.login = user.first_name.lower() | ||
user.email = user.login + "@example.com" | ||
user.password = generate_password_hash(''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(10))) | ||
user.email = f"{user.login}@example.com" | ||
user.password = generate_password_hash( | ||
''.join( | ||
random.choice(string.ascii_lowercase + string.digits) | ||
for _ in range(10) | ||
) | ||
) | ||
|
||
Comment on lines
-204
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
db.session.add(user) | ||
|
||
db.session.commit() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,8 +138,12 @@ def build_sample_db(): | |
] | ||
|
||
for i in range(len(first_names)): | ||
tmp_email = first_names[i].lower() + "." + last_names[i].lower() + "@example.com" | ||
tmp_pass = ''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(10)) | ||
tmp_email = f"{first_names[i].lower()}.{last_names[i].lower()}@example.com" | ||
tmp_pass = ''.join( | ||
random.choice(string.ascii_lowercase + string.digits) | ||
for _ in range(10) | ||
) | ||
|
||
Comment on lines
-141
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
user_datastore.create_user( | ||
first_name=first_names[i], | ||
last_name=last_names[i], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
# Create in-memory database | ||
DATABASE_FILE = 'sample_db.sqlite' | ||
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DATABASE_FILE | ||
SQLALCHEMY_DATABASE_URI = f'sqlite:///{DATABASE_FILE}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
SQLALCHEMY_ECHO = True | ||
|
||
# Flask-Security config | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,7 @@ | |
|
||
@babel.localeselector | ||
def get_locale(): | ||
override = request.args.get('lang') | ||
|
||
if override: | ||
if override := request.args.get('lang'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
session['lang'] = override | ||
|
||
return session.get('lang', 'en') | ||
|
@@ -58,7 +56,7 @@ def __unicode__(self): | |
# Flask views | ||
@app.route('/') | ||
def index(): | ||
tmp = u""" | ||
return u""" | ||
Comment on lines
-61
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
<p><a href="/admin/?lang=en">Click me to get to Admin! (English)</a></p> | ||
<p><a href="/admin/?lang=cs">Click me to get to Admin! (Czech)</a></p> | ||
<p><a href="/admin/?lang=de">Click me to get to Admin! (German)</a></p> | ||
|
@@ -71,7 +69,6 @@ def index(): | |
<p><a href="/admin/?lang=zh_CN">Click me to get to Admin! (Chinese - Simplified)</a></p> | ||
<p><a href="/admin/?lang=zh_TW">Click me to get to Admin! (Chinese - Traditional)</a></p> | ||
""" | ||
return tmp | ||
|
||
if __name__ == '__main__': | ||
# Create admin | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,8 +92,8 @@ def build_sample_db(): | |
|
||
for i in range(len(first_names)): | ||
user = User() | ||
user.name = first_names[i] + " " + last_names[i] | ||
user.email = first_names[i].lower() + "@example.com" | ||
user.name = f"{first_names[i]} {last_names[i]}" | ||
user.email = f"{first_names[i].lower()}@example.com" | ||
Comment on lines
-95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
db.session.add(user) | ||
|
||
sample_text = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,8 +86,8 @@ def build_sample_db(): | |
|
||
for i in range(len(first_names)): | ||
user = User() | ||
user.name = first_names[i] + " " + last_names[i] | ||
user.email = first_names[i].lower() + "@example.com" | ||
user.name = f"{first_names[i]} {last_names[i]}" | ||
user.email = f"{first_names[i].lower()}@example.com" | ||
Comment on lines
-89
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
db.session.add(user) | ||
|
||
sample_text = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,7 +111,7 @@ class CKTextAreaWidget(widgets.TextArea): | |
def __call__(self, field, **kwargs): | ||
# add WYSIWYG class to existing classes | ||
existing_classes = kwargs.pop('class', '') or kwargs.pop('class_', '') | ||
kwargs['class'] = '{} {}'.format(existing_classes, "ckeditor") | ||
kwargs['class'] = f'{existing_classes} ckeditor' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return super(CKTextAreaWidget, self).__call__(field, **kwargs) | ||
|
||
|
||
|
@@ -144,12 +144,15 @@ class FileView(sqla.ModelView): | |
|
||
|
||
class ImageView(sqla.ModelView): | ||
def _list_thumbnail(view, context, model, name): | ||
if not model.path: | ||
return '' | ||
|
||
return Markup('<img src="%s">' % url_for('static', | ||
filename=form.thumbgen_filename(model.path))) | ||
def _list_thumbnail(self, context, model, name): | ||
return ( | ||
Markup( | ||
'<img src="%s">' | ||
% url_for('static', filename=form.thumbgen_filename(model.path)) | ||
) | ||
if model.path | ||
else '' | ||
) | ||
Comment on lines
-147
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
column_formatters = { | ||
'path': _list_thumbnail | ||
|
@@ -256,9 +259,9 @@ def build_sample_db(): | |
user = User() | ||
user.first_name = first_names[i] | ||
user.last_name = last_names[i] | ||
user.email = user.first_name.lower() + "@example.com" | ||
tmp = ''.join(random.choice(string.digits) for i in range(10)) | ||
user.phone = "(" + tmp[0:3] + ") " + tmp[3:6] + " " + tmp[6::] | ||
user.email = f"{user.first_name.lower()}@example.com" | ||
tmp = ''.join(random.choice(string.digits) for _ in range(10)) | ||
user.phone = "(" + tmp[:3] + ") " + tmp[3:6] + " " + tmp[6::] | ||
Comment on lines
-259
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
user.city = locations[i][0] | ||
user.country = locations[i][1] | ||
db.session.add(user) | ||
|
@@ -267,13 +270,13 @@ def build_sample_db(): | |
for name in images: | ||
image = Image() | ||
image.name = name | ||
image.path = name.lower() + ".jpg" | ||
image.path = f"{name.lower()}.jpg" | ||
db.session.add(image) | ||
|
||
for i in [1, 2, 3]: | ||
file = File() | ||
file.name = "Example " + str(i) | ||
file.path = "example_" + str(i) + ".pdf" | ||
file.name = f"Example {str(i)}" | ||
file.path = f"example_{str(i)}.pdf" | ||
db.session.add(file) | ||
|
||
sample_text = "<h2>This is a test</h2>" + \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ class UserInfo(BaseModel): | |
user = peewee.ForeignKeyField(User) | ||
|
||
def __unicode__(self): | ||
return '%s - %s' % (self.key, self.value) | ||
return f'{self.key} - {self.value}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class Post(BaseModel): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ def get_list(self, *args, **kwargs): | |
users = db.user.find(query, fields=('name',)) | ||
|
||
# Contribute user names to the models | ||
users_map = dict((x['_id'], x['name']) for x in users) | ||
users_map = {x['_id']: x['name'] for x in users} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
for item in data: | ||
item['user_name'] = users_map.get(item['user_id']) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ def __init__(self, keyword=None): | |
self.keyword = keyword | ||
|
||
def __repr__(self): | ||
return 'Keyword(%s)' % repr(self.keyword) | ||
return f'Keyword({repr(self.keyword)})' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class UserAdmin(sqla.ModelView): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,9 +89,7 @@ def postprocess_form(self, form_class): | |
return form_class | ||
|
||
def on_model_change(self, form, model): | ||
file_data = request.files.get(form.upload.name) | ||
|
||
if file_data: | ||
if file_data := request.files.get(form.upload.name): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
model.path = secure_filename(file_data.filename) | ||
file_data.save(op.join(base_path, model.path)) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,7 @@ | |
|
||
@babel.localeselector | ||
def get_locale(): | ||
override = request.args.get('lang') | ||
|
||
if override: | ||
if override := request.args.get('lang'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
session['lang'] = override | ||
|
||
return session.get('lang', 'en') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ | |
|
||
# Create in-memory database | ||
DATABASE_FILE = 'sample_db.sqlite' | ||
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DATABASE_FILE | ||
SQLALCHEMY_DATABASE_URI = f'sqlite:///{DATABASE_FILE}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
SQLALCHEMY_ECHO = True | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ def build_sample_db(): | |
user.type = random.choice(AVAILABLE_USER_TYPES)[0] | ||
user.first_name = first_names[i] | ||
user.last_name = last_names[i] | ||
user.email = first_names[i].lower() + "@example.com" | ||
user.email = f"{first_names[i].lower()}@example.com" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
user.website = "https://www.example.com" | ||
user.ip_address = "127.0.0.1" | ||
|
@@ -107,7 +107,7 @@ def build_sample_db(): | |
entry = random.choice(sample_text) # select text at random | ||
post = Post() | ||
post.user = user | ||
post.title = "{}'s opinion on {}".format(user.first_name, entry['title']) | ||
post.title = f"{user.first_name}'s opinion on {entry['title']}" | ||
post.text = entry['content'] | ||
post.background_color = random.choice(["#cccccc", "red", "lightblue", "#0f0"]) | ||
tmp = int(1000 * random.random()) # random number between 0 and 1000: | ||
|
@@ -120,12 +120,12 @@ def build_sample_db(): | |
db.session.add(trunk) | ||
for i in range(5): | ||
branch = Tree() | ||
branch.name = "Branch " + str(i + 1) | ||
branch.name = f"Branch {str(i + 1)}" | ||
branch.parent = trunk | ||
db.session.add(branch) | ||
for j in range(5): | ||
leaf = Tree() | ||
leaf.name = "Leaf " + str(j + 1) | ||
leaf.name = f"Leaf {str(j + 1)}" | ||
leaf.parent = branch | ||
db.session.add(leaf) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
# Flask views | ||
@app.route('/') | ||
def index(): | ||
tmp = u""" | ||
return u""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
<p><a href="/admin/?lang=en">Click me to get to Admin! (English)</a></p> | ||
<p><a href="/admin/?lang=cs">Click me to get to Admin! (Czech)</a></p> | ||
<p><a href="/admin/?lang=de">Click me to get to Admin! (German)</a></p> | ||
|
@@ -28,7 +28,6 @@ def index(): | |
<p><a href="/admin/?lang=zh_CN">Click me to get to Admin! (Chinese - Simplified)</a></p> | ||
<p><a href="/admin/?lang=zh_TW">Click me to get to Admin! (Chinese - Traditional)</a></p> | ||
""" | ||
return tmp | ||
|
||
|
||
@app.route('/favicon.ico') | ||
|
@@ -50,7 +49,11 @@ def operation(self): | |
|
||
# Customized User model admin | ||
def phone_number_formatter(view, context, model, name): | ||
return Markup("<nobr>{}</nobr>".format(model.phone_number)) if model.phone_number else None | ||
return ( | ||
Markup(f"<nobr>{model.phone_number}</nobr>") | ||
if model.phone_number | ||
else None | ||
) | ||
Comment on lines
-53
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def is_numberic_validator(form, field): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,18 +54,21 @@ class User(db.Model): | |
def phone_number(self): | ||
if self.dialling_code and self.local_phone_number: | ||
number = str(self.local_phone_number) | ||
return "+{} ({}) {} {} {}".format(self.dialling_code, number[0], number[1:3], number[3:6], number[6::]) | ||
return f"+{self.dialling_code} ({number[0]}) {number[1:3]} {number[3:6]} {number[6::]}" | ||
|
||
Comment on lines
-57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return | ||
|
||
@phone_number.expression | ||
def phone_number(cls): | ||
return sql.operators.ColumnOperators.concat(cast(cls.dialling_code, db.String), cls.local_phone_number) | ||
def phone_number(self): | ||
return sql.operators.ColumnOperators.concat( | ||
cast(self.dialling_code, db.String), self.local_phone_number | ||
) | ||
Comment on lines
-61
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __str__(self): | ||
return "{}, {}".format(self.last_name, self.first_name) | ||
return f"{self.last_name}, {self.first_name}" | ||
Comment on lines
-65
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __repr__(self): | ||
return "{}: {}".format(self.id, self.__str__()) | ||
return f"{self.id}: {self.__str__()}" | ||
Comment on lines
-68
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
# Create M2M table | ||
|
@@ -90,15 +93,15 @@ class Post(db.Model): | |
tags = db.relationship('Tag', secondary=post_tags_table) | ||
|
||
def __str__(self): | ||
return "{}".format(self.title) | ||
return f"{self.title}" | ||
Comment on lines
-93
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class Tag(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.Unicode(64), unique=True) | ||
|
||
def __str__(self): | ||
return "{}".format(self.name) | ||
return f"{self.name}" | ||
Comment on lines
-101
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class Tree(db.Model): | ||
|
@@ -110,4 +113,4 @@ class Tree(db.Model): | |
parent = db.relationship('Tree', remote_side=[id], backref='children') | ||
|
||
def __str__(self): | ||
return "{}".format(self.name) | ||
return f"{self.name}" | ||
Comment on lines
-113
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,11 @@ def get_property(obj, name, old_name, default=None): | |
Default value | ||
""" | ||
if hasattr(obj, old_name): | ||
warnings.warn('Property %s is obsolete, please use %s instead' % | ||
(old_name, name), stacklevel=2) | ||
warnings.warn( | ||
f'Property {old_name} is obsolete, please use {name} instead', | ||
stacklevel=2, | ||
) | ||
|
||
Comment on lines
-32
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return getattr(obj, old_name) | ||
|
||
return getattr(obj, name, default) | ||
|
@@ -40,7 +43,7 @@ class ObsoleteAttr(object): | |
def __init__(self, new_name, old_name, default): | ||
self.new_name = new_name | ||
self.old_name = old_name | ||
self.cache = '_cache_' + new_name | ||
self.cache = f'_cache_{new_name}' | ||
Comment on lines
-43
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.default = default | ||
|
||
def __get__(self, obj, objtype=None): | ||
|
@@ -53,8 +56,11 @@ def __get__(self, obj, objtype=None): | |
|
||
# Check if there's old attribute | ||
if hasattr(obj, self.old_name): | ||
warnings.warn('Property %s is obsolete, please use %s instead' % | ||
(self.old_name, self.new_name), stacklevel=2) | ||
warnings.warn( | ||
f'Property {self.old_name} is obsolete, please use {self.new_name} instead', | ||
stacklevel=2, | ||
) | ||
|
||
Comment on lines
-56
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return getattr(obj, self.old_name) | ||
|
||
# Return default otherwise | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,7 @@ | |
filter_list = lambda f, l: list(filter(f, l)) | ||
|
||
def as_unicode(s): | ||
if isinstance(s, bytes): | ||
return s.decode('utf-8') | ||
|
||
return str(s) | ||
return s.decode('utf-8') if isinstance(s, bytes) else str(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def csv_encode(s): | ||
''' Returns unicode string expected by Python 3's csv module ''' | ||
|
@@ -50,10 +47,7 @@ def csv_encode(s): | |
filter_list = filter | ||
|
||
def as_unicode(s): | ||
if isinstance(s, str): | ||
return s.decode('utf-8') | ||
|
||
return unicode(s) | ||
return s.decode('utf-8') if isinstance(s, str) else unicode(s) | ||
Comment on lines
-53
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def csv_encode(s): | ||
''' Returns byte string expected by Python 2's csv module ''' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
MyAdminIndexView.index
refactored with the following changes:swap-if-expression
)reintroduce-else
)assign-if-exp
)