Skip to content
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

Refactored accounts.models #396

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils.text import slugify
from contacts.models import Contact
from teams.models import Teams
from common import utils


class Tags(models.Model):
Expand Down Expand Up @@ -80,36 +81,19 @@ class Meta:
ordering = ["-created_on"]

def get_complete_address(self):
"""Concatenates complete address."""
address = ""
if self.billing_address_line:
address += self.billing_address_line
if self.billing_street:
if address:
address += ", " + self.billing_street
else:
address += self.billing_street
if self.billing_city:
if address:
address += ", " + self.billing_city
else:
address += self.billing_city
if self.billing_state:
if address:
address += ", " + self.billing_state
else:
address += self.billing_state
if self.billing_postcode:
if address:
address += ", " + self.billing_postcode
else:
address += self.billing_postcode
if self.billing_country:
if address:
address += ", " + self.get_billing_country_display()
else:
address += self.get_billing_country_display()
add_to_address = [
self.billing_street,
self.billing_city,
self.billing_state,
self.billing_postcode,
self.get_billing_country_display(),
]
address = utils.append_str_to(address, *add_to_address)
return address


@property
def created_on_arrow(self):
return arrow.get(self.created_on).humanize()
Expand Down
19 changes: 19 additions & 0 deletions common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,22 @@ def convert_to_custom_timezone(custom_date, custom_timezone, to_utc=False):
custom_date = user_time_zone.localize(custom_date.replace(tzinfo=None))
user_time_zone = pytz.UTC
return custom_date.astimezone(user_time_zone)


def append_str_to(append_to: str, *args, sep=", ", **kwargs):
"""Concatenate to a string.

Args:
append_to(str): The string to append to.
args(list): list of string characters to concatenate.
sep(str): Seperator to use between concatenated strings.
kwargs(dict): Mapping of variables with intended string values.

Returns:
str, joined strings seperated
"""
append_to = append_to or ""
result_list = [append_to] + list(args) + list(kwargs.values())
return f"{sep}".join(filter(len, result_list))