Skip to content
This repository has been archived by the owner on Jun 30, 2019. It is now read-only.

Commit

Permalink
修复 display_property 严重的实现错误
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Dec 29, 2018
1 parent 94fc6e4 commit db4b4bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions fuocore/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ def __init__(self,
class display_property:
"""Model 的展示字段的描述器"""
def __init__(self, name):
#: display 属性对应的真正属性的名字
self.name_real = name
self.name_display = name + '_display'
self.value_display = ""
#: 用来存储值的属性名
self.store_pname = '_display_store_' + name

def __get__(self, instance, _=None):
if instance.stage >= ModelStage.inited:
return getattr(instance, self.name_real)
return self.value_display
return getattr(instance, self.store_pname, '')

def __set__(self, instance, value):
self.value_display = value
setattr(instance, self.store_pname, value)


class ModelMeta(type):
Expand Down
16 changes: 15 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import namedtuple
from unittest import TestCase

from fuocore.models import Model, BaseModel
from fuocore.models import Model, BaseModel, display_property


class FakeProvider:
Expand Down Expand Up @@ -73,3 +73,17 @@ def album_name(self):
song.album = Album(real_album_name)
song.use_display = False
self.assertEqual(song.album_name, real_album_name)


class TestDisplayProperty(TestCase):
def test_display_basic_usage(self):
class A:
stage = 4
a_display = display_property('a')

a1 = A()
a2 = A()
self.assertEqual(a1.a_display, '')
a2.a_display = 'a2'
self.assertEqual(a1.a_display, '')
self.assertEqual(a2.a_display, 'a2')

0 comments on commit db4b4bc

Please sign in to comment.