Files
2012-02-21 01:15:00 -05:00

1 line
6.9 KiB
JSON

[{"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1305150646.9171071, "message": "We proxy a couple methods on to the User/AnonymousUser classes, mostly because we like the organizational structure of models, and we dont want to have more \"shortcut\" libraries. The current way we do this seems to cause a few problems, and I'm curious if anyone else has come up w/ a better solution", "group_id": 81, "id": 1015118}, {"user_id": 1127, "stars": [{"date_created": 1305568781.283447, "user_id": 960}], "topic_id": 34840, "date_created": 1305150658.6237521, "message": "class MethodDescriptor(object):\n \"\"\"\n Attaches a method to an existing class.\n\n >> MethodDescriptor.add_to_class(User, my_function[, name='not_my_function'])\n \"\"\"\n def __init__(self, func):\n self.func = func\n\n def contribute_to_class(self, cls, name):\n setattr(cls, name, self.func)\n\n @classmethod\n def add_to_class(cls, model, func, name=None):\n if not name:\n name = func.__name__\n model.add_to_class(name, cls(func))", "group_id": 81, "id": 1015121}, {"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1305150692.6530521, "message": "Sometimes, for whatever reason, User.is_disqus_admin doesn't exist. We've got this placed in the first app listed in INSTALLED_APPS, in its models.py file (its actually all this app even contains).", "group_id": 81, "id": 1015130}, {"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1305150666.253238, "message": "def is_disqus_admin(user):\n \"\"\"Our internal version of is_superuser, which relies on ``DISQUS_ADMIN_USERNAMES``\"\"\"\n return user.username in settings.DISQUS_ADMIN_USERNAMES\nMethodDescriptor.add_to_class(User, is_disqus_admin)\n", "group_id": 81, "id": 1015124}, {"user_id": 2588, "stars": [], "topic_id": 34840, "date_created": 1305155921.119802, "message": "Does it make any difference if you try it in a class_prepared receiver?", "group_id": 81, "id": 1015957}, {"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1305175150.8271191, "message": "@unbracketed will try that out tomorrow -- I feel like I tested it before and it didn't help. we literally have _auth at the top of our INSTALLED_APPS, and the models.py injects it straight away. Figured that was the safest/most efficient way to do it but it doesnt seem to always exist", "group_id": 81, "id": 1019747}, {"user_id": 5436, "stars": [], "topic_id": 34840, "date_created": 1305185916.6187651, "message": "@zeeg perhaps that's a reason not to mess with the User class :) (personally not a fan of changing the User/AnonymousUser classes at all)", "group_id": 81, "id": 1021540}, {"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1305189767.3337171, "message": "@peterbe I dont agree with the design of User, why should I use it the way it's implemented (granted, it may get to a point that we proxy out the entire object)", "group_id": 81, "id": 1022424}, {"user_id": 22109, "stars": [{"date_created": 1305221968.2437301, "user_id": 1127}, {"date_created": 1305479689.4906509, "user_id": 14653}, {"date_created": 1305480021.565295, "user_id": 21184}, {"date_created": 1305568689.5412171, "user_id": 960}, {"date_created": 1305620144.022531, "user_id": 26925}], "topic_id": 34840, "date_created": 1305208281.2826719, "message": "You could implement a custom auth backend, monkeypatch User in that module, and return instances of the modified class from the backend's authenticate method. I've done this before to change User.check_password to support Joomla-style passwords.", "group_id": 81, "id": 1024636}, {"user_id": 13325, "stars": [], "topic_id": 34840, "date_created": 1305220543.144084, "message": "What @crisisking says is a good way to go about it. I've seen it in use before.", "group_id": 81, "id": 1026509}, {"user_id": 1127, "stars": [{"date_created": 1305320452.2018061, "user_id": 141}], "topic_id": 34840, "date_created": 1305226602.573334, "message": "Problem solved, urls.py FTW :)", "group_id": 81, "id": 1028066}, {"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1305226603.6945641, "message": "from disqus.conf.urls.default import *\nfrom django.conf import settings\nfrom disqus.common.views import direct_to_template\n\n# Force import of our patched User/AnonymousUser models\nimport disqus._auth.models\n\nimport gargoyle\n\ngargoyle.autodiscover()", "group_id": 81, "id": 1028067}, {"user_id": 14653, "stars": [], "topic_id": 34840, "date_created": 1305479685.473119, "message": "@zeeg sounds like a good topic for writing a blog article. Many Django coders believe doing this is evil or dangerous. Anyways what @crisisking mentions is probably the de facto way of doing it :/", "group_id": 81, "id": 1061140}, {"user_id": 13325, "stars": [], "topic_id": 34840, "date_created": 1305548057.56373, "message": "I wish the Django documentation had a page on load order.", "group_id": 81, "id": 1069923}, {"user_id": 2116, "stars": [], "topic_id": 34840, "date_created": 1306246572.868221, "message": "Any hint about how good/bad is it ?", "group_id": 81, "id": 1166337}, {"user_id": 2116, "stars": [], "topic_id": 34840, "date_created": 1306246419.8239231, "message": "@zeeg, about adding method to an existing class, here's what i see in some of our code :", "group_id": 81, "id": 1166299}, {"user_id": 2116, "stars": [], "topic_id": 34840, "date_created": 1306246497.6468861, "message": "class UserExtension(object):\n def is_disqus_admin(self):\n \"\"\"Our internal version of is_superuser, which relies on ``DISQUS_ADMIN_USERNAMES``\"\"\"\n return self.username in settings.DISQUS_ADMIN_USERNAMES\nUser.__bases__ = ( UserExtension, ) + User.__bases__", "group_id": 81, "id": 1166311}, {"user_id": 1127, "stars": [], "topic_id": 34840, "date_created": 1306261103.897423, "message": "@zyegfryed I'm actually not sure (didn't even realize you could modify bases after creation)", "group_id": 81, "id": 1168905}, {"user_id": 3354, "stars": [], "topic_id": 34840, "date_created": 1306261487.644697, "message": "You can even modify __class__ after creation:\n\n\n>>> class C1:\n... pass\n... \n>>> class C2:\n... pass\n... \n>>> c = C1()\n>>> c\n<__main__.C1 object at 0x1005a2d50>\n>>> c.__class__\n<class '__main__.C1'>\n>>> c.__class__ = C2\n>>> c\n<__main__.C2 object at 0x1005a2d50>\n>>> \n\nNot something you should do every day, though ...", "group_id": 81, "id": 1169011}, {"user_id": 3354, "stars": [], "topic_id": 34840, "date_created": 1306261521.2553151, "message": "That was Python 3, btw ... though it works in Py2 as well.", "group_id": 81, "id": 1169016}, {"user_id": 6894, "stars": [], "topic_id": 34840, "date_created": 1306363940.1444941, "message": "modifying __bases__ ? It looks smelly, but it's pretty okay. Use with caution obviously, but I've used it in a few places where it's really the only way forward (including adding methods to django's auth models). Works fine", "group_id": 81, "id": 1185160}]