Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring permalink resolution

  • Loading branch information...
commit 470f7e0267b285494cf944b9c57342c4f4828d30 1 parent 2437b0c
@acasimiro acasimiro authored
View
13 mootiro_komoo/apps/discussion/views.py
@@ -22,23 +22,14 @@
from investment.models import Investment
from komoo_project.models import Project
from .forms import DiscussionForm
+from main.views import ENTITY_MODEL
logger = logging.getLogger(__name__)
def _discussion_for (identifier):
- # TODO: move this perm_* id interpretation to a centralized place
- ent_model = {
- 'c': Community,
- 'n': Need,
- 'p': Proposal,
- 'r': Resource,
- 'o': Organization,
- 'i': Investment,
- 'j': Project,
- }
ent, id_ = identifier[0], identifier[1:]
- ent_content_type = ContentType.objects.get_for_model(ent_model[ent])
+ ent_content_type = ContentType.objects.get_for_model(ENTITY_MODEL[ent])
discussion = get_object_or_None(Discussion, object_id=id_,
content_type=ent_content_type)
View
3  mootiro_komoo/apps/main/utils.py
@@ -46,7 +46,8 @@ def slugify(term, slug_exists=lambda s: False):
def komoo_permalink(obj):
- return '/permalink/{}{}'.format(obj.__class__.__name__.lower()[0], obj.id)
+ from main.views import ENTITY_MODEL_REV
+ return '/permalink/{}{}'.format(ENTITY_MODEL_REV[obj.__class__], obj.id)
def create_geojson(objects, type_='FeatureCollection', convert=True, discard_empty=False):
View
39 mootiro_komoo/apps/main/views.py
@@ -23,14 +23,29 @@
from community.models import Community
from need.models import Need
-from komoo_resource.models import Resource
-from organization.models import OrganizationBranch, Organization
from proposal.models import Proposal
+from organization.models import OrganizationBranch, Organization
+from komoo_resource.models import Resource
+from investment.models import Investment
+from komoo_project.models import Project
from main.utils import create_geojson
logger = logging.getLogger(__name__)
+ENTITY_MODEL = {
+ 'c': Community,
+ 'n': Need,
+ 'p': Proposal,
+ 'o': Organization,
+ 'b': OrganizationBranch,
+ 'r': Resource,
+ 'i': Investment,
+ 'j': Project,
+}
+ENTITY_MODEL_REV = {v:k for k, v in ENTITY_MODEL.items()}
+
+
@render_to('main/root.html')
def root(request):
logger.debug('acessing Root')
@@ -257,18 +272,10 @@ def custom_500(request):
@render_to('not_anymore.html')
def permalink(request, identifier=''):
- entity_model = {
- 'r': Resource,
- 'n': Need,
- 'c': Community,
- 'o': Organization,
- 'p': Proposal,
- 'u': User,
- }
url = 'root'
if identifier:
entity, id_ = identifier[0], identifier[1:]
- obj = get_object_or_None(entity_model[entity], pk=id_)
+ obj = get_object_or_None(ENTITY_MODEL[entity], pk=id_)
if not obj:
return {}
url = getattr(obj, 'view_url', '/') if entity != 'u' \
@@ -278,17 +285,9 @@ def permalink(request, identifier=''):
@ajax_request
def get_geojson_from_hashlink(request):
- entity_model = {
- 'r': Resource,
- 'n': Need,
- 'c': Community,
- 'o': Organization,
- 'p': Proposal,
- 'b': OrganizationBranch,
- }
hashlink = request.GET.get('hashlink', '')
if hashlink:
- obj = entity_model[hashlink[0]].objects.get(pk=hashlink[1:])
+ obj = ENTITY_MODEL[hashlink[0]].objects.get(pk=hashlink[1:])
geojson = create_geojson([obj])
else:
geojson = {}
Please sign in to comment.
Something went wrong with that request. Please try again.