default.py 2.5 KB

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