Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

compat.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from django.conf import settings
  2. from django.contrib.auth import get_user_model as django_get_user_model
  3. from django.contrib.auth.models import User
  4. from django.core.exceptions import ImproperlyConfigured
  5. def get_user_model():
  6. """
  7. Return the User model.
  8. This used to live in compat to support both Django 1.4's fixed User model
  9. and custom user models introduced thereafter.
  10. Support for Django 1.4 has since been dropped in Oscar, but our
  11. get_user_model remains because code relies on us annotating the _meta class
  12. with the additional fields, and other code might rely on it as well.
  13. """
  14. model = django_get_user_model()
  15. # Test if user model has any custom fields and add attributes to the _meta
  16. # class
  17. core_fields = set([f.name for f in User._meta.fields])
  18. model_fields = set([f.name for f in model._meta.fields])
  19. new_fields = model_fields.difference(core_fields)
  20. model._meta.has_additional_fields = len(new_fields) > 0
  21. model._meta.additional_fields = new_fields
  22. return model
  23. # A setting that can be used in foreign key declarations
  24. AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
  25. # Two additional settings that are useful in South migrations when
  26. # specifying the user model in the FakeORM
  27. try:
  28. AUTH_USER_APP_LABEL, AUTH_USER_MODEL_NAME = AUTH_USER_MODEL.rsplit('.', 1)
  29. except ValueError:
  30. raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form"
  31. " 'app_label.model_name'")
  32. def existing_user_fields(fields):
  33. """
  34. Starting with Django 1.6, the User model can be overridden and it is no
  35. longer safe to assume the User model has certain fields. This helper
  36. function assists in writing portable forms Meta.fields definitions
  37. when those contain fields on the User model
  38. Usage:
  39. class UserForm(forms.Form):
  40. ...
  41. class Meta:
  42. # won't break if first_name is not defined on User model
  43. fields = existing_user_fields(['first_name', 'last_name'])
  44. """
  45. user_fields = get_user_model()._meta.fields
  46. user_field_names = [field.name for field in user_fields]
  47. return list(set(fields) & set(user_field_names))
  48. # Python3 compatibility layer
  49. # Make backwards-compatible atomic decorator available
  50. try:
  51. from django.db.transaction import atomic as atomic_compat
  52. except ImportError:
  53. from django.db.transaction import commit_on_success as atomic_compat
  54. atomic_compat = atomic_compat
  55. try:
  56. import urlparse as _urlparse
  57. except ImportError:
  58. from urllib import parse as _urlparse # NOQA
  59. urlparse = _urlparse
  60. #
  61. # Unicode compatible wrapper for CSV reader and writer that abstracts away
  62. # differences between Python 2 and 3. A package like unicodecsv would be
  63. # preferable, but it's not Python 3 compatible yet.
  64. # Code from http://python3porting.com/problems.html
  65. # Classes renamed to include CSV. Unused 'codecs' import is dropped.
  66. import sys
  67. import csv
  68. PY3 = sys.version > '3'
  69. class UnicodeCSVReader:
  70. def __init__(self, filename, dialect=csv.excel,
  71. encoding="utf-8", **kw):
  72. self.filename = filename
  73. self.dialect = dialect
  74. self.encoding = encoding
  75. self.kw = kw
  76. def __enter__(self):
  77. if PY3:
  78. self.f = open(self.filename, 'rt',
  79. encoding=self.encoding, newline='')
  80. else:
  81. self.f = open(self.filename, 'rbU')
  82. self.reader = csv.reader(self.f, dialect=self.dialect,
  83. **self.kw)
  84. return self
  85. def __exit__(self, type, value, traceback):
  86. self.f.close()
  87. def next(self):
  88. row = next(self.reader)
  89. if PY3:
  90. return row
  91. return [s.decode("utf-8") for s in row]
  92. __next__ = next
  93. def __iter__(self):
  94. return self
  95. class UnicodeCSVWriter:
  96. def __init__(self, filename, dialect=csv.excel,
  97. encoding="utf-8", **kw):
  98. self.filename = filename
  99. self.dialect = dialect
  100. self.encoding = encoding
  101. self.kw = kw
  102. def __enter__(self):
  103. if PY3:
  104. self.f = open(self.filename, 'wt',
  105. encoding=self.encoding, newline='')
  106. else:
  107. self.f = open(self.filename, 'wb')
  108. self.writer = csv.writer(self.f, dialect=self.dialect,
  109. **self.kw)
  110. return self
  111. def __exit__(self, type, value, traceback):
  112. self.f.close()
  113. def writerow(self, row):
  114. if not PY3:
  115. row = [s.encode(self.encoding) for s in row]
  116. self.writer.writerow(row)
  117. def writerows(self, rows):
  118. for row in rows:
  119. self.writerow(row)