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
|
2012-08-28 18:44:51 +02:00
|
|
|
from django.contrib.auth.models import User
|
2012-09-04 23:20:21 +02:00
|
|
|
import hashlib
|
2012-09-21 17:25:20 +02:00
|
|
|
import base64
|
2012-09-12 22:35:06 +02:00
|
|
|
import calendar
|
2012-09-19 18:41:42 +02:00
|
|
|
from zephyr.lib.cache import cache_with_key
|
2012-09-27 19:58:42 +02:00
|
|
|
import fcntl
|
|
|
|
import os
|
2012-10-04 22:26:59 +02:00
|
|
|
import re
|
2012-09-27 19:58:42 +02:00
|
|
|
import simplejson
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-09-26 20:41:54 +02:00
|
|
|
import markdown
|
|
|
|
md_engine = markdown.Markdown(
|
2012-10-02 20:19:13 +02:00
|
|
|
extensions = ['fenced_code', 'codehilite', 'nl2br'],
|
2012-09-26 20:41:54 +02:00
|
|
|
output_format = 'xhtml' )
|
2012-09-21 16:10:36 +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 get_display_recipient(recipient):
|
|
|
|
"""
|
2012-10-11 00:01:39 +02:00
|
|
|
recipient: an subject of 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
|
|
|
|
2012-10-10 22:58:51 +02:00
|
|
|
returns: an appropriate string describing the recipient (the stream
|
|
|
|
name, for a stream, or the email, for a user).
|
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-10 22:57:21 +02:00
|
|
|
if recipient.type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=recipient.type_id)
|
|
|
|
return stream.name
|
2012-09-07 20:14:13 +02:00
|
|
|
elif recipient.type == Recipient.HUDDLE:
|
2012-09-26 19:30:01 +02:00
|
|
|
user_profile_list = [UserProfile.objects.get(user=s.userprofile) for s in
|
|
|
|
Subscription.objects.filter(recipient=recipient)]
|
2012-09-26 21:25:49 +02:00
|
|
|
return [{'email': user_profile.user.email,
|
|
|
|
'name': user_profile.short_name} for user_profile in user_profile_list]
|
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
|
|
|
else:
|
2012-09-10 19:43:11 +02:00
|
|
|
user = User.objects.get(id=recipient.type_id)
|
2012-09-21 00:26:59 +02:00
|
|
|
return user.email
|
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-09-27 19:58:42 +02:00
|
|
|
def get_log_recipient(recipient):
|
|
|
|
"""
|
2012-10-11 00:01:39 +02:00
|
|
|
recipient: an subject of Recipient.
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-10 22:58:51 +02:00
|
|
|
returns: an appropriate string describing the recipient (the stream
|
|
|
|
name, for a stream, or the email, for a user).
|
2012-09-27 19:58:42 +02:00
|
|
|
"""
|
2012-10-10 22:57:21 +02:00
|
|
|
if recipient.type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=recipient.type_id)
|
|
|
|
return stream.name
|
2012-09-27 19:58:42 +02:00
|
|
|
|
|
|
|
user_profile_list = [UserProfile.objects.get(user=s.userprofile) for s in
|
|
|
|
Subscription.objects.filter(recipient=recipient)]
|
|
|
|
return [{'email': user_profile.user.email,
|
|
|
|
'full_name': user_profile.full_name,
|
|
|
|
'short_name': user_profile.short_name} for user_profile in user_profile_list]
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
callback_table = {}
|
2012-09-26 16:44:11 +02:00
|
|
|
mit_sync_table = {}
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-09-05 21:49:56 +02:00
|
|
|
class Realm(models.Model):
|
2012-09-14 23:28:38 +02:00
|
|
|
domain = models.CharField(max_length=40, db_index=True)
|
2012-09-05 21:49:56 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Realm: %s %s>" % (self.domain, self.id)
|
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
|
|
|
|
2012-10-01 21:36:44 +02:00
|
|
|
def gen_api_key():
|
|
|
|
return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
|
|
|
### TODO: For now, everyone has the same (fixed) API key to make
|
|
|
|
### testing easier. Uncomment the following to generate them randomly
|
|
|
|
### in a reasonable way. Long-term, we should use a real
|
|
|
|
### cryptographic random number generator.
|
|
|
|
|
|
|
|
# return hex(random.getrandbits(4*32))[2:34]
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class UserProfile(models.Model):
|
|
|
|
user = models.OneToOneField(User)
|
2012-09-11 19:20:01 +02:00
|
|
|
full_name = models.CharField(max_length=100)
|
|
|
|
short_name = models.CharField(max_length=100)
|
2012-08-28 18:44:51 +02:00
|
|
|
pointer = models.IntegerField()
|
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)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
# The user receives this message
|
|
|
|
def receive(self, message):
|
|
|
|
global callback_table
|
|
|
|
|
|
|
|
# Should also store in permanent database the receipt
|
2012-09-07 17:04:41 +02:00
|
|
|
um = UserMessage(user_profile=self, message=message)
|
|
|
|
um.save()
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
for cb in callback_table.get(self.user.id, []):
|
|
|
|
cb([message])
|
|
|
|
|
|
|
|
callback_table[self.user.id] = []
|
|
|
|
|
2012-09-28 21:53:20 +02:00
|
|
|
def add_callback(self, cb):
|
2012-08-28 22:56:21 +02:00
|
|
|
global callback_table
|
|
|
|
callback_table.setdefault(self.user.id, []).append(cb)
|
|
|
|
|
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):
|
2012-09-21 00:26:59 +02:00
|
|
|
return "<UserProfile: %s %s>" % (self.user.email, self.realm)
|
2012-09-05 21:49:56 +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
|
|
|
|
2012-09-19 18:54:57 +02:00
|
|
|
@classmethod
|
|
|
|
def create(cls, user, realm, full_name, short_name):
|
|
|
|
"""When creating a new user, make a profile for him or her."""
|
|
|
|
if not cls.objects.filter(user=user):
|
|
|
|
profile = cls(user=user, pointer=-1, realm_id=realm.id,
|
|
|
|
full_name=full_name, short_name=short_name)
|
2012-10-01 21:36:44 +02:00
|
|
|
profile.api_key = gen_api_key()
|
2012-09-19 18:54:57 +02:00
|
|
|
profile.save()
|
|
|
|
# Auto-sub to the ability to receive personals.
|
|
|
|
recipient = Recipient(type_id=profile.id, type=Recipient.PERSONAL)
|
|
|
|
recipient.save()
|
|
|
|
Subscription(userprofile=profile, recipient=recipient).save()
|
2012-08-29 17:50:36 +02:00
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
class PreregistrationUser(models.Model):
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
# 0 is inactive, 1 is active
|
|
|
|
status = models.IntegerField(default=0)
|
|
|
|
|
2012-09-21 16:40:46 +02:00
|
|
|
def create_user(email, password, realm, full_name, short_name):
|
2012-09-21 17:25:20 +02:00
|
|
|
# NB: the result of Base32 + truncation is not a valid Base32 encoding.
|
|
|
|
# It's just a unique alphanumeric string.
|
|
|
|
# Use base32 instead of base64 so we don't have to worry about mixed case.
|
|
|
|
# Django imposes a limit of 30 characters on usernames.
|
|
|
|
email_hash = hashlib.sha256(settings.HASH_SALT + email).digest()
|
|
|
|
username = base64.b32encode(email_hash)[:30]
|
2012-09-21 16:40:46 +02:00
|
|
|
user = User.objects.create_user(username=username, password=password,
|
|
|
|
email=email)
|
|
|
|
user.save()
|
|
|
|
UserProfile.create(user, realm, full_name, short_name)
|
|
|
|
|
2012-09-27 20:46:42 +02:00
|
|
|
def create_user_if_needed(realm, email, password, full_name, short_name):
|
|
|
|
try:
|
|
|
|
return User.objects.get(email=email)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
# forge a user for this person
|
|
|
|
create_user(email, password, realm,
|
|
|
|
full_name, short_name)
|
|
|
|
user = User.objects.get(email=email)
|
|
|
|
return user
|
|
|
|
|
2012-10-10 22:58:51 +02:00
|
|
|
def create_stream_if_needed(realm, stream_name):
|
2012-09-27 20:46:42 +02:00
|
|
|
try:
|
2012-10-10 22:58:51 +02:00
|
|
|
return Stream.objects.get(name__iexact=stream_name, realm=realm)
|
2012-10-10 22:53:24 +02:00
|
|
|
except Stream.DoesNotExist:
|
2012-10-10 22:58:51 +02:00
|
|
|
stream = Stream()
|
|
|
|
stream.name = stream_name
|
|
|
|
stream.realm = realm
|
|
|
|
stream.save()
|
|
|
|
recipient = Recipient(type_id=stream.id, type=Recipient.STREAM)
|
2012-09-27 20:46:42 +02:00
|
|
|
recipient.save()
|
2012-10-10 22:58:51 +02:00
|
|
|
return stream
|
2012-09-27 20:46:42 +02:00
|
|
|
|
|
|
|
|
2012-10-10 22:53:24 +02:00
|
|
|
class Stream(models.Model):
|
2012-09-14 23:28:38 +02:00
|
|
|
name = models.CharField(max_length=30, db_index=True)
|
|
|
|
realm = models.ForeignKey(Realm, 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):
|
2012-10-10 22:53:24 +02:00
|
|
|
return "<Stream: %s>" % (self.name,)
|
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
|
|
|
|
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-10 22:57:21 +02:00
|
|
|
recipient = Recipient(type_id=stream.id, type=Recipient.STREAM)
|
2012-09-19 18:54:57 +02:00
|
|
|
recipient.save()
|
2012-10-10 22:53:24 +02:00
|
|
|
return (stream, recipient)
|
2012-09-07 19:24:54 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
def type_name(self):
|
|
|
|
if self.type == self.PERSONAL:
|
|
|
|
return "personal"
|
2012-10-10 22:57:21 +02:00
|
|
|
elif self.type == self.STREAM:
|
|
|
|
return "stream"
|
2012-09-07 20:14:13 +02:00
|
|
|
elif self.type == self.HUDDLE:
|
|
|
|
return "huddle"
|
|
|
|
else:
|
|
|
|
raise
|
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)
|
2012-09-04 22:22:06 +02:00
|
|
|
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)
|
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-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-10-11 00:19:38 +02:00
|
|
|
subject = models.CharField(max_length=60)
|
2012-09-14 19:16:01 +02:00
|
|
|
content = models.TextField()
|
2012-08-28 18:44:51 +02:00
|
|
|
pub_date = models.DateTimeField('date published')
|
|
|
|
|
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)
|
2012-10-11 00:01:39 +02:00
|
|
|
return "<Message: %s / %s / %r>" % (display_recipient, self.subject, self.sender)
|
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
|
|
|
|
2012-10-04 22:26:59 +02:00
|
|
|
# A link starts after whitespace, and cannot contain spaces,
|
|
|
|
# end parentheses, or end brackets (which would confuse Markdown).
|
|
|
|
link_regex = re.compile(r'(\s|\A)(?P<url>https?://[^\s\])]+)')
|
|
|
|
|
|
|
|
def html_content(self):
|
|
|
|
def linkify(match):
|
|
|
|
url = match.group('url')
|
|
|
|
return ' [%s](%s) ' % (url, url)
|
|
|
|
|
2012-10-05 20:58:38 +02:00
|
|
|
# Escape the # (pound sign) to avoid markdown making them into
|
|
|
|
# (giant) headers.
|
|
|
|
content = self.content.replace("#", "#")
|
|
|
|
with_links = self.link_regex.sub(linkify, content)
|
2012-10-04 22:26:59 +02:00
|
|
|
return md_engine.convert(with_links)
|
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
@cache_with_key(lambda self, apply_markdown: 'message_dict:%d:%d' % (self.id, apply_markdown))
|
2012-09-27 22:14:14 +02:00
|
|
|
def to_dict(self, apply_markdown):
|
|
|
|
if apply_markdown:
|
2012-10-04 22:26:59 +02:00
|
|
|
content = self.html_content()
|
2012-10-04 00:13:03 +02:00
|
|
|
else:
|
|
|
|
content = self.content
|
2012-08-30 19:56:15 +02:00
|
|
|
return {'id' : self.id,
|
2012-09-11 21:20:24 +02:00
|
|
|
'sender_email' : self.sender.user.email,
|
2012-09-11 19:20:01 +02:00
|
|
|
'sender_name' : self.sender.full_name,
|
2012-09-07 20:14:13 +02:00
|
|
|
'type' : self.recipient.type_name(),
|
2012-08-30 19:56:15 +02:00
|
|
|
'display_recipient': get_display_recipient(self.recipient),
|
2012-09-21 22:57:04 +02:00
|
|
|
'recipient_id' : self.recipient.id,
|
2012-10-11 00:01:39 +02:00
|
|
|
'subject' : self.subject,
|
2012-09-27 22:14:14 +02:00
|
|
|
'content' : content,
|
2012-09-12 22:35:06 +02:00
|
|
|
'timestamp' : calendar.timegm(self.pub_date.timetuple()),
|
2012-09-26 22:34:24 +02:00
|
|
|
'gravatar_hash' : hashlib.md5(self.sender.user.email.lower()).hexdigest(),
|
2012-09-11 00:18:09 +02:00
|
|
|
}
|
2012-08-30 19:56:15 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
def to_log_dict(self):
|
|
|
|
return {'id' : self.id,
|
|
|
|
'sender_email' : self.sender.user.email,
|
|
|
|
'sender_full_name' : self.sender.full_name,
|
|
|
|
'sender_short_name': self.sender.full_name,
|
|
|
|
'type' : self.recipient.type_name(),
|
|
|
|
'recipient' : get_log_recipient(self.recipient),
|
2012-10-11 00:01:39 +02:00
|
|
|
'subject' : self.subject,
|
2012-09-27 19:58:42 +02:00
|
|
|
'content' : self.content,
|
|
|
|
'timestamp' : self.pub_date.strftime("%s"),
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
archived = models.BooleanField()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self.message.recipient)
|
2012-09-21 00:26:59 +02:00
|
|
|
return "<UserMessage: %s / %s>" % (display_recipient, self.user_profile.user.email)
|
2012-09-07 17:04:41 +02:00
|
|
|
|
2012-09-14 22:43:54 +02:00
|
|
|
user_hash = {}
|
|
|
|
def get_user_profile_by_id(uid):
|
|
|
|
if uid in user_hash:
|
|
|
|
return user_hash[uid]
|
|
|
|
return UserProfile.objects.get(id=uid)
|
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
def log_message(message):
|
2012-10-03 21:26:40 +02:00
|
|
|
if not os.path.exists(settings.MESSAGE_LOG + '.lock'):
|
|
|
|
file(settings.MESSAGE_LOG + '.lock', "w").write("0")
|
|
|
|
lock = open(settings.MESSAGE_LOG + '.lock', 'r')
|
2012-09-27 19:58:42 +02:00
|
|
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
2012-10-03 21:26:40 +02:00
|
|
|
f = open(settings.MESSAGE_LOG, "a")
|
2012-10-03 21:29:38 +02:00
|
|
|
f.write(simplejson.dumps(message.to_log_dict()) + "\n")
|
2012-09-27 19:58:42 +02:00
|
|
|
f.flush()
|
|
|
|
f.close()
|
|
|
|
fcntl.flock(lock, fcntl.LOCK_UN)
|
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
def do_send_message(message, synced_from_mit=False, no_log=False):
|
|
|
|
message.save()
|
|
|
|
# The following mit_sync_table code must be after message.save() or
|
2012-09-28 21:57:43 +02:00
|
|
|
# otherwise the id returned will be None (not having been assigned
|
|
|
|
# by the database yet)
|
2012-10-03 21:29:38 +02:00
|
|
|
mit_sync_table[message.id] = synced_from_mit
|
2012-09-27 19:58:42 +02:00
|
|
|
# Log the message to our message log for populate_db to refill
|
|
|
|
if not no_log:
|
2012-10-03 21:29:38 +02:00
|
|
|
log_message(message)
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
if message.recipient.type == Recipient.PERSONAL:
|
|
|
|
recipients = list(set([get_user_profile_by_id(message.recipient.type_id),
|
|
|
|
get_user_profile_by_id(message.sender_id)]))
|
|
|
|
# For personals, you send out either 1 or 2 copies of the message, for
|
2012-08-31 16:58:09 +02:00
|
|
|
# personals to yourself or to someone else, respectively.
|
|
|
|
assert((len(recipients) == 1) or (len(recipients) == 2))
|
2012-10-10 22:57:21 +02:00
|
|
|
elif (message.recipient.type == Recipient.STREAM or
|
2012-10-03 21:29:38 +02:00
|
|
|
message.recipient.type == Recipient.HUDDLE):
|
2012-09-14 22:43:54 +02:00
|
|
|
recipients = [get_user_profile_by_id(s.userprofile_id) for
|
2012-10-03 21:29:38 +02:00
|
|
|
s in Subscription.objects.filter(recipient=message.recipient, active=True)]
|
2012-08-28 22:56:21 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
for recipient in recipients:
|
2012-10-03 21:29:38 +02:00
|
|
|
recipient.receive(message)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
class Subscription(models.Model):
|
2012-09-05 21:55:40 +02:00
|
|
|
userprofile = models.ForeignKey(UserProfile)
|
|
|
|
recipient = models.ForeignKey(Recipient)
|
2012-08-30 18:03:58 +02:00
|
|
|
active = models.BooleanField(default=True)
|
2012-08-29 17:50:36 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2012-09-05 21:55:40 +02:00
|
|
|
return "<Subscription: %r -> %r>" % (self.userprofile, self.recipient)
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
2012-08-28 22:56:21 +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-09-14 23:28:38 +02:00
|
|
|
huddle_hash = models.CharField(max_length=40, db_index=True)
|
2012-09-04 23:20:21 +02:00
|
|
|
|
|
|
|
def get_huddle(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)
|
2012-09-04 23:20:21 +02:00
|
|
|
huddle_hash = hashlib.sha1(hash_key).hexdigest()
|
|
|
|
if Huddle.objects.filter(huddle_hash=huddle_hash):
|
|
|
|
return Huddle.objects.get(huddle_hash=huddle_hash)
|
|
|
|
else:
|
|
|
|
# since we don't have one, make a new huddle
|
|
|
|
huddle = Huddle(huddle_hash = huddle_hash)
|
|
|
|
huddle.save()
|
2012-09-10 19:43:11 +02:00
|
|
|
recipient = Recipient(type_id=huddle.id, type=Recipient.HUDDLE)
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient.save()
|
|
|
|
|
|
|
|
# Add subscriptions
|
|
|
|
for uid in id_list:
|
2012-09-05 21:55:40 +02:00
|
|
|
s = Subscription(recipient = recipient,
|
|
|
|
userprofile = UserProfile.objects.get(id=uid))
|
2012-09-04 23:20:21 +02:00
|
|
|
s.save()
|
|
|
|
return huddle
|
|
|
|
|
2012-09-07 19:54:42 +02:00
|
|
|
# This is currently dead code since all the places where we used to
|
|
|
|
# use it now have faster implementations, but I expect this to be
|
|
|
|
# potentially useful for code in the future, so not deleting it yet.
|
2012-10-03 21:29:38 +02:00
|
|
|
def filter_by_subscriptions(messages, user):
|
2012-08-28 22:56:21 +02:00
|
|
|
userprofile = UserProfile.objects.get(user=user)
|
2012-10-03 21:29:38 +02:00
|
|
|
user_messages = []
|
2012-09-07 19:53:24 +02:00
|
|
|
subscriptions = [sub.recipient for sub in
|
|
|
|
Subscription.objects.filter(userprofile=userprofile, active=True)]
|
2012-10-03 21:29:38 +02:00
|
|
|
for message in messages:
|
2012-10-10 22:58:51 +02:00
|
|
|
# If you are subscribed to the personal or stream, or if you
|
2012-10-03 21:29:38 +02:00
|
|
|
# sent the personal, you can see the message.
|
|
|
|
if (message.recipient in subscriptions) or \
|
|
|
|
(message.recipient.type == Recipient.PERSONAL and
|
|
|
|
message.sender == userprofile):
|
|
|
|
user_messages.append(message)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
return user_messages
|