Skip to content

Commit

Permalink
🚨 [#9] Run black & isort
Browse files Browse the repository at this point in the history
  • Loading branch information
ErhanCitil committed Nov 3, 2023
1 parent 32b15a1 commit e524d69
Show file tree
Hide file tree
Showing 32 changed files with 596 additions and 282 deletions.
168 changes: 99 additions & 69 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@
Set up my development environment for me!
"""

project_name = 'netland'
project_name = "netland"

parser = argparse.ArgumentParser(description=description)
parser.add_argument('target', choices=['production', 'staging', 'test', 'jenkins', 'dev'],
help='production/staging/test/jenkins/dev')
parser.add_argument('--project', default=project_name,
help='Name of the project in your src directory, "%s" by default' % project_name)
parser.add_argument('--init', action='store_true',
help='Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
'Automatically done if requirements/base.txt does not yet exist.')
parser.add_argument('--env', default='env',
help='Directory name for virtualenv, "env" by default')
parser.add_argument(
"target",
choices=["production", "staging", "test", "jenkins", "dev"],
help="production/staging/test/jenkins/dev",
)
parser.add_argument(
"--project",
default=project_name,
help='Name of the project in your src directory, "%s" by default' % project_name,
)
parser.add_argument(
"--init",
action="store_true",
help='Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
"Automatically done if requirements/base.txt does not yet exist.",
)
parser.add_argument(
"--env", default="env", help='Directory name for virtualenv, "env" by default'
)

args = parser.parse_args()


def replace_or_append(file_path, search_val, replace_val):
file_handle, abs_path = mkstemp()
new_file = open(abs_path, 'w')
old_file = open(file_path, 'r')
new_file = open(abs_path, "w")
old_file = open(file_path, "r")
found = False
for line in old_file:
if line.startswith(search_val):
Expand All @@ -51,89 +61,109 @@ def replace_or_append(file_path, search_val, replace_val):


def replace_wsgi_settings(target):
path = os.path.join('src', project_name, 'wsgi.py')
path = os.path.join("src", project_name, "wsgi.py")
replace_or_append(
path, 'os.environ.setdefault',
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
path,
"os.environ.setdefault",
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n'
% (project_name, target),
)


def replace_manage_settings(target):
path = os.path.join('src', project_name, 'manage.py')
path = os.path.join("src", project_name, "manage.py")
replace_or_append(
path, ' os.environ.setdefault',
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
path,
" os.environ.setdefault",
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n'
% (project_name, target),
)


def append_settings_activate(project, target, env):
if os.name == 'posix':
path = '%s/bin/activate' % env
replace_or_append(path, 'export DJANGO_SETTINGS_MODULE=',
'export DJANGO_SETTINGS_MODULE=\'%s.conf.%s\'\n' %
(project, target))
elif os.name == 'nt':
path = '%s\\Scripts\\activate.bat' % env
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
'set DJANGO_SETTINGS_MODULE=%s.conf.%s\n' %
(project, target))
path = '%s\\Scripts\\deactivate.bat' % env
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
'set DJANGO_SETTINGS_MODULE=\n')
if os.name == "posix":
path = "%s/bin/activate" % env
replace_or_append(
path,
"export DJANGO_SETTINGS_MODULE=",
"export DJANGO_SETTINGS_MODULE='%s.conf.%s'\n" % (project, target),
)
elif os.name == "nt":
path = "%s\\Scripts\\activate.bat" % env
replace_or_append(
path,
"set DJANGO_SETTINGS_MODULE=",
"set DJANGO_SETTINGS_MODULE=%s.conf.%s\n" % (project, target),
)
path = "%s\\Scripts\\deactivate.bat" % env
replace_or_append(
path, "set DJANGO_SETTINGS_MODULE=", "set DJANGO_SETTINGS_MODULE=\n"
)


def pip_compile_pin_requirements(virtualenv):
print('\n== Compiling base requirements ==\n')
if os.name == 'posix':
pip_path = os.path.join(virtualenv, 'bin', 'pip')
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install pip-tools'.format(pip=pip_path)
print("\n== Compiling base requirements ==\n")
if os.name == "posix":
pip_path = os.path.join(virtualenv, "bin", "pip")
elif os.name == "nt":
pip_path = os.path.join(virtualenv, "Scripts", "pip")
cmd_tpl = "{pip} install pip-tools".format(pip=pip_path)
call(cmd_tpl, shell=True)
print('Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt')
print('After that rerun bootstrap.py')
print(
"Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt"
)
print("After that rerun bootstrap.py")
sys.exit(1)


def main():
virtualenv = args.env
if not hasattr(sys, 'real_prefix'):
print('\n== Creating virtual environment ==\n')
call('virtualenv {0} --python=python3 --prompt="({1}-{2}) "'.format(
virtualenv, args.project, args.target
), shell=True)
print('\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target))
if not hasattr(sys, "real_prefix"):
print("\n== Creating virtual environment ==\n")
call(
'virtualenv {0} --python=python3 --prompt="({1}-{2}) "'.format(
virtualenv, args.project, args.target
),
shell=True,
)
print(
'\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target)
)
append_settings_activate(args.project, args.target, args.env)

if os.name == 'posix':
if os.name == "posix":
# Make manage.py executable
st = os.stat('src/manage.py')
os.chmod('src/manage.py', st.st_mode | stat.S_IEXEC)
django_admin_symlink = os.path.join(virtualenv, 'bin', 'django')
st = os.stat("src/manage.py")
os.chmod("src/manage.py", st.st_mode | stat.S_IEXEC)
django_admin_symlink = os.path.join(virtualenv, "bin", "django")
if not os.path.exists(django_admin_symlink):
os.symlink('../../src/manage.py', django_admin_symlink)

print('\n== Upgrading Pip ==\n')
if os.name == 'posix':
pip_path = os.path.join(virtualenv, 'bin', 'pip')
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install --upgrade pip'.format(pip=pip_path)
os.symlink("../../src/manage.py", django_admin_symlink)

print("\n== Upgrading Pip ==\n")
if os.name == "posix":
pip_path = os.path.join(virtualenv, "bin", "pip")
elif os.name == "nt":
pip_path = os.path.join(virtualenv, "Scripts", "pip")
cmd_tpl = "{pip} install --upgrade pip".format(pip=pip_path)
call(cmd_tpl, shell=True)

if args.init \
or not os.path.exists(os.path.join('requirements', 'base.txt')) \
or not os.path.exists(os.path.join('requirements', 'dev.txt')):
if (
args.init
or not os.path.exists(os.path.join("requirements", "base.txt"))
or not os.path.exists(os.path.join("requirements", "dev.txt"))
):
pip_compile_pin_requirements(virtualenv)

print('\n== Installing %s requirements ==\n' % args.target)
if os.name == 'posix':
os.environ['TMPDIR'] = '/var/tmp/'
pip_path = os.path.join(virtualenv, 'bin', 'pip')
cmd_tpl = '{pip} install -r requirements/{target}.txt'
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install -r requirements\\{target}.txt'
print("\n== Installing %s requirements ==\n" % args.target)
if os.name == "posix":
os.environ["TMPDIR"] = "/var/tmp/"
pip_path = os.path.join(virtualenv, "bin", "pip")
cmd_tpl = "{pip} install -r requirements/{target}.txt"
elif os.name == "nt":
pip_path = os.path.join(virtualenv, "Scripts", "pip")
cmd_tpl = "{pip} install -r requirements\\{target}.txt"
return call(cmd_tpl.format(pip=pip_path, target=args.target), shell=True)

if __name__ == '__main__':

if __name__ == "__main__":
sys.exit(main())
1 change: 0 additions & 1 deletion src/netland/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
1 change: 1 addition & 0 deletions src/netland/accounts/tests/test_permission_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_user_add(self):

self.assertEqual(response.status_code, 200)


class PasswordResetViewTests(TestCase):
def setUp(self):
super().setUp()
Expand Down
6 changes: 4 additions & 2 deletions src/netland/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
WSGI_APPLICATION = "netland.wsgi.application"

# Translations
LOCALE_PATHS = (DJANGO_PROJECT_DIR / "conf" / "locale", )
LOCALE_PATHS = (DJANGO_PROJECT_DIR / "conf" / "locale",)

#
# SERVING of static and media files
Expand Down Expand Up @@ -356,7 +356,9 @@
repo = git.Repo(search_parent_directories=True)
try:
GIT_SHA = repo.head.object.hexsha
except ValueError: # on startproject initial runs before any git commits have been made
except (
ValueError
): # on startproject initial runs before any git commits have been made
GIT_SHA = repo.active_branch.name
else:
GIT_SHA = None
Expand Down
4 changes: 1 addition & 3 deletions src/netland/conf/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def get_db_name(prefix):
INSTALLED_APPS += [
"django_jenkins",
]
PROJECT_APPS = [
app for app in INSTALLED_APPS if app.startswith("netland.")
]
PROJECT_APPS = [app for app in INSTALLED_APPS if app.startswith("netland.")]
JENKINS_TASKS = (
# 'django_jenkins.tasks.run_pylint', # Pylint < 2.0 does not run on Python 3.7+
# "django_jenkins.tasks.run_pep8", # -> renamed to pycodestyle, but django-jenkins hasn't been updated in 6 years
Expand Down
4 changes: 2 additions & 2 deletions src/netland/login/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class LoginConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'netland.login'
default_auto_field = "django.db.models.BigAutoField"
name = "netland.login"
10 changes: 6 additions & 4 deletions src/netland/login/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib.auth.views import LogoutView
from django.urls import path

from .views import *
from django.contrib.auth.views import LogoutView

urlpatterns = [
path('login/', Login.as_view(), name='login'),
path('logout/', LogoutView.as_view(next_page='login'), name='logout'),
]
path("login/", Login.as_view(), name="login"),
path("logout/", LogoutView.as_view(next_page="login"), name="logout"),
]
11 changes: 6 additions & 5 deletions src/netland/login/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.shortcuts import render
from django.contrib.auth.views import LoginView, LogoutView
from django.shortcuts import render
from django.urls import reverse_lazy


# Create your views here.
class Login(LoginView):
template_name = 'registration/login.html'
fields = '__all__'
template_name = "registration/login.html"
fields = "__all__"
redirect_authenticated_user = True

def get_success_url(self):
return reverse_lazy('index')

return reverse_lazy("index")
4 changes: 3 additions & 1 deletion src/netland/movie/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.contrib import admin

from .models import Movies

# Register your models here.
admin.site.register(Movies)
admin.site.register(Movies)
4 changes: 2 additions & 2 deletions src/netland/movie/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class MovieConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'netland.movie'
default_auto_field = "django.db.models.BigAutoField"
name = "netland.movie"
27 changes: 18 additions & 9 deletions src/netland/movie/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
from django import forms
from django import forms

from .models import Movies


class MovieForm(forms.ModelForm):
class Meta:
model = Movies
fields = ['title', 'length_in_minutes', 'released_at', 'country_of_origin', 'summary', 'youtube_trailer_id']
fields = [
"title",
"length_in_minutes",
"released_at",
"country_of_origin",
"summary",
"youtube_trailer_id",
]
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control'}),
'length_in_minutes': forms.TextInput(attrs={'class': 'form-control'}),
'released_at': forms.TextInput(attrs={'class': 'form-control'}),
'country_of_origin': forms.TextInput(attrs={'class': 'form-control'}),
'summary': forms.Textarea(attrs={'class': 'form-control'}),
'youtube_trailer_id': forms.TextInput(attrs={'class': 'form-control'}),
}
"title": forms.TextInput(attrs={"class": "form-control"}),
"length_in_minutes": forms.TextInput(attrs={"class": "form-control"}),
"released_at": forms.TextInput(attrs={"class": "form-control"}),
"country_of_origin": forms.TextInput(attrs={"class": "form-control"}),
"summary": forms.Textarea(attrs={"class": "form-control"}),
"youtube_trailer_id": forms.TextInput(attrs={"class": "form-control"}),
}
Loading

0 comments on commit e524d69

Please sign in to comment.