本文整理汇总了Python中jmbo.models.ModelBase类的典型用法代码示例。如果您正苦于以下问题:Python ModelBase类的具体用法?Python ModelBase怎么用?Python ModelBase使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelBase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LocationAwarenessTestCase
class LocationAwarenessTestCase(unittest.TestCase):
def setUp(self):
country = Country(name="South Africa", country_code="ZA")
country.save()
self.ct = City(
name="Cape Town",
country=country,
coordinates=fromstr('POINT(18.423218 -33.925839)', srid=4326)
)
self.ct.save()
loc1 = Location(
city=self.ct,
country=country,
coordinates=fromstr('POINT(18.41 -33.91)', srid=4326),
name='loc1'
)
loc1.save()
self.model = ModelBase(title="title1", location=loc1)
self.model.save()
def test_distance_calculation(self):
qs = ModelBase.objects.distance(self.ct.coordinates)
for obj in qs:
if obj.distance is not None:
self.assertEqual(obj.location.coordinates.distance(self.ct.coordinates), obj.distance)
示例2: test_save_model
def test_save_model(self):
# setup mock objects
admin_obj = ModelBaseAdmin(ModelBase, 1)
request = RequestFactory()
request.user = self.user
# After admin save the object's owner should be the current user.
obj = ModelBase()
admin_obj.save_model(request, obj, admin_obj.form, 1)
self.failUnless(obj.owner == self.user)
obj.save()
示例3: test_comment_count
def test_comment_count(self):
comment_model = comments.get_model()
# Return 0 if no comments exist.
obj = ModelBase()
obj.save()
self.failUnless(obj.comment_count == 0)
# Return the number of comments if comments exist. Here it
# should be 1 since we've created 1 comment.
comment_obj = comment_model(content_object=obj, site_id=1)
comment_obj.save()
obj = ModelBase.objects.get(id=obj.id)
self.failUnless(obj.comment_count == 1)
# Return 0 if no comments exist.
dummy_obj = DummyModel()
dummy_obj.save()
dummy_obj = ModelBase.objects.get(id=dummy_obj.id)
self.failUnless(dummy_obj.comment_count == 0)
# Return the number of comments if comments exist on the
# ModelBase object. Here it should be 1 since we've created 1
# comment on the ModelBase object.
comment_obj = comment_model(
content_object=dummy_obj.modelbase_obj,
site_id=1
)
comment_obj.save()
dummy_obj = ModelBase.objects.get(id=dummy_obj.id)
self.failUnless(dummy_obj.modelbase_obj.comment_count == 1)
# If a comment was made on the ModelBase object it should
# still count for leaf class objects.
self.failUnless(dummy_obj.comment_count == 1)
# Add another comment on dummy object and make sure the count
# is 2 for both the dummy object and its modelbase object.
comment_obj = comment_model(content_object=dummy_obj, site_id=1)
comment_obj.save()
dummy_obj = ModelBase.objects.get(id=dummy_obj.id)
self.failUnless(dummy_obj.comment_count == 2)
self.failUnless(dummy_obj.modelbase_obj.comment_count == 2)
# There should now only be 3 comment objects.
self.failUnless(comment_model.objects.all().count() == 3)
示例4: test_vote_total
def test_vote_total(self):
# create object with some votes
obj = ModelBase(title='title')
obj.save()
obj.add_vote("token1", 1)
obj.add_vote("token2", -1)
obj.add_vote("token3", 1)
# vote_total should return an integer
result = obj.vote_total
self.failUnlessEqual(result.__class__, int)
# vote total is calculated as total_upvotes - total_downvotes
self.failUnlessEqual(result, 1)
示例5: setUp
def setUp(self):
country = Country(name="South Africa", country_code="ZA")
country.save()
self.ct = City(
name="Cape Town",
country=country,
coordinates=fromstr('POINT(18.423218 -33.925839)', srid=4326)
)
self.ct.save()
loc1 = Location(
city=self.ct,
country=country,
coordinates=fromstr('POINT(18.41 -33.91)', srid=4326),
name='loc1'
)
loc1.save()
self.model = ModelBase(title="title1", location=loc1)
self.model.save()
示例6: test_publish_retract
def test_publish_retract(self):
today = timezone.make_aware(timezone.datetime.today())
yesterday = today - timezone.timedelta(days=1)
tomorrow = today + timezone.timedelta(days=1)
p1 = ModelBase(title="title", state="published")
p1.save()
p1.sites.add(self.web_site)
p1.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failUnless(p1 in queryset)
p2 = ModelBase(title="title", publish_on=today)
p2.save()
p2.sites.add(self.web_site)
p2.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failUnless(p2 in queryset)
p4 = ModelBase(title="title", publish_on=today, retract_on=tomorrow)
p4.save()
p4.sites.add(self.web_site)
p4.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failUnless(p4 in queryset)
p5 = ModelBase(title="title", publish_on=tomorrow)
p5.save()
p5.sites.add(self.web_site)
p5.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failIf(p5 in queryset)
p6 = ModelBase(title="title", publish_on=yesterday, retract_on=today)
p6.save()
p6.sites.add(self.web_site)
p6.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failIf(p6 in queryset)
p7 = ModelBase(title="title", publish_on=tomorrow, retract_on=tomorrow)
p7.save()
p7.sites.add(self.web_site)
p7.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failIf(p7 in queryset)
p8 = ModelBase(
title="title", publish_on=yesterday, retract_on=yesterday
)
p8.save()
p8.sites.add(self.web_site)
p8.save()
jmbo_publish.Command().handle()
queryset = ModelBase.permitted.all()
self.failIf(p8 in queryset)
示例7: test_get_queryset
def test_get_queryset(self):
# create unpublished item
unpublished_obj = ModelBase(title="title", state="unpublished")
unpublished_obj.save()
unpublished_obj.sites.add(self.web_site)
unpublished_obj.save()
# create published item
published_obj = ModelBase(title="title", state="published")
published_obj.save()
published_obj.sites.add(self.web_site)
published_obj.save()
# unpublished objects should not be available in queryset
queryset = ModelBase.permitted.all()
self.failIf(unpublished_obj in queryset)
# published objects should always be available in queryset
self.failUnless(published_obj in queryset)
# queryset should only contain items for the current site
published_obj_web = ModelBase(state="published")
published_obj_web.save()
published_obj_web.sites.add(self.web_site)
published_obj_web.save()
queryset = ModelBase.permitted.all()
self.failUnless(published_obj_web in queryset)
# queryset should not contain items for other sites
published_obj_mobile = ModelBase(state="published")
published_obj_mobile.save()
published_obj_mobile.sites.add(self.mobile_site)
published_obj_mobile.save()
queryset = ModelBase.permitted.all()
self.failIf(published_obj_mobile in queryset)
# Publish to different layers. The current layer during this test run
# is "web".
with override_settings(LAYERS={"tree": ["basic", ["web"]], "current": "web"}):
call_command("load_layers")
obj_layer_basic = ModelBase.objects.create(state="published")
obj_layer_basic.sites = Site.objects.all()
obj_layer_basic.layers = [Layer.objects.get(name="basic")]
obj_layer_basic.save()
obj_layer_web = ModelBase.objects.create(state="published")
obj_layer_web.sites = Site.objects.all()
obj_layer_web.layers = [Layer.objects.get(name="web")]
obj_layer_web.save()
queryset = ModelBase.permitted.all()
self.failIf(obj_layer_basic in queryset)
self.failUnless(obj_layer_web in queryset)
示例8: test_get_query_set
def test_get_query_set(self):
# create unpublished item
unpublished_obj = ModelBase(title='title', state='unpublished')
unpublished_obj.save()
unpublished_obj.sites.add(self.web_site)
unpublished_obj.save()
# create published item
published_obj = ModelBase(title='title', state='published')
published_obj.save()
published_obj.sites.add(self.web_site)
published_obj.save()
# create staging item
staging_obj = ModelBase(title='title', state='staging')
staging_obj.save()
staging_obj.sites.add(self.web_site)
staging_obj.save()
# unpublished objects should not be available in queryset
queryset = ModelBase.permitted.all()
self.failIf(unpublished_obj in queryset)
# published objects should always be available in queryset
self.failUnless(published_obj in queryset)
# Staging objects should only be available on instances
# that define settings.STAGING = True.
from django.conf import settings
settings.STAGING = False
queryset = ModelBase.permitted.all()
self.failIf(staging_obj in queryset)
settings.STAGING = True
queryset = ModelBase.permitted.all()
self.failUnless(staging_obj in queryset)
# queryset should only contain items for the current site
published_obj_web = ModelBase(state='published')
published_obj_web.save()
published_obj_web.sites.add(self.web_site)
published_obj_web.save()
queryset = ModelBase.permitted.all()
self.failUnless(published_obj_web in queryset)
# queryset should not contain items for other sites
mobile_site = Site(id=101, domain="mobi.address.com")
mobile_site.save()
published_obj_mobile = ModelBase(state='published')
published_obj_mobile.save()
published_obj_mobile.sites.add(mobile_site)
published_obj_mobile.save()
queryset = ModelBase.permitted.all()
self.failIf(published_obj_mobile in queryset)
示例9: test_can_comment
def test_can_comment(self):
# create dummy request object
request = type('Request', (object,), {})
class User():
def is_authenticated(self):
return False
request.user = User()
request.secretballot_token = 'test_token'
# return false when commenting is closed
obj = ModelBase(
comments_enabled=True,
comments_closed=True,
anonymous_comments=True
)
obj.save()
self.failIf(obj.can_comment(request)[0])
# return false when commenting is disabled
obj = ModelBase(
comments_enabled=False,
comments_closed=False,
anonymous_comments=True
)
obj.save()
self.failIf(obj.can_comment(request)[0])
# return false if anonymous and anonymous commenting is disabled
obj = ModelBase(
comments_enabled=True,
comments_closed=False,
anonymous_comments=False
)
obj.save()
self.failIf(obj.can_comment(request)[0])
# return true if anonymous and anonymous commenting is enabled
obj = ModelBase(
comments_enabled=True,
comments_closed=False,
anonymous_comments=True
)
obj.save()
self.failUnless(obj.can_comment(request)[0])
示例10: test_can_vote
def test_can_vote(self):
# create dummy request object
request = type('Request', (object,), {})
class User():
def is_authenticated(self):
return False
request.user = User()
request.secretballot_token = 'test_token'
# return false when liking is closed
obj = ModelBase(
likes_enabled=True,
likes_closed=True,
anonymous_likes=True
)
obj.save()
self.failIf(obj.can_vote(request)[0])
# return false when liking is disabled
obj = ModelBase(
likes_enabled=False,
likes_closed=False,
anonymous_likes=True
)
obj.save()
self.failIf(obj.can_vote(request)[0])
# return false if anonymous and anonymous liking is disabled
obj = ModelBase(
likes_enabled=True,
likes_closed=False,
anonymous_likes=False
)
obj.save()
self.failIf(obj.can_vote(request)[0])
# return true if anonymous and anonymous liking is enabled
obj = ModelBase(
likes_enabled=True,
likes_closed=False,
anonymous_likes=True
)
obj.save()
self.failUnless(obj.can_vote(request))
# return false if vote already exist
content_type = ContentType.objects.get(
app_label="jmbo",
model="modelbase"
)
Vote.objects.create(
object_id=obj.id,
token='test_token',
content_type=content_type, vote=1
)
self.failIf(obj.can_vote(request)[0])
示例11: test_is_permitted
def test_is_permitted(self):
# create unpublished item
unpublished_obj = ModelBase(title='title', state='unpublished')
unpublished_obj.save()
unpublished_obj.sites.add(self.web_site)
unpublished_obj.save()
# create published item
published_obj = ModelBase(title='title', state='published')
published_obj.save()
published_obj.sites.add(self.web_site)
published_obj.save()
# create staging item
staging_obj = ModelBase(title='title', state='staging')
staging_obj.save()
staging_obj.sites.add(self.web_site)
staging_obj.save()
# is_permitted should be False for unpublished objects
self.failIf(unpublished_obj.is_permitted)
# is_permitted should be True for published objects
self.failUnless(published_obj.is_permitted)
# Is_permitted should be True for otherwise published objects in
# the staging state for instances that define settings.STAGING = True.
from django.conf import settings
settings.STAGING = False
self.failIf(staging_obj.is_permitted)
settings.STAGING = True
self.failUnless(staging_obj.is_permitted)
# Is_permitted should be True only if the object is
# published for the current site.
published_obj_web = ModelBase(state='published')
published_obj_web.save()
published_obj_web.sites.add(self.web_site)
published_obj_web.save()
self.failUnless(published_obj_web.is_permitted)
# Is_permitted should be False if the object is
# not published for the current site.
mobile_site = Site(id=100, domain="mobi.address.com")
mobile_site.save()
published_obj_mobile = ModelBase(state='published')
published_obj_mobile.save()
published_obj_mobile.sites.add(mobile_site)
published_obj_mobile.save()
self.failIf(published_obj_mobile.is_permitted)
示例12: test_unique_slugs
def test_unique_slugs(self):
# create 2 sites
site_1 = Site(id=201, domain="site1.example.com")
site_1.save()
site_2 = Site(id=202, domain="site2.example.com")
site_2.save()
# Create an object for site 1
obj_1 = ModelBase(title='object for site 1')
obj_1.save()
obj_1.sites.add(site_1)
obj_1.slug = 'generic_slug'
obj_1.save()
# Create an object for site 2
obj_2 = ModelBase(title='object for site 2')
obj_2.save()
obj_2.sites.add(site_2)
obj_2.slug = 'generic_slug'
obj_2.save()
# Trying to add site_1 should raise an error.
with self.assertRaises(RuntimeError):
obj_2.sites.add(site_1)
obj_2.save()
# When the slugs differ, you can add site_1.
obj_2.slug = 'generic_slug_2'
obj_2.sites.add(site_1)
obj_2.save()
# Trying to change the slug to an existing one should raise an error.
with self.assertRaises(RuntimeError):
obj_2.slug = 'generic_slug'
obj_2.save()
示例13: test_save
def test_save(self):
before_save = timezone.now()
# created field should be set on save
obj = ModelBase(title='title')
obj.save()
# created field should be set to current datetime on save
after_save = timezone.now()
self.failIf(obj.created > after_save or obj.created < before_save)
# If a user supplies a created date use that
# instead of the current datetime.
test_datetime = datetime(2008, 10, 10, 12, 12)
obj = ModelBase(title='title', created=test_datetime)
obj.save()
self.failIf(obj.created != test_datetime)
# modified should be set to current datetime on each save
before_save = timezone.now()
obj = ModelBase(title='title')
obj.save()
after_save = timezone.now()
self.failIf(obj.modified > after_save or obj.modified < before_save)
# leaf class content type should be set on save
obj = DummyModel(title='title')
obj.save()
self.failUnless(obj.content_type == ContentType.objects.\
get_for_model(DummyModel))
# leaf class class name should be set on save
self.failUnless(obj.class_name == DummyModel.__name__)
# Correct leaf class content type should be retained
# over base class' content type.
base = obj.modelbase_ptr
base.save()
self.failUnless(base.content_type == ContentType.objects.\
get_for_model(DummyModel))
# Correct leaf class class name should be
# retained over base class' class name.
self.failUnless(base.class_name == DummyModel.__name__)
示例14: test_generate_slug
def test_generate_slug(self):
# on save a slug should be set
obj = ModelBase(title='utils test case title')
obj.save()
self.failIf(obj.slug == '')
# slug should become sluggified version of title
obj = ModelBase(title='utils test case title 1')
obj.save()
self.failUnless(obj.slug == slugify(obj.title))
# no two items should have the same slug
obj = ModelBase(title='utils test case title 1')
obj.save()
# in case an object title is updated, the slug not be updated
obj.title = "updated title"
obj.save()
self.failIf(obj.slug == slugify(obj.title))
# In case an object is updated, without the title being changed
# the slug should remain unchanged.
orig_slug = obj.slug
obj.save()
self.failUnless(obj.slug == orig_slug)
# make sure the slug is actually saved
obj = ModelBase.objects.get(id=obj.id)
self.failIf(obj.slug == '')
# Empty slugs might trip up regex query.
obj = ModelBase()
obj.save()
obj = ModelBase()
obj.save()
obj = ModelBase()
obj.save()
示例15: test_is_permitted
def test_is_permitted(self):
# create unpublished item
unpublished_obj = ModelBase(title="title", state="unpublished")
unpublished_obj.save()
unpublished_obj.sites.add(self.web_site)
unpublished_obj.save()
# create published item
published_obj = ModelBase(title="title", state="published")
published_obj.save()
published_obj.sites.add(self.web_site)
published_obj.save()
# is_permitted should be False for unpublished objects
self.failIf(unpublished_obj.is_permitted)
# is_permitted should be True for published objects
self.failUnless(published_obj.is_permitted)
# Is_permitted should be True only if the object is
# published for the current site.
published_obj_web = ModelBase(state="published")
published_obj_web.save()
published_obj_web.sites.add(self.web_site)
published_obj_web.save()
self.failUnless(published_obj_web.is_permitted)
# Is_permitted should be False if the object is
# not published for the current site.
published_obj_mobile = ModelBase(state="published")
published_obj_mobile.save()
published_obj_mobile.sites.add(self.mobile_site)
published_obj_mobile.save()
self.failIf(published_obj_mobile.is_permitted)