Skip to content

Commit

Permalink
refactor: Deduplicate get_new_slug method (#7015)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjana-302 authored May 16, 2020
1 parent a5b9c1a commit 069d8f8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 77 deletions.
21 changes: 21 additions & 0 deletions app/api/helpers/db.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import uuid

from flask import request
from flask_rest_jsonapi.exceptions import ObjectNotFound
Expand Down Expand Up @@ -108,3 +109,23 @@ def get_count(query):
count_q = query.statement.with_only_columns([func.count()]).order_by(None)
count = query.session.execute(count_q).scalar()
return count


def get_new_slug(model, name):
"""
Helper function to create a new slug if required, else return orignal.
:param model: Specify model from db.
:param name: Identifier to generate slug.
"""
slug = (
name.lower()
.replace("& ", "")
.replace(",", "")
.replace("/", "-")
.replace(" ", "-")
)
count = get_count(model.query.filter_by(slug=slug))
if count == 0:
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)
21 changes: 2 additions & 19 deletions app/models/event_location.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import uuid

from app.api.helpers.db import get_count
from app.api.helpers.db import get_new_slug
from app.models import db


def get_new_slug(name):
slug = (
name.lower()
.replace("& ", "")
.replace(",", "")
.replace("/", "-")
.replace(" ", "-")
)
count = get_count(EventLocation.query.filter_by(slug=slug))
if count == 0:
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)


class EventLocation(db.Model):
"""Event location object table"""

Expand All @@ -30,7 +13,7 @@ class EventLocation(db.Model):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.slug = get_new_slug(name=self.name)
self.slug = get_new_slug(EventLocation, self.name)

def __repr__(self):
return '<EventLocation %r>' % self.slug
22 changes: 2 additions & 20 deletions app/models/event_sub_topic.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import uuid

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


# TODO(Areeb): Deduplicate this
def get_new_slug(name):
slug = (
name.lower()
.replace("& ", "")
.replace(",", "")
.replace("/", "-")
.replace(" ", "-")
)
count = get_count(EventSubTopic.query.filter_by(slug=slug))
if count == 0:
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)


class EventSubTopic(SoftDeletionModel):
"""Event sub topic object table"""

Expand All @@ -37,7 +19,7 @@ class EventSubTopic(SoftDeletionModel):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.slug = get_new_slug(name=self.name)
self.slug = get_new_slug(EventSubTopic, self.name)

def __repr__(self):
return '<EventSubTopic %r>' % self.name
21 changes: 2 additions & 19 deletions app/models/event_topic.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import uuid

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


def get_new_slug(name):
slug = (
name.lower()
.replace("& ", "")
.replace(",", "")
.replace("/", "-")
.replace(" ", "-")
)
count = get_count(EventTopic.query.filter_by(slug=slug))
if count == 0:
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)


class EventTopic(SoftDeletionModel):
"""Event topic object table"""

Expand All @@ -34,7 +17,7 @@ class EventTopic(SoftDeletionModel):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.slug = get_new_slug(name=self.name)
self.slug = get_new_slug(EventTopic, self.name)

def __repr__(self):
return '<EventTopic %r>' % self.name
21 changes: 2 additions & 19 deletions app/models/event_type.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import uuid

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


def get_new_slug(name):
slug = (
name.lower()
.replace("& ", "")
.replace(",", "")
.replace("/", "-")
.replace(" ", "-")
)
count = get_count(EventType.query.filter_by(slug=slug))
if count == 0:
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)


class EventType(SoftDeletionModel):
"""Event type object table"""

Expand All @@ -32,7 +15,7 @@ class EventType(SoftDeletionModel):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.slug = get_new_slug(name=self.name)
self.slug = get_new_slug(EventType, self.name)

def __repr__(self):
return '<EventType %r>' % self.name

0 comments on commit 069d8f8

Please sign in to comment.