Skip to content

Commit 584f2bb

Browse files
felipmartinsadamghill
authored andcommitted
replaces loop code for extracted methods calls
1 parent f579235 commit 584f2bb

File tree

1 file changed

+8
-98
lines changed

1 file changed

+8
-98
lines changed

django_unicorn/management/commands/startunicorn.py

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -210,111 +210,21 @@ def handle(self, **options):
210210
f"An app named '{app_name}' does not exist yet. You might need to create it first."
211211
) from e
212212

213-
is_first_component = False
214-
215213
if not app_directory.exists():
216214
app_directory.mkdir()
217215

218-
# Create component
219-
component_base_path = app_directory / "components"
220-
221-
if not component_base_path.exists():
222-
component_base_path.mkdir()
223-
224-
self.stdout.write(self.style.SUCCESS(f"Created your first component in '{app_name}'! ✨\n"))
225-
226-
is_first_component = True
227-
228-
(component_base_path / "__init__.py").touch(exist_ok=True)
229-
230-
nested_paths = []
231-
232-
for component_name in options["component_names"]:
233-
if "." in component_name:
234-
(*nested_paths, component_name) = component_name.split(".") # noqa: PLW2901
235-
236-
for nested_path in nested_paths:
237-
component_base_path /= nested_path
238-
239-
if not component_base_path.exists():
240-
component_base_path.mkdir()
241-
242-
(component_base_path / "__init__.py").touch(exist_ok=True)
243-
244-
snake_case_component_name = convert_to_snake_case(component_name)
245-
pascal_case_component_name = convert_to_pascal_case(component_name)
246-
247-
component_path = component_base_path / f"{snake_case_component_name}.py"
248-
249-
if component_path.exists():
250-
self.stdout.write(
251-
self.style.ERROR(f"Skipping creating {snake_case_component_name}.py because it already exists.")
252-
)
253-
else:
254-
component_path.write_text(
255-
COMPONENT_FILE_CONTENT.format(**{"pascal_case_component_name": pascal_case_component_name})
256-
)
257-
self.stdout.write(self.style.SUCCESS(f"Created {component_path}."))
258-
259-
# Create template
260-
template_base_path = app_directory / "templates" / "unicorn"
261-
262-
if not template_base_path.exists():
263-
if not (app_directory / "templates").exists():
264-
(app_directory / "templates").mkdir()
265-
266-
template_base_path.mkdir()
267-
268-
for nested_path in nested_paths:
269-
template_base_path /= nested_path
270-
271-
if not template_base_path.exists():
272-
template_base_path.mkdir()
216+
paths, is_first = self.check_initials_directories(app_directory)
273217

274-
template_path = template_base_path / f"{component_name}.html"
218+
for name in options["component_names"]:
219+
nested_path, name = self.obtain_nested_path(name)
275220

276-
if template_path.exists():
277-
self.stdout.write(
278-
self.style.ERROR(f"Skipping creating {component_name}.html because it already exists.")
279-
)
280-
else:
281-
template_path.write_text(TEMPLATE_FILE_CONTENT)
282-
self.stdout.write(self.style.SUCCESS(f"Created {template_path}."))
283-
284-
if is_first_component:
285-
will_star_repo = input(
286-
"\nStarring the GitHub repo helps other Django users find Unicorn. Can you star it for me? [y/N] "
287-
)
288-
289-
if will_star_repo.strip().lower() in ("y", "yes"):
290-
self.stdout.write(self.style.SUCCESS("Thank you for helping spread the word about Unicorn!"))
221+
self.create_nested_directories(paths, nested_path)
291222

292-
self.stdout.write(
293-
"""
294-
,/
295-
//
296-
,//
297-
__ /| |//
298-
`__/\\_ --(/|___/-/
299-
\\|\\_-\\___ __-_`- /-/ \\.
300-
|\\_-___,-\\_____--/_)' ) \\
301-
\\ -_ / __ \\( `( __`\\|
302-
`\\__| |\\) (/|
303-
',--//-| \\ | ' /
304-
/,---| \\ /
305-
`_/ _,' | |
306-
__/'/ | |
307-
___/ \\ ( ) /
308-
\\____/\\
309-
\\
310-
"""
311-
)
223+
self.create_component_and_template(paths, nested_path, name)
312224

313-
webbrowser.open("https://github.com/adamghill/django-unicorn", new=2)
314-
else:
315-
self.stdout.write(
316-
self.style.ERROR("That's a bummer, but I understand. I hope you will star it for me later!")
317-
)
225+
if is_first:
226+
self.ask_for_star_repo()
227+
is_first = False
318228

319229
if is_new_app:
320230
msg = f'\nMake sure to add `"{app_name}",` to your INSTALLED_APPS list in your settings file if necessary.' # noqa: E501

0 commit comments

Comments
 (0)