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

refactor: Deduplicate get_new_identifier #7182

Merged
merged 1 commit into from
Aug 4, 2020
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
9 changes: 9 additions & 0 deletions app/api/helpers/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ def get_new_slug(model, name):
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)


def get_new_identifier(model):
identifier = str(uuid.uuid4())
count = get_count(model.query.filter_by(identifier=identifier))
if count == 0:
return identifier
else:
return get_new_identifier(model)
2 changes: 1 addition & 1 deletion app/api/helpers/scheduled_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from app.api.helpers.query import get_user_event_roles_by_role_name
from app.api.helpers.storage import UPLOAD_PATHS
from app.api.helpers.utilities import monthdelta, make_dict
from app.api.helpers.utilities import make_dict, monthdelta
from app.instance import celery
from app.models import db
from app.models.event import Event
Expand Down
14 changes: 4 additions & 10 deletions app/models/event_invoice.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import time
import uuid

from sqlalchemy.sql import func

from app.api.helpers.db import get_count
from app.api.helpers.db import get_new_identifier
from app.models import db
from app.models.base import SoftDeletionModel


def get_new_identifier():
identifier = str(uuid.uuid4())
count = get_count(EventInvoice.query.filter_by(identifier=identifier))
if count == 0:
return identifier
else:
return get_new_identifier()
def get_new_id():
return get_new_identifier(EventInvoice)


class EventInvoice(SoftDeletionModel):
Expand All @@ -25,7 +19,7 @@ class EventInvoice(SoftDeletionModel):
__tablename__ = 'event_invoices'

id = db.Column(db.Integer, primary_key=True)
identifier = db.Column(db.String, unique=True, default=get_new_identifier)
identifier = db.Column(db.String, unique=True, default=get_new_id)
amount = db.Column(db.Float)
address = db.Column(db.String)
city = db.Column(db.String)
Expand Down
14 changes: 4 additions & 10 deletions app/models/order.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import time
import uuid

from sqlalchemy.sql import func

from app.api.helpers.db import get_count
from app.api.helpers.db import get_new_identifier
from app.models import db
from app.models.base import SoftDeletionModel


def get_new_order_identifier():
identifier = str(uuid.uuid4())
count = get_count(Order.query.filter_by(identifier=identifier))
if count == 0:
return identifier
else:
return get_new_order_identifier()
def get_new_id():
return get_new_identifier(Order)


def get_updatable_fields():
Expand Down Expand Up @@ -54,7 +48,7 @@ class Order(SoftDeletionModel):
__tablename__ = "orders"

id = db.Column(db.Integer, primary_key=True)
identifier = db.Column(db.String, unique=True, default=get_new_order_identifier)
identifier = db.Column(db.String, unique=True, default=get_new_id)
amount = db.Column(db.Float, nullable=False, default=0)
address = db.Column(db.String)
city = db.Column(db.String)
Expand Down