Skip to content

Commit

Permalink
Merge pull request #705 from tj-django/chore/update-local-dev-setup
Browse files Browse the repository at this point in the history
chore: update local dev setup
  • Loading branch information
repo-ranger[bot] committed Nov 21, 2022
2 parents 41b0a82 + b773bae commit 8c282f4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ migrations:
migrate:
@$(MANAGE_PY) migrate

run: migrate
@echo "Starting server..."
@$(MANAGE_PY) runserver

default-user: migrate
@echo "Creating a default user..."
@$(MANAGE_PY) create_default_user
@echo "Username: admin@admin.com"
@echo "Password: admin"

run: default-user
@echo "Starting server..."
@$(MANAGE_PY) runserver

makemessages: clean-build ## Runs over the entire source tree of the current directory and pulls out all strings marked for translation.
@$(MANAGE_PY) makemessages --locale=en_US --ignore=sample --ignore=django_clone
@$(MANAGE_PY) makemessages --locale=fr --ignore=sample --ignore=django_clone
Expand Down
39 changes: 37 additions & 2 deletions model_clone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def clean_value(value, suffix):
:param suffix: The suffix value to be replaced with an empty string.
:type suffix: `str`
:return: Stripped string without the suffix.
:rtype: `str`
"""
# type: (str, str) -> str
return re.sub(r"([\s-]?){}[\s-][\d]$".format(suffix), "", value, flags=re.I)
Expand All @@ -154,7 +155,10 @@ def clean_value(value, suffix):
@contextlib.contextmanager
def transaction_autocommit(using=None):
"""
Context manager with autocommit enabled.
Context manager to enable autocommit.
:param using: The database alias used to save the created instances.
:type using: str
"""
try:
transaction.set_autocommit(True, using=using)
Expand All @@ -167,6 +171,13 @@ def transaction_autocommit(using=None):
def context_mutable_attribute(obj, key, value):
"""
Context manager that modifies an obj temporarily.
:param obj: The object to modify.
:type obj: `object`
:param key: The attribute name to modify.
:type key: `str`
:param value: The value to set on the attribute.
:type value: `object`
"""
attribute_exists = hasattr(obj, key)
default = getattr(obj, key, None)
Expand All @@ -182,8 +193,20 @@ def context_mutable_attribute(obj, key, value):

def get_value(value, suffix, transform, max_length, index=None):
"""
Append a suffix to a string value and apply a pass directly to a
Append a suffix to a string value and pass it directly to a
transformation function.
:param value: Current value e.g "Test Copy" or "test-copy" for slug fields.
:type value: `str`
:param suffix: The suffix value to be replaced with an empty string.
:type suffix: `str`
:param transform: The transformation function to apply to the value.
:type transform: `callable`
:param max_length: The maximum length of the value.
:type max_length: `int`
:param index: The index of the copy.
:type index: `int`
:return: The transformed value.
"""
if index is None:
duplicate_suffix = " {}".format(suffix.strip())
Expand All @@ -202,6 +225,18 @@ def get_value(value, suffix, transform, max_length, index=None):
def generate_value(value, suffix, transform, max_length, max_attempts):
"""
Given a fixed max attempt generate a unique value.
:param value: Current value e.g "Test Copy" or "test-copy" for slug fields.
:type value: `str`
:param suffix: The suffix value to be replaced with an empty string.
:type suffix: `str`
:param transform: The transformation function to apply to the value.
:type transform: `callable`
:param max_length: The maximum length of the value.
:type max_length: `int`
:param max_attempts: The maximum number of attempts to generate a unique value.
:type max_attempts: `int`
:return: The unique value.
"""

for i in range(1, max_attempts):
Expand Down
1 change: 0 additions & 1 deletion sample/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.contrib import admin

# Register your models here.
from model_clone import CloneModelAdmin
from sample.models import Author, Book, Library, Page

Expand Down
8 changes: 8 additions & 0 deletions sample_assignment/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin

from sample_assignment.models import Contract


@admin.register(Contract)
class ContractAdmin(admin.ModelAdmin):
pass
3 changes: 3 additions & 0 deletions sample_assignment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

class Contract(models.Model):
title = models.CharField(max_length=255)

def __str__(self):
return self.title

0 comments on commit 8c282f4

Please sign in to comment.