default.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import re
  2. from django.forms import ValidationError
  3. from django.utils.translation import gettext
  4. from django.utils.translation import gettext_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 "
  37. "contact you over 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 "
  45. 'other users to find you. Starting your handle with "@" sign is optional. '
  46. 'Either "@%(slug)s" or "%(slug)s" are valid values.'
  47. ) % {"slug": user.slug}
  48. def get_value_display_data(self, request, user, value):
  49. return {"text": "@%s" % value, "url": "https://twitter.com/%s" % value}
  50. def clean(self, request, user, data):
  51. data = data.lstrip("@")
  52. if data and not re.search("^[A-Za-z0-9_]+$", data):
  53. raise ValidationError(gettext("This is not a valid twitter handle."))
  54. return data
  55. class JoinIpField(basefields.TextProfileField):
  56. fieldname = "join_ip"
  57. label = _("Join IP")
  58. readonly = True
  59. def get_value_display_data(self, request, user, value):
  60. if not request.user_acl.get("can_see_users_ips"):
  61. return None
  62. if not user.joined_from_ip:
  63. return None
  64. return {"text": user.joined_from_ip}