diff --git a/evennia/accounts/tests.py b/evennia/accounts/tests.py index 2f2d6cc7eb..81ac25c2a3 100644 --- a/evennia/accounts/tests.py +++ b/evennia/accounts/tests.py @@ -6,14 +6,12 @@ from unittest import TestCase from django.test import override_settings from evennia.accounts.accounts import AccountSessionHandler + from evennia.accounts.accounts import DefaultAccount, DefaultGuest from evennia.server.session import Session -from evennia.utils.test_resources import EvenniaTest from evennia.utils import create from evennia.utils.test_resources import EvenniaTest -from django.conf import settings - class TestAccountSessionHandler(TestCase): "Check AccountSessionHandler class" @@ -136,6 +134,19 @@ class TestDefaultAccountAuth(EvenniaTest): result, error = DefaultAccount.validate_username('xx') self.assertFalse(result, "2-character username passed validation.") + 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.web_get_detail_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.web_get_admin_url()) + self.assertTrue(self.account.web_get_admin_url() != '#') + def test_password_validation(self): "Check password validators deny bad passwords" @@ -274,17 +285,18 @@ class TestDefaultAccount(TestCase): class TestAccountPuppetDeletion(EvenniaTest): - + @override_settings(MULTISESSION_MODE=2) def test_puppet_deletion(self): # Check for existing chars self.assertFalse(self.account.db._playable_characters, 'Account should not have any chars by default.') - + # Add char1 to account's playable characters self.account.db._playable_characters.append(self.char1) self.assertTrue(self.account.db._playable_characters, 'Char was not added to account.') - + # See what happens when we delete char1. self.char1.delete() # Playable char list should be empty. - self.assertFalse(self.account.db._playable_characters, 'Playable character list is not empty! %s' % self.account.db._playable_characters) \ No newline at end of file + self.assertFalse(self.account.db._playable_characters, + 'Playable character list is not empty! %s' % self.account.db._playable_characters) diff --git a/evennia/objects/objects.py b/evennia/objects/objects.py index a863a78cea..b829ab7165 100644 --- a/evennia/objects/objects.py +++ b/evennia/objects/objects.py @@ -219,7 +219,7 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)): @property def is_connected(self): # we get an error for objects subscribed to channels without this - if self.account: # seems sane to pass on the account + if self.account: # seems sane to pass on the account return self.account.is_connected else: return False @@ -985,7 +985,7 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)): self.account.db._playable_characters = [x for x in self.account.db._playable_characters if x != self] for session in self.sessions.all(): self.account.unpuppet_object(session) - + self.account = None for script in _ScriptDB.objects.get_all_scripts_on_obj(self): diff --git a/evennia/objects/tests.py b/evennia/objects/tests.py index 144ac5a678..cb999bc616 100644 --- a/evennia/objects/tests.py +++ b/evennia/objects/tests.py @@ -1,9 +1,8 @@ - from evennia.utils.test_resources import EvenniaTest from evennia import DefaultObject, DefaultCharacter, DefaultRoom, DefaultExit -class ObjectCreationTest(EvenniaTest): - +class DefaultObjectTest(EvenniaTest): + ip = '212.216.139.14' def test_object_create(self): @@ -24,7 +23,7 @@ class ObjectCreationTest(EvenniaTest): def test_room_create(self): description = 'A dimly-lit alley behind the local Chinese restaurant.' - obj, errors = DefaultRoom.create('oscar', self.account, description=description, ip=self.ip) + obj, errors = DefaultRoom.create('alley', self.account, description=description, ip=self.ip) self.assertTrue(obj, errors) self.assertFalse(errors, errors) self.assertEqual(description, obj.db.desc) @@ -37,5 +36,11 @@ class ObjectCreationTest(EvenniaTest): self.assertFalse(errors, errors) self.assertEqual(description, obj.db.desc) self.assertEqual(obj.db.creator_ip, self.ip) - - \ No newline at end of file + + def test_urls(self): + "Make sure objects are returning URLs" + self.assertTrue(self.char1.get_absolute_url()) + self.assertTrue('admin' in self.char1.web_get_admin_url()) + + self.assertTrue(self.room1.get_absolute_url()) + self.assertTrue('admin' in self.room1.web_get_admin_url()) \ No newline at end of file diff --git a/evennia/typeclasses/models.py b/evennia/typeclasses/models.py index c156aa2ac6..61dddc0c80 100644 --- a/evennia/typeclasses/models.py +++ b/evennia/typeclasses/models.py @@ -31,9 +31,12 @@ from django.db.models import signals from django.db.models.base import ModelBase from django.db import models +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist from django.conf import settings +from django.urls import reverse from django.utils.encoding import smart_str +from django.utils.text import slugify from evennia.typeclasses.attributes import Attribute, AttributeHandler, NAttributeHandler from evennia.typeclasses.tags import Tag, TagHandler, AliasHandler, PermissionHandler @@ -733,3 +736,150 @@ class TypedObject(SharedMemoryModel): """ pass + + # + # Web/Django methods + # + + def web_get_admin_url(self): + """ + Returns the URI path for the Django Admin page for this object. + + ex. Account#1 = '/admin/accounts/accountdb/1/change/' + + Returns: + path (str): URI path to Django Admin page for object. + + """ + 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,)) + + @classmethod + def web_get_create_url(cls): + """ + Returns the URI path for a View that allows users to create new + instances of this object. + + ex. Chargen = '/characters/create/' + + For this to work, the developer must have defined a named view somewhere + in urls.py that follows the format 'modelname-action', so in this case + a named view of 'character-create' would be referenced by this method. + + ex. + url(r'characters/create/', ChargenView.as_view(), name='character-create') + + If no View has been created and defined in urls.py, returns an + HTML anchor. + + This method is naive and simply returns a path. Securing access to + the actual view and limiting who can create new objects is the + developer's responsibility. + + Returns: + path (str): URI path to object creation page, if defined. + + """ + try: + return reverse('%s-create' % cls._meta.verbose_name.lower()) + except: + return '#' + + def web_get_detail_url(self): + """ + Returns the URI path for a View that allows users to view details for + this object. + + ex. Oscar (Character) = '/characters/oscar/1/' + + For this to work, the developer must have defined a named view somewhere + in urls.py that follows the format 'modelname-action', so in this case + a named view of 'character-detail' would be referenced by this method. + + ex. + url(r'characters/(?P[\w\d\-]+)/(?P[0-9]+)/$', + CharDetailView.as_view(), name='character-detail') + + If no View has been created and defined in urls.py, returns an + HTML anchor. + + This method is naive and simply returns a path. Securing access to + the actual view and limiting who can view this object is the developer's + responsibility. + + Returns: + path (str): URI path to object detail page, if defined. + + """ + try: + return reverse('%s-detail' % self._meta.verbose_name.lower(), + kwargs={'pk': self.pk, 'slug': slugify(self.name)}) + except: + return '#' + + def web_get_update_url(self): + """ + Returns the URI path for a View that allows users to update this + object. + + ex. Oscar (Character) = '/characters/oscar/1/change/' + + For this to work, the developer must have defined a named view somewhere + in urls.py that follows the format 'modelname-action', so in this case + a named view of 'character-update' would be referenced by this method. + + ex. + url(r'characters/(?P[\w\d\-]+)/(?P[0-9]+)/change/$', + CharUpdateView.as_view(), name='character-update') + + If no View has been created and defined in urls.py, returns an + HTML anchor. + + This method is naive and simply returns a path. Securing access to + the actual view and limiting who can modify objects is the developer's + responsibility. + + Returns: + path (str): URI path to object update page, if defined. + + """ + try: + return reverse('%s-update' % self._meta.verbose_name.lower(), + kwargs={'pk': self.pk, 'slug': slugify(self.name)}) + except: + return '#' + + def web_get_delete_url(self): + """ + Returns the URI path for a View that allows users to delete this object. + + ex. Oscar (Character) = '/characters/oscar/1/delete/' + + For this to work, the developer must have defined a named view somewhere + in urls.py that follows the format 'modelname-action', so in this case + a named view of 'character-detail' would be referenced by this method. + + ex. + url(r'characters/(?P[\w\d\-]+)/(?P[0-9]+)/delete/$', + CharDeleteView.as_view(), name='character-delete') + + If no View has been created and defined in urls.py, returns an + HTML anchor. + + This method is naive and simply returns a path. Securing access to + the actual view and limiting who can delete this object is the developer's + responsibility. + + Returns: + path (str): URI path to object deletion page, if defined. + + """ + try: + return reverse('%s-delete' % self._meta.verbose_name.lower(), + kwargs={'pk': self.pk, 'slug': slugify(self.name)}) + except: + return '#' + + # Used by Django Sites/Admin + get_absolute_url = web_get_detail_url