default.py 2.5 KB

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