2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
from django.db import models
|
2012-09-19 19:39:34 +02:00
|
|
|
from django.conf import settings
|
2013-06-24 17:51:10 +02:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser, UserManager, \
|
|
|
|
PermissionsMixin
|
2014-01-28 18:18:19 +01:00
|
|
|
from zerver.lib.cache import cache_with_key, flush_user_profile, flush_realm, \
|
2013-04-05 00:13:03 +02:00
|
|
|
user_profile_by_id_cache_key, user_profile_by_email_cache_key, \
|
2014-01-28 20:49:55 +01:00
|
|
|
generic_bulk_cached_fetch, cache_set, flush_stream, \
|
2014-01-15 22:48:27 +01:00
|
|
|
display_recipient_cache_key, cache_delete, \
|
|
|
|
get_stream_cache_key, active_user_dicts_in_realm_cache_key
|
2013-08-08 16:51:18 +02:00
|
|
|
from zerver.lib.utils import make_safe_digest, generate_random_token
|
2013-08-09 19:15:44 +02:00
|
|
|
from django.db import transaction, IntegrityError
|
2013-09-21 16:32:29 +02:00
|
|
|
from zerver.lib.avatar import gravatar_hash, get_avatar_url
|
2012-10-20 18:02:58 +02:00
|
|
|
from django.utils import timezone
|
2012-10-29 19:43:00 +01:00
|
|
|
from django.contrib.sessions.models import Session
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2013-08-22 16:56:37 +02:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
2013-11-02 15:36:17 +01:00
|
|
|
from guardian.shortcuts import get_users_with_perms
|
2013-06-19 21:41:29 +02:00
|
|
|
import zlib
|
2012-09-21 16:10:36 +02:00
|
|
|
|
2013-03-12 17:51:55 +01:00
|
|
|
from bitfield import BitField
|
2013-09-13 23:33:11 +02:00
|
|
|
from collections import defaultdict
|
2013-07-08 17:53:50 +02:00
|
|
|
import pylibmc
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2013-09-17 22:31:05 +02:00
|
|
|
import logging
|
2013-03-12 17:51:55 +01:00
|
|
|
|
2013-08-09 19:15:44 +02:00
|
|
|
bugdown = None
|
|
|
|
|
2012-12-07 01:05:14 +01:00
|
|
|
MAX_SUBJECT_LENGTH = 60
|
2012-12-11 17:12:53 +01:00
|
|
|
MAX_MESSAGE_LENGTH = 10000
|
2012-12-07 01:05:14 +01:00
|
|
|
|
2013-08-26 18:09:17 +02:00
|
|
|
def is_super_user(user):
|
2013-11-12 23:22:08 +01:00
|
|
|
return user.email in settings.API_SUPER_USERS
|
2013-08-26 18:09:17 +02:00
|
|
|
|
2013-12-12 18:36:32 +01:00
|
|
|
def is_super_user_api(request):
|
|
|
|
return request.user.is_authenticated() and is_super_user(request.user)
|
|
|
|
|
2013-04-25 20:42:28 +02:00
|
|
|
# Doing 1000 memcached requests to get_display_recipient is quite slow,
|
|
|
|
# so add a local cache as well as the memcached cache.
|
2013-12-18 23:00:14 +01:00
|
|
|
per_request_display_recipient_cache = {}
|
2013-09-21 15:35:12 +02:00
|
|
|
def get_display_recipient_by_id(recipient_id, recipient_type, recipient_type_id):
|
2013-12-18 23:00:14 +01:00
|
|
|
if recipient_id not in per_request_display_recipient_cache:
|
2013-09-21 15:35:12 +02:00
|
|
|
result = get_display_recipient_memcached(recipient_id, recipient_type, recipient_type_id)
|
2013-12-18 23:00:14 +01:00
|
|
|
per_request_display_recipient_cache[recipient_id] = result
|
|
|
|
return per_request_display_recipient_cache[recipient_id]
|
2013-09-21 15:35:12 +02:00
|
|
|
|
2013-04-25 20:42:28 +02:00
|
|
|
def get_display_recipient(recipient):
|
2013-09-21 15:35:12 +02:00
|
|
|
return get_display_recipient_by_id(
|
|
|
|
recipient.id,
|
|
|
|
recipient.type,
|
|
|
|
recipient.type_id
|
|
|
|
)
|
2013-04-25 20:42:28 +02:00
|
|
|
|
2013-12-18 23:00:14 +01:00
|
|
|
def flush_per_request_caches():
|
|
|
|
global per_request_display_recipient_cache
|
|
|
|
per_request_display_recipient_cache = {}
|
2013-12-18 23:01:11 +01:00
|
|
|
global per_request_realm_filters_cache
|
|
|
|
per_request_realm_filters_cache = {}
|
2013-08-22 17:45:15 +02:00
|
|
|
|
2013-09-21 15:35:12 +02:00
|
|
|
@cache_with_key(lambda *args: display_recipient_cache_key(args[0]),
|
2013-03-26 19:09:45 +01:00
|
|
|
timeout=3600*24*7)
|
2013-09-21 15:35:12 +02:00
|
|
|
def get_display_recipient_memcached(recipient_id, recipient_type, recipient_type_id):
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
"""
|
2012-12-03 19:49:12 +01:00
|
|
|
returns: an appropriate object describing the recipient. For a
|
|
|
|
stream this will be the stream name as a string. For a huddle or
|
|
|
|
personal, it will be an array of dicts about each recipient.
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
"""
|
2013-09-21 15:35:12 +02:00
|
|
|
if recipient_type == Recipient.STREAM:
|
|
|
|
stream = Stream.objects.get(id=recipient_type_id)
|
2012-10-10 22:53:24 +02:00
|
|
|
return stream.name
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
# We don't really care what the ordering is, just that it's deterministic.
|
2013-09-21 15:35:12 +02:00
|
|
|
user_profile_list = (UserProfile.objects.filter(subscription__recipient_id=recipient_id)
|
2012-12-03 19:49:12 +01:00
|
|
|
.select_related()
|
2013-03-28 20:43:34 +01:00
|
|
|
.order_by('email'))
|
|
|
|
return [{'email': user_profile.email,
|
2013-05-02 18:56:42 +02:00
|
|
|
'domain': user_profile.realm.domain,
|
2012-09-27 19:58:42 +02:00
|
|
|
'full_name': user_profile.full_name,
|
2013-07-29 22:07:42 +02:00
|
|
|
'short_name': user_profile.short_name,
|
|
|
|
'id': user_profile.id} for user_profile in user_profile_list]
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2013-08-07 16:23:31 +02:00
|
|
|
def completely_open(domain):
|
|
|
|
# This domain is completely open to everyone on the internet to
|
|
|
|
# join. This is not the same as a "restricted_to_domain" realm: in
|
|
|
|
# those realms, users from outside the domain must be invited.
|
2013-12-09 19:40:16 +01:00
|
|
|
return domain and Realm.objects.filter(domain=domain).count() == 1 and \
|
|
|
|
domain.lower() == "customer3.invalid"
|
2013-08-07 16:23:31 +02:00
|
|
|
|
2013-08-22 16:56:37 +02:00
|
|
|
|
|
|
|
def get_realm_emoji_cache_key(realm):
|
|
|
|
return 'realm_emoji:%s' % (realm.id,)
|
|
|
|
|
2012-09-05 21:49:56 +02:00
|
|
|
class Realm(models.Model):
|
2013-10-17 17:36:13 +02:00
|
|
|
# domain is a domain in the Internet sense. It must be structured like a
|
|
|
|
# valid email domain. We use is to restrict access, identify bots, etc.
|
2012-10-22 19:23:11 +02:00
|
|
|
domain = models.CharField(max_length=40, db_index=True, unique=True)
|
2013-10-17 17:36:13 +02:00
|
|
|
# name is the user-visible identifier for the realm. It has no required
|
|
|
|
# structure.
|
|
|
|
name = models.CharField(max_length=40, null=True)
|
2013-02-06 16:59:31 +01:00
|
|
|
restricted_to_domain = models.BooleanField(default=True)
|
2013-09-20 16:19:00 +02:00
|
|
|
date_created = models.DateTimeField(default=timezone.now)
|
2013-10-02 23:40:21 +02:00
|
|
|
notifications_stream = models.ForeignKey('Stream', related_name='+', null=True, blank=True)
|
2014-01-07 17:37:13 +01:00
|
|
|
deactivated = models.BooleanField(default=False)
|
2013-10-02 23:40:21 +02:00
|
|
|
|
|
|
|
NOTIFICATION_STREAM_NAME = 'zulip'
|
2012-09-05 21:49:56 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2013-03-26 19:46:47 +01:00
|
|
|
return (u"<Realm: %s %s>" % (self.domain, self.id)).encode("utf-8")
|
2012-09-05 21:49:56 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
|
|
|
|
2013-08-22 16:56:37 +02:00
|
|
|
@cache_with_key(get_realm_emoji_cache_key, timeout=3600*24*7)
|
|
|
|
def get_emoji(self):
|
|
|
|
return get_realm_emoji_uncached(self)
|
|
|
|
|
2013-10-17 16:33:04 +02:00
|
|
|
@property
|
|
|
|
def deployment(self):
|
|
|
|
try:
|
|
|
|
return self._deployments.all()[0]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@deployment.setter
|
|
|
|
def set_deployments(self, value):
|
|
|
|
self._deployments = [value]
|
|
|
|
|
2013-11-02 15:36:17 +01:00
|
|
|
def get_admin_users(self):
|
|
|
|
# This method is kind of expensive, due to our complex permissions model.
|
2014-02-12 21:46:40 +01:00
|
|
|
candidates = get_users_with_perms(self, only_with_perms=['administer'])
|
|
|
|
return candidates
|
2013-11-02 15:36:17 +01:00
|
|
|
|
2014-01-28 17:29:00 +01:00
|
|
|
def get_active_users(self):
|
|
|
|
return UserProfile.objects.filter(realm=self, is_active=True).select_related()
|
|
|
|
|
2013-06-24 21:26:38 +02:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
('administer', "Administer a realm"),
|
|
|
|
)
|
|
|
|
|
2014-01-28 18:18:19 +01:00
|
|
|
post_save.connect(flush_realm, sender=Realm)
|
|
|
|
|
2013-11-07 20:17:54 +01:00
|
|
|
class RealmAlias(models.Model):
|
|
|
|
realm = models.ForeignKey(Realm, null=True)
|
|
|
|
domain = models.CharField(max_length=80, db_index=True, unique=True)
|
|
|
|
|
2013-07-18 18:48:56 +02:00
|
|
|
# These functions should only be used on email addresses that have
|
|
|
|
# been validated via django.core.validators.validate_email
|
|
|
|
#
|
|
|
|
# Note that we need to use some care, since can you have multiple @-signs; e.g.
|
2013-07-24 23:41:24 +02:00
|
|
|
# "tabbott@test"@zulip.com
|
2013-07-18 18:48:56 +02:00
|
|
|
# is valid email address
|
|
|
|
def email_to_username(email):
|
2013-08-15 19:16:03 +02:00
|
|
|
return "@".join(email.split("@")[:-1]).lower()
|
2013-07-18 18:48:56 +02:00
|
|
|
|
2013-11-22 23:48:00 +01:00
|
|
|
# Returns the raw domain portion of the desired email address
|
|
|
|
def split_email_to_domain(email):
|
2013-08-15 19:16:03 +02:00
|
|
|
return email.split("@")[-1].lower()
|
2013-07-18 18:48:56 +02:00
|
|
|
|
2013-11-22 23:48:00 +01:00
|
|
|
# Returns the domain, potentually de-aliased, for the realm
|
|
|
|
# that this user's email is in
|
|
|
|
def resolve_email_to_domain(email):
|
|
|
|
domain = split_email_to_domain(email)
|
|
|
|
alias = alias_for_realm(domain)
|
|
|
|
if alias is not None:
|
|
|
|
domain = alias.realm.domain
|
|
|
|
return domain
|
|
|
|
|
|
|
|
def alias_for_realm(domain):
|
|
|
|
try:
|
|
|
|
return RealmAlias.objects.get(domain=domain)
|
|
|
|
except RealmAlias.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2013-11-04 23:16:46 +01:00
|
|
|
def remote_user_to_email(remote_user):
|
|
|
|
if settings.SSO_APPEND_DOMAIN is not None:
|
2013-11-05 23:50:28 +01:00
|
|
|
remote_user += "@" + settings.SSO_APPEND_DOMAIN
|
2013-11-04 23:16:46 +01:00
|
|
|
return remote_user
|
|
|
|
|
2013-08-22 16:56:37 +02:00
|
|
|
class RealmEmoji(models.Model):
|
|
|
|
realm = models.ForeignKey(Realm)
|
|
|
|
name = models.TextField()
|
|
|
|
img_url = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("realm", "name")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "<RealmEmoji(%s): %s %s>" % (self.realm.domain, self.name, self.img_url)
|
|
|
|
|
|
|
|
def get_realm_emoji_uncached(realm):
|
|
|
|
d = {}
|
|
|
|
for row in RealmEmoji.objects.filter(realm=realm):
|
|
|
|
d[row.name] = row.img_url
|
|
|
|
return d
|
|
|
|
|
2014-01-28 20:53:57 +01:00
|
|
|
def flush_realm_emoji(sender, **kwargs):
|
2013-08-22 16:56:37 +02:00
|
|
|
realm = kwargs['instance'].realm
|
|
|
|
cache_set(get_realm_emoji_cache_key(realm),
|
|
|
|
get_realm_emoji_uncached(realm),
|
|
|
|
timeout=3600*24*7)
|
|
|
|
|
2014-01-28 20:53:57 +01:00
|
|
|
post_save.connect(flush_realm_emoji, sender=RealmEmoji)
|
|
|
|
post_delete.connect(flush_realm_emoji, sender=RealmEmoji)
|
2013-08-22 16:56:37 +02:00
|
|
|
|
2013-12-06 23:02:52 +01:00
|
|
|
class RealmFilter(models.Model):
|
|
|
|
realm = models.ForeignKey(Realm)
|
|
|
|
pattern = models.TextField()
|
|
|
|
url_format_string = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("realm", "pattern")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "<RealmFilter(%s): %s %s>" % (self.realm.domain, self.pattern, self.url_format_string)
|
|
|
|
|
|
|
|
def get_realm_filters_cache_key(domain):
|
|
|
|
return 'all_realm_filters:%s' % (domain,)
|
|
|
|
|
2013-12-18 23:01:11 +01:00
|
|
|
# We have a per-process cache to avoid doing 1000 memcached queries during page load
|
|
|
|
per_request_realm_filters_cache = {}
|
2013-12-06 23:02:52 +01:00
|
|
|
def realm_filters_for_domain(domain):
|
2013-12-18 23:01:11 +01:00
|
|
|
domain = domain.lower()
|
|
|
|
if domain not in per_request_realm_filters_cache:
|
|
|
|
per_request_realm_filters_cache[domain] = realm_filters_for_domain_memcached(domain)
|
|
|
|
return per_request_realm_filters_cache[domain]
|
|
|
|
|
|
|
|
@cache_with_key(get_realm_filters_cache_key, timeout=3600*24*7)
|
|
|
|
def realm_filters_for_domain_memcached(domain):
|
2013-12-06 23:02:52 +01:00
|
|
|
filters = []
|
|
|
|
for realm_filter in RealmFilter.objects.filter(realm=get_realm(domain)):
|
|
|
|
filters.append((realm_filter.pattern, realm_filter.url_format_string))
|
|
|
|
|
|
|
|
return filters
|
|
|
|
|
|
|
|
def all_realm_filters():
|
|
|
|
filters = defaultdict(list)
|
|
|
|
for realm_filter in RealmFilter.objects.all():
|
|
|
|
filters[realm_filter.realm.domain].append((realm_filter.pattern, realm_filter.url_format_string))
|
|
|
|
|
|
|
|
return filters
|
|
|
|
|
2014-01-28 21:00:38 +01:00
|
|
|
def flush_realm_filter(sender, **kwargs):
|
2013-12-06 23:02:52 +01:00
|
|
|
realm = kwargs['instance'].realm
|
|
|
|
cache_delete(get_realm_filters_cache_key(realm.domain))
|
2013-12-18 23:01:11 +01:00
|
|
|
try:
|
|
|
|
per_request_realm_filters_cache.pop(realm.domain.lower())
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2013-12-06 23:02:52 +01:00
|
|
|
|
2014-01-28 21:00:38 +01:00
|
|
|
post_save.connect(flush_realm_filter, sender=RealmFilter)
|
|
|
|
post_delete.connect(flush_realm_filter, sender=RealmFilter)
|
2013-12-06 23:02:52 +01:00
|
|
|
|
2013-06-24 17:51:10 +02:00
|
|
|
class UserProfile(AbstractBaseUser, PermissionsMixin):
|
2013-04-04 20:38:22 +02:00
|
|
|
# Fields from models.AbstractUser minus last_name and first_name,
|
|
|
|
# which we don't use; email is modified to make it indexed and unique.
|
|
|
|
email = models.EmailField(blank=False, db_index=True, unique=True)
|
2013-03-08 19:53:00 +01:00
|
|
|
is_staff = models.BooleanField(default=False)
|
|
|
|
is_active = models.BooleanField(default=True)
|
2013-05-03 00:25:43 +02:00
|
|
|
is_bot = models.BooleanField(default=False)
|
2013-03-08 19:53:00 +01:00
|
|
|
date_joined = models.DateTimeField(default=timezone.now)
|
2014-01-07 18:57:54 +01:00
|
|
|
is_mirror_dummy = models.BooleanField(default=False)
|
2013-05-03 00:25:43 +02:00
|
|
|
bot_owner = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
|
|
|
|
|
2013-03-08 19:53:00 +01:00
|
|
|
USERNAME_FIELD = 'email'
|
2013-07-08 16:42:00 +02:00
|
|
|
MAX_NAME_LENGTH = 100
|
2013-03-08 19:53:00 +01:00
|
|
|
|
|
|
|
# Our custom site-specific fields
|
2013-07-08 16:42:00 +02:00
|
|
|
full_name = models.CharField(max_length=MAX_NAME_LENGTH)
|
|
|
|
short_name = models.CharField(max_length=MAX_NAME_LENGTH)
|
2013-09-20 20:53:56 +02:00
|
|
|
# pointer points to Message.id, NOT UserMessage.id.
|
2012-08-28 18:44:51 +02:00
|
|
|
pointer = models.IntegerField()
|
2012-10-17 17:42:40 +02:00
|
|
|
last_pointer_updater = models.CharField(max_length=64)
|
2012-09-05 21:49:56 +02:00
|
|
|
realm = models.ForeignKey(Realm)
|
2012-10-01 21:36:44 +02:00
|
|
|
api_key = models.CharField(max_length=32)
|
2014-02-05 21:31:30 +01:00
|
|
|
|
|
|
|
### Notifications settings. ###
|
|
|
|
|
|
|
|
# Stream notifications.
|
|
|
|
enable_stream_desktop_notifications = models.BooleanField(default=True)
|
|
|
|
enable_stream_sounds = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
# PM + @-mention notifications.
|
2012-11-23 21:23:41 +01:00
|
|
|
enable_desktop_notifications = models.BooleanField(default=True)
|
2013-05-03 21:49:01 +02:00
|
|
|
enable_sounds = models.BooleanField(default=True)
|
2013-05-07 23:15:48 +02:00
|
|
|
enable_offline_email_notifications = models.BooleanField(default=True)
|
2013-10-16 17:24:52 +02:00
|
|
|
enable_offline_push_notifications = models.BooleanField(default=True)
|
2014-02-05 21:31:30 +01:00
|
|
|
|
2013-11-30 14:55:16 +01:00
|
|
|
enable_digest_emails = models.BooleanField(default=True)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2014-02-05 21:31:30 +01:00
|
|
|
# Old notification field superseded by existence of stream notification
|
|
|
|
# settings.
|
|
|
|
default_desktop_notifications = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
last_reminder = models.DateTimeField(default=timezone.now, null=True)
|
|
|
|
rate_limits = models.CharField(default="", max_length=100) # comma-separated list of range:max pairs
|
|
|
|
|
2013-12-04 22:48:47 +01:00
|
|
|
# UI vars
|
2014-02-05 17:40:31 +01:00
|
|
|
enter_sends = models.NullBooleanField(default=True)
|
2013-12-04 22:48:47 +01:00
|
|
|
autoscroll_forever = models.BooleanField(default=False)
|
|
|
|
|
2013-05-17 21:28:51 +02:00
|
|
|
# Hours to wait before sending another email to a user
|
|
|
|
EMAIL_REMINDER_WAITPERIOD = 24
|
2013-09-17 22:31:05 +02:00
|
|
|
# Minutes to wait before warning a bot owner that her bot sent a message
|
|
|
|
# to a nonexistent stream
|
|
|
|
BOT_OWNER_STREAM_ALERT_WAITPERIOD = 1
|
2013-05-17 21:28:51 +02:00
|
|
|
|
2013-06-07 21:51:57 +02:00
|
|
|
AVATAR_FROM_GRAVATAR = 'G'
|
|
|
|
AVATAR_FROM_USER = 'U'
|
|
|
|
AVATAR_FROM_SYSTEM = 'S'
|
|
|
|
AVATAR_SOURCES = (
|
|
|
|
(AVATAR_FROM_GRAVATAR, 'Hosted by Gravatar'),
|
|
|
|
(AVATAR_FROM_USER, 'Uploaded by user'),
|
|
|
|
(AVATAR_FROM_SYSTEM, 'System generated'),
|
|
|
|
)
|
|
|
|
avatar_source = models.CharField(default=AVATAR_FROM_GRAVATAR, choices=AVATAR_SOURCES, max_length=1)
|
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
TUTORIAL_WAITING = 'W'
|
|
|
|
TUTORIAL_STARTED = 'S'
|
|
|
|
TUTORIAL_FINISHED = 'F'
|
|
|
|
TUTORIAL_STATES = ((TUTORIAL_WAITING, "Waiting"),
|
|
|
|
(TUTORIAL_STARTED, "Started"),
|
|
|
|
(TUTORIAL_FINISHED, "Finished"))
|
|
|
|
|
|
|
|
tutorial_status = models.CharField(default=TUTORIAL_WAITING, choices=TUTORIAL_STATES, max_length=1)
|
2013-05-08 16:12:19 +02:00
|
|
|
# Contains serialized JSON of the form:
|
|
|
|
# [("step 1", true), ("step 2", false)]
|
|
|
|
# where the second element of each tuple is if the step has been
|
|
|
|
# completed.
|
2013-06-18 23:55:55 +02:00
|
|
|
onboarding_steps = models.TextField(default=ujson.dumps([]))
|
2013-04-04 22:30:28 +02:00
|
|
|
|
2013-07-26 16:51:02 +02:00
|
|
|
invites_granted = models.IntegerField(default=0)
|
|
|
|
invites_used = models.IntegerField(default=0)
|
|
|
|
|
2013-09-03 22:41:17 +02:00
|
|
|
alert_words = models.TextField(default=ujson.dumps([])) # json-serialized list of strings
|
|
|
|
|
2013-09-09 21:53:40 +02:00
|
|
|
# Contains serialized JSON of the form:
|
|
|
|
# [["social", "mit"], ["devel", "ios"]]
|
|
|
|
muted_topics = models.TextField(default=ujson.dumps([]))
|
|
|
|
|
2013-03-08 19:53:00 +01:00
|
|
|
objects = UserManager()
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2013-09-05 20:51:38 +02:00
|
|
|
def can_admin_user(self, target_user):
|
|
|
|
"""Returns whether this user has permission to modify target_user"""
|
|
|
|
if target_user.bot_owner == self:
|
|
|
|
return True
|
|
|
|
elif self.has_perm('administer', target_user.realm):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2013-11-19 20:21:12 +01:00
|
|
|
def is_admin(self):
|
|
|
|
return self.has_perm('administer', self.realm)
|
2013-08-12 23:31:23 +02:00
|
|
|
|
2013-08-22 17:49:02 +02:00
|
|
|
@property
|
|
|
|
def public_streams_disabled(self):
|
|
|
|
return self.email.lower() == "restricted-user@customer5.invalid"
|
|
|
|
|
2013-09-17 22:31:05 +02:00
|
|
|
def last_reminder_tzaware(self):
|
|
|
|
if self.last_reminder is not None and timezone.is_naive(self.last_reminder):
|
|
|
|
logging.warning("Loaded a user_profile.last_reminder for user %s that's not tz-aware: %s"
|
|
|
|
% (self.email, self.last_reminder))
|
|
|
|
return self.last_reminder.replace(tzinfo=timezone.utc)
|
|
|
|
|
|
|
|
return self.last_reminder
|
|
|
|
|
2013-03-08 19:53:00 +01:00
|
|
|
def __repr__(self):
|
2013-03-28 20:43:34 +01:00
|
|
|
return (u"<UserProfile: %s %s>" % (self.email, self.realm)).encode("utf-8")
|
2013-03-08 19:53:00 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
|
|
|
|
2013-10-20 21:10:03 +02:00
|
|
|
@staticmethod
|
|
|
|
def emails_from_ids(user_ids):
|
|
|
|
rows = UserProfile.objects.filter(id__in=user_ids).values('id', 'email')
|
|
|
|
return {row['id']: row['email'] for row in rows}
|
|
|
|
|
2014-01-15 22:31:38 +01:00
|
|
|
def can_create_streams(self):
|
|
|
|
# Long term this might be an actual DB attribute. Short term and long term, we want
|
|
|
|
# this to be conceptually a property of the user, although it may actually be administered
|
|
|
|
# in a more complicated way, like certain realms may allow only admins to create streams.
|
|
|
|
return True
|
|
|
|
|
2014-01-24 22:29:17 +01:00
|
|
|
def receives_offline_notifications(user_profile):
|
|
|
|
return ((user_profile.enable_offline_email_notifications or
|
|
|
|
user_profile.enable_offline_push_notifications) and
|
|
|
|
not user_profile.is_bot)
|
|
|
|
|
2013-03-15 21:17:32 +01:00
|
|
|
# Make sure we flush the UserProfile object from our memcached
|
|
|
|
# whenever we save it.
|
2014-01-28 17:02:30 +01:00
|
|
|
post_save.connect(flush_user_profile, sender=UserProfile)
|
2013-03-15 21:17:32 +01:00
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
class PreregistrationUser(models.Model):
|
2012-12-11 23:42:32 +01:00
|
|
|
email = models.EmailField()
|
|
|
|
referred_by = models.ForeignKey(UserProfile, null=True)
|
|
|
|
streams = models.ManyToManyField('Stream', null=True)
|
|
|
|
invited_at = models.DateTimeField(auto_now=True)
|
|
|
|
|
2012-10-29 19:08:18 +01:00
|
|
|
# status: whether an object has been confirmed.
|
|
|
|
# if confirmed, set to confirmation.settings.STATUS_ACTIVE
|
|
|
|
status = models.IntegerField(default=0)
|
|
|
|
|
2013-08-02 20:31:19 +02:00
|
|
|
realm = models.ForeignKey(Realm, null=True)
|
|
|
|
|
2013-12-09 23:17:16 +01:00
|
|
|
# Deprecated. Drop this table once prod uses PushDeviceToken and the data has
|
|
|
|
# been copied there.
|
2013-10-15 20:19:41 +02:00
|
|
|
class AppleDeviceToken(models.Model):
|
|
|
|
# The token is a unique device-specific token that is
|
|
|
|
# sent to us from each iOS device, after registering with
|
|
|
|
# the APNS service
|
|
|
|
token = models.CharField(max_length=255, unique=True)
|
2013-10-23 20:07:38 +02:00
|
|
|
last_updated = models.DateTimeField(auto_now=True, default=timezone.now)
|
2013-10-15 20:19:41 +02:00
|
|
|
|
|
|
|
# The user who's device this is
|
|
|
|
user = models.ForeignKey(UserProfile, db_index=True)
|
|
|
|
|
2013-12-09 23:17:16 +01:00
|
|
|
class PushDeviceToken(models.Model):
|
|
|
|
APNS = 1
|
|
|
|
GCM = 2
|
|
|
|
|
|
|
|
KINDS = (
|
|
|
|
(APNS, 'apns'),
|
|
|
|
(GCM, 'gcm'),
|
|
|
|
)
|
|
|
|
|
|
|
|
kind = models.PositiveSmallIntegerField(choices=KINDS)
|
|
|
|
|
|
|
|
# The token is a unique device-specific token that is
|
|
|
|
# sent to us from each device:
|
|
|
|
# - APNS token if kind == APNS
|
|
|
|
# - GCM registration id if kind == GCM
|
|
|
|
token = models.CharField(max_length=4096, unique=True)
|
|
|
|
last_updated = models.DateTimeField(auto_now=True, default=timezone.now)
|
|
|
|
|
|
|
|
# The user who's device this is
|
|
|
|
user = models.ForeignKey(UserProfile, db_index=True)
|
|
|
|
|
2012-10-29 19:08:18 +01:00
|
|
|
class MitUser(models.Model):
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
# status: whether an object has been confirmed.
|
|
|
|
# if confirmed, set to confirmation.settings.STATUS_ACTIVE
|
2012-09-28 22:47:05 +02:00
|
|
|
status = models.IntegerField(default=0)
|
|
|
|
|
2012-10-10 22:53:24 +02:00
|
|
|
class Stream(models.Model):
|
2013-10-09 16:55:17 +02:00
|
|
|
MAX_NAME_LENGTH = 60
|
2013-03-20 01:16:41 +01:00
|
|
|
name = models.CharField(max_length=MAX_NAME_LENGTH, db_index=True)
|
2012-09-14 23:28:38 +02:00
|
|
|
realm = models.ForeignKey(Realm, db_index=True)
|
2013-01-23 20:36:32 +01:00
|
|
|
invite_only = models.NullBooleanField(default=False)
|
2013-08-08 16:51:18 +02:00
|
|
|
# Used by the e-mail forwarder. The e-mail RFC specifies a maximum
|
|
|
|
# e-mail length of 254, and our max stream length is 30, so we
|
|
|
|
# have plenty of room for the token.
|
|
|
|
email_token = models.CharField(
|
2013-08-08 18:47:04 +02:00
|
|
|
max_length=32, default=lambda: generate_random_token(32))
|
2014-01-24 20:40:06 +01:00
|
|
|
description = models.CharField(max_length=1024, default='')
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2013-09-26 21:48:08 +02:00
|
|
|
date_created = models.DateTimeField(default=timezone.now)
|
2014-01-24 17:41:54 +01:00
|
|
|
deactivated = models.BooleanField(default=False)
|
2013-09-26 21:48:08 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
2013-03-26 19:46:47 +01:00
|
|
|
return (u"<Stream: %s>" % (self.name,)).encode("utf-8")
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2013-01-15 21:10:50 +01:00
|
|
|
def is_public(self):
|
2013-12-06 21:29:21 +01:00
|
|
|
if self.realm.domain in ["mit.edu", "customer1.invalid", "customer22.invalid"]:
|
|
|
|
# These realms predate public streams and all streams are private
|
|
|
|
return False
|
|
|
|
return not self.invite_only
|
2013-01-15 21:10:50 +01:00
|
|
|
|
2012-11-07 22:33:38 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("name", "realm")
|
|
|
|
|
2012-09-19 18:54:57 +02:00
|
|
|
@classmethod
|
|
|
|
def create(cls, name, realm):
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = cls(name=name, realm=realm)
|
|
|
|
stream.save()
|
2012-09-07 19:24:54 +02:00
|
|
|
|
2012-10-19 23:40:44 +02:00
|
|
|
recipient = Recipient.objects.create(type_id=stream.id,
|
|
|
|
type=Recipient.STREAM)
|
2012-10-10 22:53:24 +02:00
|
|
|
return (stream, recipient)
|
2012-09-07 19:24:54 +02:00
|
|
|
|
2013-09-19 22:55:08 +02:00
|
|
|
def num_subscribers(self):
|
|
|
|
return Subscription.objects.filter(
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
recipient__type_id=self.id,
|
|
|
|
user_profile__is_active=True,
|
|
|
|
active=True
|
|
|
|
).count()
|
|
|
|
|
2014-01-28 20:49:55 +01:00
|
|
|
post_save.connect(flush_stream, sender=Stream)
|
|
|
|
post_delete.connect(flush_stream, sender=Stream)
|
2014-01-15 22:48:27 +01:00
|
|
|
|
2013-03-18 18:57:34 +01:00
|
|
|
def valid_stream_name(name):
|
|
|
|
return name != ""
|
|
|
|
|
2012-08-28 21:27:42 +02:00
|
|
|
class Recipient(models.Model):
|
2012-09-14 23:28:38 +02:00
|
|
|
type_id = models.IntegerField(db_index=True)
|
|
|
|
type = models.PositiveSmallIntegerField(db_index=True)
|
2012-10-10 22:58:51 +02:00
|
|
|
# Valid types are {personal, stream, huddle}
|
2012-09-07 20:14:13 +02:00
|
|
|
PERSONAL = 1
|
2012-10-10 22:57:21 +02:00
|
|
|
STREAM = 2
|
2012-09-07 20:14:13 +02:00
|
|
|
HUDDLE = 3
|
|
|
|
|
2012-11-07 22:33:38 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("type", "type_id")
|
|
|
|
|
2012-11-02 21:08:29 +01:00
|
|
|
# N.B. If we used Django's choice=... we would get this for free (kinda)
|
|
|
|
_type_names = {
|
|
|
|
PERSONAL: 'personal',
|
|
|
|
STREAM: 'stream',
|
|
|
|
HUDDLE: 'huddle' }
|
|
|
|
|
2012-09-07 20:14:13 +02:00
|
|
|
def type_name(self):
|
2012-11-02 21:08:29 +01:00
|
|
|
# Raises KeyError if invalid
|
|
|
|
return self._type_names[self.type]
|
2012-08-28 21:27:42 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self)
|
2013-03-26 19:46:47 +01:00
|
|
|
return (u"<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)).encode("utf-8")
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-10-19 21:30:42 +02:00
|
|
|
class Client(models.Model):
|
2012-10-22 19:23:11 +02:00
|
|
|
name = models.CharField(max_length=30, db_index=True, unique=True)
|
2012-10-19 21:30:42 +02:00
|
|
|
|
2013-11-20 22:16:48 +01:00
|
|
|
get_client_cache = {}
|
|
|
|
def get_client(name):
|
|
|
|
if name not in get_client_cache:
|
|
|
|
result = get_client_memcached(name)
|
|
|
|
get_client_cache[name] = result
|
|
|
|
return get_client_cache[name]
|
|
|
|
|
2013-03-26 17:47:52 +01:00
|
|
|
def get_client_cache_key(name):
|
|
|
|
return 'get_client:%s' % (make_safe_digest(name),)
|
|
|
|
|
2013-03-26 19:09:45 +01:00
|
|
|
@cache_with_key(get_client_cache_key, timeout=3600*24*7)
|
2013-11-20 22:16:48 +01:00
|
|
|
def get_client_memcached(name):
|
2013-11-01 18:59:05 +01:00
|
|
|
(client, _) = Client.objects.get_or_create(name=name)
|
2012-10-19 21:30:42 +02:00
|
|
|
return client
|
|
|
|
|
2013-03-19 13:05:19 +01:00
|
|
|
# get_stream_backend takes either a realm id or a realm
|
2013-03-26 19:09:45 +01:00
|
|
|
@cache_with_key(get_stream_cache_key, timeout=3600*24*7)
|
2013-03-19 13:05:19 +01:00
|
|
|
def get_stream_backend(stream_name, realm):
|
2013-01-17 22:16:39 +01:00
|
|
|
if isinstance(realm, Realm):
|
|
|
|
realm_id = realm.id
|
|
|
|
else:
|
|
|
|
realm_id = realm
|
2013-03-19 13:05:19 +01:00
|
|
|
return Stream.objects.select_related("realm").get(
|
|
|
|
name__iexact=stream_name.strip(), realm_id=realm_id)
|
|
|
|
|
2014-01-24 23:30:53 +01:00
|
|
|
def get_active_streams(realm):
|
|
|
|
"""
|
|
|
|
Return all streams (including invite-only streams) that have not been deactivated.
|
|
|
|
"""
|
|
|
|
return Stream.objects.filter(realm=realm, deactivated=False)
|
|
|
|
|
2013-03-19 13:05:19 +01:00
|
|
|
# get_stream takes either a realm id or a realm
|
|
|
|
def get_stream(stream_name, realm):
|
2013-01-17 22:16:39 +01:00
|
|
|
try:
|
2013-03-19 13:05:19 +01:00
|
|
|
return get_stream_backend(stream_name, realm)
|
2013-01-17 22:16:39 +01:00
|
|
|
except Stream.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2013-06-27 22:52:05 +02:00
|
|
|
def bulk_get_streams(realm, stream_names):
|
|
|
|
if isinstance(realm, Realm):
|
|
|
|
realm_id = realm.id
|
|
|
|
else:
|
|
|
|
realm_id = realm
|
|
|
|
|
|
|
|
def fetch_streams_by_name(stream_names):
|
|
|
|
# This should be just
|
|
|
|
#
|
|
|
|
# Stream.objects.select_related("realm").filter(name__iexact__in=stream_names,
|
|
|
|
# realm_id=realm_id)
|
|
|
|
#
|
|
|
|
# But chaining __in and __iexact doesn't work with Django's
|
|
|
|
# ORM, so we have the following hack to construct the relevant where clause
|
|
|
|
if len(stream_names) == 0:
|
|
|
|
return []
|
|
|
|
upper_list = ", ".join(["UPPER(%s)"] * len(stream_names))
|
2013-07-29 23:03:31 +02:00
|
|
|
where_clause = "UPPER(zerver_stream.name::text) IN (%s)" % (upper_list,)
|
2014-01-24 23:30:53 +01:00
|
|
|
return get_active_streams(realm_id).select_related("realm").extra(
|
2013-06-27 22:52:05 +02:00
|
|
|
where=[where_clause],
|
|
|
|
params=stream_names)
|
|
|
|
|
|
|
|
return generic_bulk_cached_fetch(lambda stream_name: get_stream_cache_key(stream_name, realm),
|
|
|
|
fetch_streams_by_name,
|
|
|
|
[stream_name.lower() for stream_name in stream_names],
|
|
|
|
id_fetcher=lambda stream: stream.name.lower())
|
|
|
|
|
2013-03-26 17:10:44 +01:00
|
|
|
def get_recipient_cache_key(type, type_id):
|
|
|
|
return "get_recipient:%s:%s" % (type, type_id,)
|
|
|
|
|
2013-03-26 19:09:45 +01:00
|
|
|
@cache_with_key(get_recipient_cache_key, timeout=3600*24*7)
|
2013-03-18 16:54:58 +01:00
|
|
|
def get_recipient(type, type_id):
|
|
|
|
return Recipient.objects.get(type_id=type_id, type=type)
|
|
|
|
|
2013-06-25 19:26:58 +02:00
|
|
|
def bulk_get_recipients(type, type_ids):
|
|
|
|
def cache_key_function(type_id):
|
|
|
|
return get_recipient_cache_key(type, type_id)
|
|
|
|
def query_function(type_ids):
|
|
|
|
return Recipient.objects.filter(type=type, type_id__in=type_ids)
|
|
|
|
|
|
|
|
return generic_bulk_cached_fetch(cache_key_function, query_function, type_ids,
|
|
|
|
id_fetcher=lambda recipient: recipient.type_id)
|
|
|
|
|
2013-02-02 00:56:37 +01:00
|
|
|
# NB: This function is currently unused, but may come in handy.
|
2012-12-10 21:39:28 +01:00
|
|
|
def linebreak(string):
|
|
|
|
return string.replace('\n\n', '<p/>').replace('\n', '<br/>')
|
|
|
|
|
2013-06-18 20:02:45 +02:00
|
|
|
def extract_message_dict(message_str):
|
2013-06-19 21:41:29 +02:00
|
|
|
return ujson.loads(zlib.decompress(message_str))
|
2013-06-18 20:02:45 +02:00
|
|
|
|
|
|
|
def stringify_message_dict(message_dict):
|
2013-06-19 21:41:29 +02:00
|
|
|
return zlib.compress(ujson.dumps(message_dict))
|
2013-06-18 20:02:45 +02:00
|
|
|
|
2013-07-08 21:37:58 +02:00
|
|
|
def to_dict_cache_key_id(message_id, apply_markdown):
|
2013-05-02 18:51:09 +02:00
|
|
|
return 'message_dict:%d:%d' % (message_id, apply_markdown)
|
|
|
|
|
2013-07-08 21:37:58 +02:00
|
|
|
def to_dict_cache_key(message, apply_markdown):
|
|
|
|
return to_dict_cache_key_id(message.id, apply_markdown)
|
2013-04-22 16:29:57 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
class Message(models.Model):
|
2012-08-28 18:44:51 +02:00
|
|
|
sender = models.ForeignKey(UserProfile)
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient = models.ForeignKey(Recipient)
|
2012-12-07 23:21:26 +01:00
|
|
|
subject = models.CharField(max_length=MAX_SUBJECT_LENGTH, db_index=True)
|
2012-09-14 19:16:01 +02:00
|
|
|
content = models.TextField()
|
2013-03-18 22:51:08 +01:00
|
|
|
rendered_content = models.TextField(null=True)
|
|
|
|
rendered_content_version = models.IntegerField(null=True)
|
2012-12-07 23:21:26 +01:00
|
|
|
pub_date = models.DateTimeField('date published', db_index=True)
|
2012-10-19 21:30:42 +02:00
|
|
|
sending_client = models.ForeignKey(Client)
|
2013-05-21 17:48:46 +02:00
|
|
|
last_edit_time = models.DateTimeField(null=True)
|
|
|
|
edit_history = models.TextField(null=True)
|
2014-02-21 17:44:48 +01:00
|
|
|
has_attachment = models.BooleanField(default=False, db_index=True)
|
|
|
|
has_image = models.BooleanField(default=False, db_index=True)
|
|
|
|
has_link = models.BooleanField(default=False, db_index=True)
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self.recipient)
|
2013-03-26 19:46:47 +01:00
|
|
|
return (u"<Message: %s / %s / %r>" % (display_recipient, self.subject, self.sender)).encode("utf-8")
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2013-08-22 16:56:37 +02:00
|
|
|
def get_realm(self):
|
|
|
|
return self.sender.realm
|
|
|
|
|
2013-09-04 22:44:45 +02:00
|
|
|
def render_markdown(self, content, domain=None):
|
2013-06-28 16:02:58 +02:00
|
|
|
"""Return HTML for given markdown. Bugdown may add properties to the
|
|
|
|
message object such as `mentions_user_ids` and `mentions_wildcard`.
|
|
|
|
These are only on this Django object and are not saved in the
|
|
|
|
database.
|
|
|
|
"""
|
2013-08-09 19:15:44 +02:00
|
|
|
global bugdown
|
|
|
|
if bugdown is None:
|
|
|
|
from zerver.lib import bugdown
|
2013-06-28 16:02:58 +02:00
|
|
|
|
|
|
|
self.mentions_wildcard = False
|
2014-01-27 16:56:01 +01:00
|
|
|
self.is_me_message = False
|
2013-06-28 16:02:58 +02:00
|
|
|
self.mentions_user_ids = set()
|
2013-09-03 22:41:17 +02:00
|
|
|
self.user_ids_with_alert_words = set()
|
2013-06-28 16:02:58 +02:00
|
|
|
|
2013-09-04 22:44:45 +02:00
|
|
|
if not domain:
|
|
|
|
domain = self.sender.realm.domain
|
2013-07-29 23:57:26 +02:00
|
|
|
if self.sending_client.name == "zephyr_mirror" and domain == "mit.edu":
|
|
|
|
# Use slightly customized Markdown processor for content
|
|
|
|
# delivered via zephyr_mirror
|
|
|
|
domain = "mit.edu/zephyr_mirror"
|
2013-11-08 02:00:12 +01:00
|
|
|
rendered_content = bugdown.convert(content, domain, self)
|
|
|
|
|
2014-01-27 16:56:01 +01:00
|
|
|
# For /me syntax, JS can detect the is_me_message flag
|
|
|
|
# and do special rendering.
|
2013-11-15 01:00:35 +01:00
|
|
|
if content.startswith('/me ') and '\n' not in content:
|
|
|
|
if rendered_content.startswith('<p>') and rendered_content.endswith('</p>'):
|
2014-01-27 16:56:01 +01:00
|
|
|
self.is_me_message = True
|
2013-11-08 02:00:12 +01:00
|
|
|
|
|
|
|
return rendered_content
|
2013-06-28 16:02:58 +02:00
|
|
|
|
2013-07-08 20:46:58 +02:00
|
|
|
def set_rendered_content(self, rendered_content, save = False):
|
2013-06-28 16:02:58 +02:00
|
|
|
"""Set the content on the message.
|
|
|
|
"""
|
2013-08-09 19:15:44 +02:00
|
|
|
global bugdown
|
|
|
|
if bugdown is None:
|
|
|
|
from zerver.lib import bugdown
|
2013-06-28 16:02:58 +02:00
|
|
|
|
|
|
|
self.rendered_content = rendered_content
|
|
|
|
self.rendered_content_version = bugdown.version
|
|
|
|
|
2013-07-08 20:46:58 +02:00
|
|
|
if self.rendered_content is not None:
|
|
|
|
if save:
|
2013-09-20 21:25:51 +02:00
|
|
|
self.save_rendered_content()
|
2013-07-08 20:46:58 +02:00
|
|
|
return True
|
|
|
|
else:
|
2013-06-28 16:02:58 +02:00
|
|
|
return False
|
|
|
|
|
2013-09-20 21:25:51 +02:00
|
|
|
def save_rendered_content(self):
|
|
|
|
self.save(update_fields=["rendered_content", "rendered_content_version"])
|
|
|
|
|
2013-09-04 22:44:45 +02:00
|
|
|
def maybe_render_content(self, domain, save = False):
|
2013-06-28 16:02:58 +02:00
|
|
|
"""Render the markdown if there is no existing rendered_content"""
|
2013-08-09 19:15:44 +02:00
|
|
|
global bugdown
|
|
|
|
if bugdown is None:
|
|
|
|
from zerver.lib import bugdown
|
|
|
|
|
2013-09-21 16:46:28 +02:00
|
|
|
if Message.need_to_render_content(self.rendered_content, self.rendered_content_version):
|
2013-09-04 22:44:45 +02:00
|
|
|
return self.set_rendered_content(self.render_markdown(self.content, domain), save)
|
2013-06-28 16:02:58 +02:00
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2013-09-21 16:46:28 +02:00
|
|
|
@staticmethod
|
|
|
|
def need_to_render_content(rendered_content, rendered_content_version):
|
|
|
|
return rendered_content_version < bugdown.version or rendered_content is None
|
|
|
|
|
2013-06-28 16:02:58 +02:00
|
|
|
def to_dict(self, apply_markdown):
|
|
|
|
return extract_message_dict(self.to_dict_json(apply_markdown))
|
2013-06-18 20:02:45 +02:00
|
|
|
|
|
|
|
@cache_with_key(to_dict_cache_key, timeout=3600*24)
|
2013-06-28 16:02:58 +02:00
|
|
|
def to_dict_json(self, apply_markdown):
|
|
|
|
return stringify_message_dict(self.to_dict_uncached(apply_markdown))
|
2013-04-25 20:41:54 +02:00
|
|
|
|
2013-06-28 16:02:58 +02:00
|
|
|
def to_dict_uncached(self, apply_markdown):
|
2013-09-21 17:40:20 +02:00
|
|
|
return Message.build_message_dict(
|
|
|
|
apply_markdown = apply_markdown,
|
|
|
|
message = self,
|
|
|
|
message_id = self.id,
|
|
|
|
last_edit_time = self.last_edit_time,
|
|
|
|
edit_history = self.edit_history,
|
|
|
|
content = self.content,
|
|
|
|
subject = self.subject,
|
|
|
|
pub_date = self.pub_date,
|
|
|
|
rendered_content = self.rendered_content,
|
|
|
|
rendered_content_version = self.rendered_content_version,
|
|
|
|
sender_id = self.sender.id,
|
|
|
|
sender_email = self.sender.email,
|
|
|
|
sender_realm_domain = self.sender.realm.domain,
|
|
|
|
sender_full_name = self.sender.full_name,
|
|
|
|
sender_short_name = self.sender.short_name,
|
|
|
|
sender_avatar_source = self.sender.avatar_source,
|
|
|
|
sending_client_name = self.sending_client.name,
|
|
|
|
recipient_id = self.recipient.id,
|
|
|
|
recipient_type = self.recipient.type,
|
|
|
|
recipient_type_id = self.recipient.type_id,
|
|
|
|
)
|
|
|
|
|
2013-09-21 18:43:43 +02:00
|
|
|
@staticmethod
|
|
|
|
def build_dict_from_raw_db_row(row, apply_markdown):
|
|
|
|
'''
|
|
|
|
row is a row from a .values() call, and it needs to have
|
|
|
|
all the relevant fields populated
|
|
|
|
'''
|
|
|
|
return Message.build_message_dict(
|
|
|
|
apply_markdown = apply_markdown,
|
|
|
|
message = None,
|
|
|
|
message_id = row['id'],
|
|
|
|
last_edit_time = row['last_edit_time'],
|
|
|
|
edit_history = row['edit_history'],
|
|
|
|
content = row['content'],
|
|
|
|
subject = row['subject'],
|
|
|
|
pub_date = row['pub_date'],
|
|
|
|
rendered_content = row['rendered_content'],
|
|
|
|
rendered_content_version = row['rendered_content_version'],
|
|
|
|
sender_id = row['sender_id'],
|
|
|
|
sender_email = row['sender__email'],
|
|
|
|
sender_realm_domain = row['sender__realm__domain'],
|
|
|
|
sender_full_name = row['sender__full_name'],
|
|
|
|
sender_short_name = row['sender__short_name'],
|
|
|
|
sender_avatar_source = row['sender__avatar_source'],
|
|
|
|
sending_client_name = row['sending_client__name'],
|
|
|
|
recipient_id = row['recipient_id'],
|
|
|
|
recipient_type = row['recipient__type'],
|
|
|
|
recipient_type_id = row['recipient__type_id'],
|
|
|
|
)
|
|
|
|
|
2013-09-21 17:40:20 +02:00
|
|
|
@staticmethod
|
|
|
|
def build_message_dict(
|
|
|
|
apply_markdown,
|
|
|
|
message,
|
|
|
|
message_id,
|
|
|
|
last_edit_time,
|
|
|
|
edit_history,
|
|
|
|
content,
|
|
|
|
subject,
|
|
|
|
pub_date,
|
|
|
|
rendered_content,
|
|
|
|
rendered_content_version,
|
|
|
|
sender_id,
|
|
|
|
sender_email,
|
|
|
|
sender_realm_domain,
|
|
|
|
sender_full_name,
|
|
|
|
sender_short_name,
|
|
|
|
sender_avatar_source,
|
|
|
|
sending_client_name,
|
|
|
|
recipient_id,
|
|
|
|
recipient_type,
|
|
|
|
recipient_type_id,
|
|
|
|
):
|
2013-08-09 19:15:44 +02:00
|
|
|
global bugdown
|
|
|
|
if bugdown is None:
|
|
|
|
from zerver.lib import bugdown
|
2013-09-21 15:41:39 +02:00
|
|
|
|
2013-09-21 16:32:29 +02:00
|
|
|
avatar_url = get_avatar_url(sender_avatar_source, sender_email)
|
|
|
|
|
2013-09-21 15:41:39 +02:00
|
|
|
display_recipient = get_display_recipient_by_id(
|
|
|
|
recipient_id,
|
|
|
|
recipient_type,
|
|
|
|
recipient_type_id
|
|
|
|
)
|
|
|
|
|
|
|
|
if recipient_type == Recipient.STREAM:
|
2012-12-03 19:49:12 +01:00
|
|
|
display_type = "stream"
|
2013-09-21 15:41:39 +02:00
|
|
|
elif recipient_type in (Recipient.HUDDLE, Recipient.PERSONAL):
|
2012-12-03 19:49:12 +01:00
|
|
|
display_type = "private"
|
|
|
|
if len(display_recipient) == 1:
|
|
|
|
# add the sender in if this isn't a message between
|
|
|
|
# someone and his self, preserving ordering
|
2013-09-21 16:19:52 +02:00
|
|
|
recip = {'email': sender_email,
|
|
|
|
'domain': sender_realm_domain,
|
|
|
|
'full_name': sender_full_name,
|
|
|
|
'short_name': sender_short_name,
|
|
|
|
'id': sender_id};
|
2012-12-03 19:49:12 +01:00
|
|
|
if recip['email'] < display_recipient[0]['email']:
|
|
|
|
display_recipient = [recip, display_recipient[0]]
|
|
|
|
elif recip['email'] > display_recipient[0]['email']:
|
|
|
|
display_recipient = [display_recipient[0], recip]
|
|
|
|
|
2012-10-24 20:16:26 +02:00
|
|
|
obj = dict(
|
2013-09-21 16:19:52 +02:00
|
|
|
id = message_id,
|
|
|
|
sender_email = sender_email,
|
|
|
|
sender_full_name = sender_full_name,
|
|
|
|
sender_short_name = sender_short_name,
|
|
|
|
sender_domain = sender_realm_domain,
|
|
|
|
sender_id = sender_id,
|
2012-12-03 19:49:12 +01:00
|
|
|
type = display_type,
|
|
|
|
display_recipient = display_recipient,
|
2013-09-21 15:41:39 +02:00
|
|
|
recipient_id = recipient_id,
|
2013-09-21 16:19:52 +02:00
|
|
|
subject = subject,
|
|
|
|
timestamp = datetime_to_timestamp(pub_date),
|
|
|
|
gravatar_hash = gravatar_hash(sender_email), # Deprecated June 2013
|
2013-09-21 16:32:29 +02:00
|
|
|
avatar_url = avatar_url,
|
2013-09-21 16:19:52 +02:00
|
|
|
client = sending_client_name)
|
2012-10-24 20:16:26 +02:00
|
|
|
|
2013-09-21 16:19:52 +02:00
|
|
|
obj['subject_links'] = bugdown.subject_links(sender_realm_domain.lower(), subject)
|
2013-07-12 22:29:25 +02:00
|
|
|
|
2013-09-21 16:19:52 +02:00
|
|
|
if last_edit_time != None:
|
|
|
|
obj['last_edit_timestamp'] = datetime_to_timestamp(last_edit_time)
|
|
|
|
obj['edit_history'] = ujson.loads(edit_history)
|
2013-07-08 20:46:58 +02:00
|
|
|
|
|
|
|
if apply_markdown:
|
2013-09-21 17:29:27 +02:00
|
|
|
if Message.need_to_render_content(rendered_content, rendered_content_version):
|
2013-09-21 18:43:43 +02:00
|
|
|
if message is None:
|
|
|
|
# We really shouldn't be rendering objects in this method, but there is
|
|
|
|
# a scenario where we upgrade the version of bugdown and fail to run
|
|
|
|
# management commands to re-render historical messages, and then we
|
|
|
|
# need to have side effects. This method is optimized to not need full
|
|
|
|
# blown ORM objects, but the bugdown renderer is unfortunately highly
|
|
|
|
# coupled to Message, and we also need to persist the new rendered content.
|
|
|
|
# If we don't have a message object passed in, we get one here. The cost
|
|
|
|
# of going to the DB here should be overshadowed by the cost of rendering
|
|
|
|
# and updating the row.
|
|
|
|
message = Message.objects.select_related().get(id=message_id)
|
|
|
|
|
2013-09-21 17:29:27 +02:00
|
|
|
# It's unfortunate that we need to have side effects on the message
|
|
|
|
# in some cases.
|
|
|
|
rendered_content = message.render_markdown(content, sender_realm_domain)
|
|
|
|
message.set_rendered_content(rendered_content, True)
|
|
|
|
|
|
|
|
if rendered_content is not None:
|
|
|
|
obj['content'] = rendered_content
|
2013-06-28 16:02:58 +02:00
|
|
|
else:
|
2013-07-10 22:00:47 +02:00
|
|
|
obj['content'] = '<p>[Zulip note: Sorry, we could not understand the formatting of your message]</p>'
|
2013-06-28 16:02:58 +02:00
|
|
|
|
2012-10-24 20:51:45 +02:00
|
|
|
obj['content_type'] = 'text/html'
|
2012-10-04 00:13:03 +02:00
|
|
|
else:
|
2013-09-21 16:19:52 +02:00
|
|
|
obj['content'] = content
|
2012-10-24 20:51:45 +02:00
|
|
|
obj['content_type'] = 'text/x-markdown'
|
2012-10-24 20:16:26 +02:00
|
|
|
|
|
|
|
return obj
|
2012-08-30 19:56:15 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
def to_log_dict(self):
|
2012-10-24 20:16:26 +02:00
|
|
|
return dict(
|
|
|
|
id = self.id,
|
2013-03-28 20:43:34 +01:00
|
|
|
sender_email = self.sender.email,
|
2013-05-02 18:56:42 +02:00
|
|
|
sender_domain = self.sender.realm.domain,
|
2012-10-24 20:16:26 +02:00
|
|
|
sender_full_name = self.sender.full_name,
|
|
|
|
sender_short_name = self.sender.short_name,
|
|
|
|
sending_client = self.sending_client.name,
|
|
|
|
type = self.recipient.type_name(),
|
2012-12-03 19:49:12 +01:00
|
|
|
recipient = get_display_recipient(self.recipient),
|
2012-10-24 20:16:26 +02:00
|
|
|
subject = self.subject,
|
|
|
|
content = self.content,
|
2012-12-11 23:08:17 +01:00
|
|
|
timestamp = datetime_to_timestamp(self.pub_date))
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2013-09-20 02:19:25 +02:00
|
|
|
@staticmethod
|
2013-09-21 18:43:43 +02:00
|
|
|
def get_raw_db_rows(needed_ids):
|
2013-09-20 02:19:25 +02:00
|
|
|
# This is a special purpose function optimized for
|
|
|
|
# callers like get_old_messages_backend().
|
2013-09-21 18:43:43 +02:00
|
|
|
fields = [
|
|
|
|
'id',
|
|
|
|
'subject',
|
|
|
|
'pub_date',
|
|
|
|
'last_edit_time',
|
|
|
|
'edit_history',
|
|
|
|
'content',
|
|
|
|
'rendered_content',
|
|
|
|
'rendered_content_version',
|
|
|
|
'recipient_id',
|
|
|
|
'recipient__type',
|
|
|
|
'recipient__type_id',
|
|
|
|
'sender_id',
|
|
|
|
'sending_client__name',
|
|
|
|
'sender__email',
|
|
|
|
'sender__full_name',
|
|
|
|
'sender__short_name',
|
|
|
|
'sender__realm__id',
|
|
|
|
'sender__realm__domain',
|
|
|
|
'sender__avatar_source'
|
|
|
|
]
|
|
|
|
return Message.objects.filter(id__in=needed_ids).values(*fields)
|
2013-09-20 02:19:25 +02:00
|
|
|
|
2012-11-28 00:50:33 +01:00
|
|
|
@classmethod
|
|
|
|
def remove_unreachable(cls):
|
|
|
|
"""Remove all Messages that are not referred to by any UserMessage."""
|
|
|
|
cls.objects.exclude(id__in = UserMessage.objects.values('message_id')).delete()
|
|
|
|
|
2013-12-31 22:42:38 +01:00
|
|
|
def sent_by_human(self):
|
|
|
|
sending_client = self.sending_client.name.lower()
|
|
|
|
|
|
|
|
return (sending_client in ('zulipandroid', 'zulipios', 'zulipdesktop',
|
|
|
|
'website', 'ios', 'android')) or \
|
|
|
|
('desktop app' in sending_client)
|
|
|
|
|
2012-09-07 17:04:41 +02:00
|
|
|
class UserMessage(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
2012-10-03 21:05:48 +02:00
|
|
|
message = models.ForeignKey(Message)
|
2012-09-07 17:04:41 +02:00
|
|
|
# We're not using the archived field for now, but create it anyway
|
|
|
|
# since this table will be an unpleasant one to do schema changes
|
|
|
|
# on later
|
2013-07-25 22:08:16 +02:00
|
|
|
ALL_FLAGS = ['read', 'starred', 'collapsed', 'mentioned', 'wildcard_mentioned',
|
2013-09-03 22:41:17 +02:00
|
|
|
'summarize_in_home', 'summarize_in_stream', 'force_expand', 'force_collapse',
|
2014-01-27 16:56:01 +01:00
|
|
|
'has_alert_word', "historical", 'is_me_message']
|
2013-06-19 20:43:45 +02:00
|
|
|
flags = BitField(flags=ALL_FLAGS, default=0)
|
2012-09-07 17:04:41 +02:00
|
|
|
|
2012-11-08 21:08:13 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("user_profile", "message")
|
|
|
|
|
2012-09-07 17:04:41 +02:00
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self.message.recipient)
|
2013-06-25 20:05:13 +02:00
|
|
|
return (u"<UserMessage: %s / %s (%s)>" % (display_recipient, self.user_profile.email, self.flags_list())).encode("utf-8")
|
2012-09-07 17:04:41 +02:00
|
|
|
|
2013-06-19 21:05:22 +02:00
|
|
|
def flags_list(self):
|
|
|
|
return [flag for flag in self.flags.keys() if getattr(self.flags, flag).is_set]
|
2013-03-11 15:47:29 +01:00
|
|
|
|
2013-06-19 20:43:45 +02:00
|
|
|
def parse_usermessage_flags(val):
|
|
|
|
flags = []
|
|
|
|
mask = 1
|
|
|
|
for flag in UserMessage.ALL_FLAGS:
|
|
|
|
if val & mask:
|
|
|
|
flags.append(flag)
|
|
|
|
mask <<= 1
|
|
|
|
return flags
|
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
class Subscription(models.Model):
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
2012-09-05 21:55:40 +02:00
|
|
|
recipient = models.ForeignKey(Recipient)
|
2012-08-30 18:03:58 +02:00
|
|
|
active = models.BooleanField(default=True)
|
2013-01-15 22:31:46 +01:00
|
|
|
in_home_view = models.NullBooleanField(default=True)
|
2012-08-29 17:50:36 +02:00
|
|
|
|
2013-03-29 20:57:02 +01:00
|
|
|
DEFAULT_STREAM_COLOR = "#c2c2c2"
|
|
|
|
color = models.CharField(max_length=10, default=DEFAULT_STREAM_COLOR)
|
2014-02-05 23:00:46 +01:00
|
|
|
|
|
|
|
desktop_notifications = models.BooleanField(default=True)
|
|
|
|
audible_notifications = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
# Combination desktop + audible notifications superseded by the
|
|
|
|
# above.
|
2013-04-08 17:29:33 +02:00
|
|
|
notifications = models.BooleanField(default=False)
|
2013-03-29 20:57:02 +01:00
|
|
|
|
2012-11-07 22:33:38 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("user_profile", "recipient")
|
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
def __repr__(self):
|
2013-03-26 19:46:47 +01:00
|
|
|
return (u"<Subscription: %r -> %s>" % (self.user_profile, self.recipient)).encode("utf-8")
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2013-03-26 19:09:45 +01:00
|
|
|
@cache_with_key(user_profile_by_id_cache_key, timeout=3600*24*7)
|
2013-03-26 18:51:55 +01:00
|
|
|
def get_user_profile_by_id(uid):
|
|
|
|
return UserProfile.objects.select_related().get(id=uid)
|
|
|
|
|
2013-03-28 20:20:31 +01:00
|
|
|
@cache_with_key(user_profile_by_email_cache_key, timeout=3600*24*7)
|
|
|
|
def get_user_profile_by_email(email):
|
2014-01-08 00:07:53 +01:00
|
|
|
return UserProfile.objects.select_related().get(email__iexact=email.strip())
|
2013-03-28 20:20:31 +01:00
|
|
|
|
2013-10-23 23:16:39 +02:00
|
|
|
@cache_with_key(active_user_dicts_in_realm_cache_key, timeout=3600*24*7)
|
|
|
|
def get_active_user_dicts_in_realm(realm):
|
|
|
|
return UserProfile.objects.filter(realm=realm, is_active=True) \
|
|
|
|
.values('id', 'full_name', 'short_name', 'email', 'is_bot')
|
2013-08-28 20:25:31 +02:00
|
|
|
|
2013-04-08 18:27:07 +02:00
|
|
|
def get_prereg_user_by_email(email):
|
|
|
|
# A user can be invited many times, so only return the result of the latest
|
|
|
|
# invite.
|
2014-01-08 00:07:53 +01:00
|
|
|
return PreregistrationUser.objects.filter(email__iexact=email.strip()).latest("invited_at")
|
2013-04-08 18:27:07 +02:00
|
|
|
|
2012-09-04 23:20:21 +02:00
|
|
|
class Huddle(models.Model):
|
2012-09-07 20:14:13 +02:00
|
|
|
# TODO: We should consider whether using
|
|
|
|
# CommaSeparatedIntegerField would be better.
|
2012-10-22 19:23:11 +02:00
|
|
|
huddle_hash = models.CharField(max_length=40, db_index=True, unique=True)
|
2012-09-04 23:20:21 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def get_huddle_hash(id_list):
|
2012-09-05 17:38:09 +02:00
|
|
|
id_list = sorted(set(id_list))
|
2012-09-05 17:41:53 +02:00
|
|
|
hash_key = ",".join(str(x) for x in id_list)
|
2013-03-20 15:31:27 +01:00
|
|
|
return make_safe_digest(hash_key)
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2013-03-26 18:17:55 +01:00
|
|
|
def huddle_hash_cache_key(huddle_hash):
|
|
|
|
return "huddle_by_hash:%s" % (huddle_hash,)
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def get_huddle(id_list):
|
|
|
|
huddle_hash = get_huddle_hash(id_list)
|
2013-03-26 18:17:55 +01:00
|
|
|
return get_huddle_backend(huddle_hash, id_list)
|
|
|
|
|
2013-03-26 19:09:45 +01:00
|
|
|
@cache_with_key(lambda huddle_hash, id_list: huddle_hash_cache_key(huddle_hash), timeout=3600*24*7)
|
2013-03-26 18:17:55 +01:00
|
|
|
def get_huddle_backend(huddle_hash, id_list):
|
2012-10-19 23:40:44 +02:00
|
|
|
(huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash)
|
|
|
|
if created:
|
2013-03-26 18:51:55 +01:00
|
|
|
with transaction.commit_on_success():
|
|
|
|
recipient = Recipient.objects.create(type_id=huddle.id,
|
|
|
|
type=Recipient.HUDDLE)
|
|
|
|
subs_to_create = [Subscription(recipient=recipient,
|
|
|
|
user_profile=get_user_profile_by_id(user_profile_id))
|
|
|
|
for user_profile_id in id_list]
|
|
|
|
Subscription.objects.bulk_create(subs_to_create)
|
2012-10-19 23:40:44 +02:00
|
|
|
return huddle
|
2012-09-04 23:20:21 +02:00
|
|
|
|
2013-05-31 20:04:18 +02:00
|
|
|
def get_realm(domain):
|
2013-08-02 20:32:56 +02:00
|
|
|
if not domain:
|
|
|
|
return None
|
2013-05-31 20:04:18 +02:00
|
|
|
try:
|
|
|
|
return Realm.objects.get(domain__iexact=domain.strip())
|
|
|
|
except Realm.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2012-10-29 19:43:00 +01:00
|
|
|
def clear_database():
|
2013-07-08 17:53:50 +02:00
|
|
|
pylibmc.Client(['127.0.0.1']).flush_all()
|
2013-04-01 16:57:50 +02:00
|
|
|
for model in [Message, Stream, UserProfile, Recipient,
|
2012-11-27 18:26:51 +01:00
|
|
|
Realm, Subscription, Huddle, UserMessage, Client,
|
|
|
|
DefaultStream]:
|
2012-10-29 19:43:00 +01:00
|
|
|
model.objects.all().delete()
|
|
|
|
Session.objects.all().delete()
|
2012-11-08 23:02:16 +01:00
|
|
|
|
|
|
|
class UserActivity(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
|
|
|
client = models.ForeignKey(Client)
|
|
|
|
query = models.CharField(max_length=50, db_index=True)
|
|
|
|
|
|
|
|
count = models.IntegerField()
|
|
|
|
last_visit = models.DateTimeField('last visit')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("user_profile", "client", "query")
|
2012-11-27 18:26:51 +01:00
|
|
|
|
2013-09-06 21:52:12 +02:00
|
|
|
class UserActivityInterval(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
|
|
|
start = models.DateTimeField('start time', db_index=True)
|
|
|
|
end = models.DateTimeField('end time', db_index=True)
|
|
|
|
|
2013-02-08 23:44:15 +01:00
|
|
|
class UserPresence(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
|
|
|
client = models.ForeignKey(Client)
|
|
|
|
|
|
|
|
# Valid statuses
|
|
|
|
ACTIVE = 1
|
|
|
|
IDLE = 2
|
|
|
|
|
|
|
|
timestamp = models.DateTimeField('presence changed')
|
|
|
|
status = models.PositiveSmallIntegerField(default=ACTIVE)
|
|
|
|
|
2013-09-13 23:33:11 +02:00
|
|
|
@staticmethod
|
|
|
|
def status_to_string(status):
|
|
|
|
if status == UserPresence.ACTIVE:
|
|
|
|
return 'active'
|
|
|
|
elif status == UserPresence.IDLE:
|
|
|
|
return 'idle'
|
|
|
|
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_status_dict_by_realm(realm_id):
|
|
|
|
user_statuses = defaultdict(dict)
|
|
|
|
|
|
|
|
query = UserPresence.objects.filter(
|
|
|
|
user_profile__realm_id=realm_id,
|
2013-09-16 21:21:28 +02:00
|
|
|
user_profile__is_active=True,
|
|
|
|
user_profile__is_bot=False
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
).values(
|
|
|
|
'client__name',
|
|
|
|
'status',
|
|
|
|
'timestamp',
|
2014-02-26 22:06:57 +01:00
|
|
|
'user_profile__email',
|
|
|
|
'user_profile__id',
|
|
|
|
'user_profile__enable_offline_push_notifications',
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
)
|
|
|
|
|
2014-02-26 22:06:57 +01:00
|
|
|
mobile_user_ids = [row['user'] for row in PushDeviceToken.objects.filter(
|
|
|
|
user__realm_id=1,
|
|
|
|
user__is_active=True,
|
|
|
|
user__is_bot=False,
|
|
|
|
).distinct("user").values("user")]
|
|
|
|
|
|
|
|
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
for row in query:
|
2014-02-14 22:45:32 +01:00
|
|
|
info = UserPresence.to_presence_dict(
|
|
|
|
client_name=row['client__name'],
|
|
|
|
status=row['status'],
|
|
|
|
timestamp=row['timestamp'],
|
2014-02-26 22:06:57 +01:00
|
|
|
push_enabled=row['user_profile__enable_offline_push_notifications'],
|
|
|
|
has_push_devices=row['user_profile__id'] in mobile_user_ids
|
2014-02-14 22:45:32 +01:00
|
|
|
)
|
|
|
|
user_statuses[row['user_profile__email']][row['client__name']] = info
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
|
|
|
|
return user_statuses
|
|
|
|
|
|
|
|
@staticmethod
|
2014-02-26 22:06:57 +01:00
|
|
|
def to_presence_dict(client_name=None, status=None, timestamp=None, push_enabled=None,
|
|
|
|
has_push_devices=None):
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
presence_val = UserPresence.status_to_string(status)
|
|
|
|
timestamp = datetime_to_timestamp(timestamp)
|
|
|
|
return dict(
|
|
|
|
client=client_name,
|
|
|
|
status=presence_val,
|
2014-02-26 22:06:57 +01:00
|
|
|
timestamp=timestamp,
|
|
|
|
pushable=(push_enabled and has_push_devices),
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
)
|
2013-04-03 22:00:02 +02:00
|
|
|
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
def to_dict(self):
|
2014-02-14 22:45:32 +01:00
|
|
|
return UserPresence.to_presence_dict(
|
|
|
|
client_name=self.client.name,
|
|
|
|
status=self.status,
|
|
|
|
timestamp=self.timestamp
|
Optimize user presence/activity query.
The get_status_dict_by_realm helper gets called whenever our
realm user_presences cache expires, and it used to query these fields:
"zerver_userpresence"."id", "zerver_userpresence"."user_profile_id", "zerver_userpresence"."client_id", "zerver_userpresence"."timestamp", "zerver_userpresence"."status", "zerver_userprofile"."id", "zerver_userprofile"."password", "zerver_userprofile"."last_login", "zerver_userprofile"."is_superuser", "zerver_userprofile"."email", "zerver_userprofile"."is_staff", "zerver_userprofile"."is_active", "zerver_userprofile"."is_bot", "zerver_userprofile"."date_joined", "zerver_userprofile"."bot_owner_id", "zerver_userprofile"."full_name", "zerver_userprofile"."short_name", "zerver_userprofile"."pointer", "zerver_userprofile"."last_pointer_updater", "zerver_userprofile"."realm_id", "zerver_userprofile"."api_key", "zerver_userprofile"."enable_desktop_notifications", "zerver_userprofile"."enable_sounds", "zerver_userprofile"."enter_sends", "zerver_userprofile"."enable_offline_email_notifications", "zerver_userprofile"."last_reminder", "zerver_userprofile"."rate_limits", "zerver_userprofile"."avatar_source", "zerver_userprofile"."tutorial_status", "zerver_userprofile"."onboarding_steps", "zerver_userprofile"."invites_granted", "zerver_userprofile"."invites_used", "zerver_userprofile"."alert_words", "zerver_userprofile"."muted_topics", "zerver_client"."id", "zerver_client"."name"
Now it queries just the fields it needs:
"zerver_client"."name", "zerver_userpresence"."status", "zerver_userpresence"."timestamp", "zerver_userprofile"."email" FROM "zerver_userpresence"
Also, get_status_dict_by_realm is now namespaced under UserPresence as a static method.
(imported from commit be1266844b6bd28b6c615594796713c026a850a1)
2013-09-14 23:59:03 +02:00
|
|
|
)
|
2013-04-03 22:00:02 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def status_from_string(status):
|
|
|
|
if status == 'active':
|
|
|
|
status_val = UserPresence.ACTIVE
|
|
|
|
elif status == 'idle':
|
|
|
|
status_val = UserPresence.IDLE
|
|
|
|
else:
|
|
|
|
status_val = None
|
|
|
|
|
|
|
|
return status_val
|
|
|
|
|
2013-02-08 23:44:15 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("user_profile", "client")
|
|
|
|
|
2012-11-27 18:26:51 +01:00
|
|
|
class DefaultStream(models.Model):
|
|
|
|
realm = models.ForeignKey(Realm)
|
|
|
|
stream = models.ForeignKey(Stream)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("realm", "stream")
|
2012-12-01 04:35:59 +01:00
|
|
|
|
2012-12-05 22:23:45 +01:00
|
|
|
# FIXME: The foreign key relationship here is backwards.
|
|
|
|
#
|
|
|
|
# We can't easily get a list of streams and their associated colors (if any) in
|
2013-07-29 23:03:31 +02:00
|
|
|
# a single query. See zerver.views.gather_subscriptions for an example.
|
2012-12-05 22:23:45 +01:00
|
|
|
#
|
|
|
|
# We should change things around so that is possible. Probably this should
|
|
|
|
# just be a column on Subscription.
|
2012-12-01 04:35:59 +01:00
|
|
|
class StreamColor(models.Model):
|
2013-01-28 23:06:35 +01:00
|
|
|
DEFAULT_STREAM_COLOR = "#c2c2c2"
|
|
|
|
|
2012-12-01 04:35:59 +01:00
|
|
|
subscription = models.ForeignKey(Subscription)
|
|
|
|
color = models.CharField(max_length=10)
|
2013-07-26 16:51:02 +02:00
|
|
|
|
|
|
|
class Referral(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
|
|
|
email = models.EmailField(blank=False, null=False)
|
|
|
|
timestamp = models.DateTimeField(auto_now_add=True, null=False)
|
2013-11-06 00:47:59 +01:00
|
|
|
|
|
|
|
# This table only gets used on Zulip Enterprise instances
|
|
|
|
# For reasons of deliverability (and sending from multiple email addresses),
|
|
|
|
# we will still send from mandrill when we send things from the (staging.)zulip.com install
|
|
|
|
class ScheduledJob(models.Model):
|
|
|
|
scheduled_timestamp = models.DateTimeField(auto_now_add=False, null=False)
|
|
|
|
type = models.PositiveSmallIntegerField()
|
|
|
|
# Valid types are {email}
|
|
|
|
# for EMAIL, filter_string is recipient_email
|
|
|
|
EMAIL = 1
|
|
|
|
|
|
|
|
# JSON representation of the job's data. Be careful, as we are not relying on Django to do validation
|
|
|
|
data = models.TextField()
|
|
|
|
# Kind if like a ForeignKey, but table is determined by type.
|
|
|
|
filter_id = models.IntegerField(null=True)
|
|
|
|
filter_string = models.CharField(max_length=100)
|