Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

general.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from django.contrib.postgres.fields import JSONField
  2. from django.db.models.aggregates import Aggregate
  3. __all__ = [
  4. 'ArrayAgg', 'BitAnd', 'BitOr', 'BoolAnd', 'BoolOr', 'JSONBAgg', 'StringAgg',
  5. ]
  6. class ArrayAgg(Aggregate):
  7. function = 'ARRAY_AGG'
  8. template = '%(function)s(%(distinct)s%(expressions)s)'
  9. def __init__(self, expression, distinct=False, **extra):
  10. super().__init__(expression, distinct='DISTINCT ' if distinct else '', **extra)
  11. def convert_value(self, value, expression, connection):
  12. if not value:
  13. return []
  14. return value
  15. class BitAnd(Aggregate):
  16. function = 'BIT_AND'
  17. class BitOr(Aggregate):
  18. function = 'BIT_OR'
  19. class BoolAnd(Aggregate):
  20. function = 'BOOL_AND'
  21. class BoolOr(Aggregate):
  22. function = 'BOOL_OR'
  23. class JSONBAgg(Aggregate):
  24. function = 'JSONB_AGG'
  25. output_field = JSONField()
  26. def convert_value(self, value, expression, connection):
  27. if not value:
  28. return []
  29. return value
  30. class StringAgg(Aggregate):
  31. function = 'STRING_AGG'
  32. template = "%(function)s(%(distinct)s%(expressions)s, '%(delimiter)s')"
  33. def __init__(self, expression, delimiter, distinct=False, **extra):
  34. distinct = 'DISTINCT ' if distinct else ''
  35. super().__init__(expression, delimiter=delimiter, distinct=distinct, **extra)
  36. def convert_value(self, value, expression, connection):
  37. if not value:
  38. return ''
  39. return value