default.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import unicode_literals
  2. import re
  3. from django.forms import ValidationError
  4. from django.utils.translation import ugettext, ugettext_lazy as _
  5. from . import basefields
  6. class BioField(basefields.UrlifiedTextareaProfileField):
  7. fieldname = 'bio'
  8. label = _("Bio")
  9. class RealNameField(basefields.TextProfileField):
  10. fieldname = 'real_name'
  11. label = _("Real name")
  12. class LocationField(basefields.TextProfileField):
  13. fieldname = 'location'
  14. label = _("Location")
  15. class GenderField(basefields.ChoiceProfileField):
  16. fieldname = 'gender'
  17. label = _("Gender")
  18. choices = (
  19. ('', _('Not specified')),
  20. ('secret', _('Not telling')),
  21. ('female', _('Female')),
  22. ('male', _('Male')),
  23. )
  24. class WebsiteField(basefields.UrlProfileField):
  25. fieldname = 'website'
  26. label = _("Website")
  27. help_text = _(
  28. 'If you own website in the internet you wish to share on your profile '
  29. 'you may enter its address here. Remember to for it to be valid http '
  30. 'address starting with either "http://" or "https://".'
  31. )
  32. class SkypeIdField(basefields.TextProfileField):
  33. fieldname = 'skype'
  34. label = _("Skype ID")
  35. help_text = _(
  36. "Entering your Skype ID in this field may invite other users to contact you over "
  37. "the Skype instead of via private threads."
  38. )
  39. class TwitterHandleField(basefields.TextProfileField):
  40. fieldname = 'twitter'
  41. label = _("Twitter handle")
  42. def get_help_text(self, user):
  43. return _(
  44. 'If you own Twitter account, here you may enter your Twitter handle for other users '
  45. 'to find you. Starting your handle with "@" sign is optional. Either "@%(slug)s" or '
  46. '"%(slug)s" are valid values.'
  47. ) % {
  48. 'slug': user.slug
  49. }
  50. def get_value_display_data(self, request, user, value):
  51. return {
  52. 'text': '@{}'.format(value),
  53. 'url': 'https://twitter.com/{}'.format(value),
  54. }
  55. def clean(self, request, user, data):
  56. data = data.lstrip('@')
  57. if data and not re.search('^[A-Za-z0-9_]+$', data):
  58. raise ValidationError(ugettext("This is not a valid twitter handle."))
  59. return data
  60. class JoinIpField(basefields.TextProfileField):
  61. fieldname = 'join_ip'
  62. label = _("Join IP")
  63. readonly = True
  64. def get_value_display_data(self, request, user, value):
  65. if not request.user.acl_cache.get('can_see_users_ips'):
  66. return None
  67. if not user.joined_from_ip:
  68. return None
  69. return {
  70. 'text': user.joined_from_ip
  71. }