Adds get_absolute_url() and get_admin_url() methods to DefaultObject, DefaultCharacter, Account and DefaultRoom objects.

This commit is contained in:
Johnny 2018-10-05 22:02:20 +00:00
parent 9553acecb2
commit 95577487a7
4 changed files with 89 additions and 0 deletions

View file

@ -14,7 +14,9 @@ instead for most things).
import time import time
from django.conf import settings from django.conf import settings
from django.contrib.auth import password_validation from django.contrib.auth import password_validation
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.urls import reverse_lazy
from django.utils import timezone from django.utils import timezone
from evennia.typeclasses.models import TypeclassBase from evennia.typeclasses.models import TypeclassBase
from evennia.accounts.manager import AccountManager from evennia.accounts.manager import AccountManager
@ -190,6 +192,25 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
def sessions(self): def sessions(self):
return AccountSessionHandler(self) return AccountSessionHandler(self)
def get_absolute_url(self):
"""
Returns the canonical URL for an Account.
To callers, this method should appear to return a string that can be
used to refer to the object over HTTP.
https://docs.djangoproject.com/en/2.1/ref/models/instances/#get-absolute-url
"""
try: return reverse_lazy('account-detail', kwargs={'pk': self.pk, 'slug': slugify(self.name)})
except: return '#'
def get_admin_url(self):
"""
Returns a link to this object's entry within the Django Admin panel.
"""
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse_lazy("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(self.id,))
# session-related methods # session-related methods
def disconnect_session_from_account(self, session, reason=None): def disconnect_session_from_account(self, session, reason=None):

View file

@ -60,6 +60,19 @@ class TestDefaultAccount(TestCase):
self.s1.puppet = None self.s1.puppet = None
self.s1.sessid = 0 self.s1.sessid = 0
def test_absolute_url(self):
"Get URL for account detail page on website"
self.account = create.create_account("TestAccount%s" % randint(100000, 999999),
email="test@test.com", password="testpassword", typeclass=DefaultAccount)
self.assertTrue(self.account.get_absolute_url())
def test_admin_url(self):
"Get object's URL for access via Admin pane"
self.account = create.create_account("TestAccount%s" % randint(100000, 999999),
email="test@test.com", password="testpassword", typeclass=DefaultAccount)
self.assertTrue(self.account.get_admin_url())
self.assertTrue(self.account.get_admin_url() != '#')
def test_password_validation(self): def test_password_validation(self):
"Check password validators deny bad passwords" "Check password validators deny bad passwords"

View file

@ -12,6 +12,9 @@ from future.utils import with_metaclass
from collections import defaultdict from collections import defaultdict
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.text import slugify
from evennia.typeclasses.models import TypeclassBase from evennia.typeclasses.models import TypeclassBase
from evennia.typeclasses.attributes import NickHandler from evennia.typeclasses.attributes import NickHandler
@ -325,6 +328,25 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)):
self.aliases.add(singular, category="plural_key") self.aliases.add(singular, category="plural_key")
return singular, plural return singular, plural
def get_absolute_url(self):
"""
Returns the canonical URL for an object.
To callers, this method should appear to return a string that can be
used to refer to the object over HTTP.
https://docs.djangoproject.com/en/2.1/ref/models/instances/#get-absolute-url
"""
try: return reverse('object-detail', kwargs={'pk': self.pk, 'slug': slugify(self.name)})
except: return '#'
def get_admin_url(self):
"""
Returns a link to this object's entry within the Django Admin panel.
"""
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(self.id,))
def search(self, searchdata, def search(self, searchdata,
global_search=False, global_search=False,
use_nicks=True, use_nicks=True,
@ -1821,6 +1843,17 @@ class DefaultCharacter(DefaultObject):
a character avatar controlled by an account. a character avatar controlled by an account.
""" """
def get_absolute_url(self):
"""
Returns the canonical URL for a Character.
To callers, this method should appear to return a string that can be
used to refer to the object over HTTP.
https://docs.djangoproject.com/en/2.1/ref/models/instances/#get-absolute-url
"""
try: return reverse('character-detail', kwargs={'pk': self.pk, 'slug': slugify(self.name)})
except: return super(DefaultCharacter, self).get_absolute_url()
def basetype_setup(self): def basetype_setup(self):
""" """
@ -1938,6 +1971,17 @@ class DefaultRoom(DefaultObject):
This is the base room object. It's just like any Object except its This is the base room object. It's just like any Object except its
location is always `None`. location is always `None`.
""" """
def get_absolute_url(self):
"""
Returns the canonical URL for a Room.
To callers, this method should appear to return a string that can be
used to refer to the object over HTTP.
https://docs.djangoproject.com/en/2.1/ref/models/instances/#get-absolute-url
"""
try: return reverse('location-detail', kwargs={'pk': self.pk, 'slug': slugify(self.name)})
except: return super(DefaultRoom, self).get_absolute_url()
def basetype_setup(self): def basetype_setup(self):
""" """

11
evennia/objects/tests.py Normal file
View file

@ -0,0 +1,11 @@
from evennia.utils.test_resources import EvenniaTest
class DefaultObjectTest(EvenniaTest):
def test_urls(self):
"Make sure objects are returning URLs"
self.assertTrue(self.char1.get_absolute_url())
self.assertTrue('admin' in self.char1.get_admin_url())
self.assertTrue(self.room1.get_absolute_url())
self.assertTrue('admin' in self.room1.get_admin_url())