mirror of https://github.com/zulip/zulip.git
[schema] Add a Client model keeping track of the sending client.
(imported from commit 31a430b1de14ce973addafd5d13ace049a8f8091)
This commit is contained in:
parent
2c89103b23
commit
7b3b4362dd
|
@ -18,6 +18,8 @@ class HumbugAPI():
|
|||
def do_api_query(self, request, url):
|
||||
request["email"] = self.email
|
||||
request["api-key"] = self.api_key
|
||||
if "client" not in request:
|
||||
request["client"] = "API"
|
||||
while True:
|
||||
try:
|
||||
res = requests.post(urlparse.urljoin(self.base_url, url), data=request, verify=True)
|
||||
|
|
|
@ -99,6 +99,7 @@ def send_humbug(zeph):
|
|||
elif isinstance(zeph[key], str):
|
||||
zeph[key] = zeph[key].decode("utf-8")
|
||||
|
||||
zeph['client'] = "zephyr_mirror"
|
||||
return humbug_client.send_message(zeph)
|
||||
|
||||
def fetch_fullname(username):
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
<div class="alert" id="send-status"></div>
|
||||
<form action="/json/send_message/" method="post" class="zephyr">
|
||||
<input type="hidden" name="type" value="stream" id="new_message_type"/>
|
||||
<input type="hidden" name="client" value="website" id="client_name"/>
|
||||
{% csrf_token %}
|
||||
<table class="compose_table">
|
||||
<tbody>
|
||||
|
|
|
@ -2,11 +2,12 @@ from django.core.management.base import BaseCommand
|
|||
from django.utils.timezone import utc
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from zephyr.models import Message, UserProfile, Stream, Recipient, \
|
||||
from zephyr.models import Message, UserProfile, Stream, Recipient, Client, \
|
||||
Subscription, Huddle, get_huddle, Realm, UserMessage, get_user_profile_by_id, \
|
||||
bulk_create_realms, bulk_create_streams, bulk_create_users, bulk_create_huddles, \
|
||||
bulk_create_clients, \
|
||||
create_user, do_send_message, create_user_if_needed, create_stream_if_needed, \
|
||||
filter_by_subscriptions, get_huddle_hash
|
||||
filter_by_subscriptions, get_huddle_hash, get_client
|
||||
from zephyr.lib.parallel import run_parallel
|
||||
from zephyr.lib.initial_password import initial_password
|
||||
from django.db import transaction
|
||||
|
@ -106,7 +107,7 @@ class Command(BaseCommand):
|
|||
|
||||
if options["delete"]:
|
||||
for model in [Message, Stream, UserProfile, User, Recipient,
|
||||
Realm, Subscription, Huddle, UserMessage]:
|
||||
Realm, Subscription, Huddle, UserMessage, Client]:
|
||||
model.objects.all().delete()
|
||||
|
||||
# Create a test realm
|
||||
|
@ -224,6 +225,9 @@ def restore_saved_messages():
|
|||
user_set = set()
|
||||
email_set = set(u.email for u in User.objects.all())
|
||||
realm_set = set()
|
||||
# Initial client_set is nonempty temporarily because we don't have
|
||||
# clients in logs at all right now -- later we can start with nothing.
|
||||
client_set = set(["populate_db", "website", "zephyr_mirror"])
|
||||
huddle_user_set = set()
|
||||
# First, determine all the objects our messages will need.
|
||||
print datetime.datetime.now(), "Creating realms/streams/etc..."
|
||||
|
@ -263,6 +267,9 @@ def restore_saved_messages():
|
|||
old_message["sender_full_name"],
|
||||
old_message["sender_short_name"]))
|
||||
|
||||
if 'sending_client' in old_message:
|
||||
client_set.add(old_message['sending_client'])
|
||||
|
||||
if old_message['type'] == 'stream':
|
||||
stream_set.add((domain, old_message['recipient']))
|
||||
elif old_message['type'] == 'personal':
|
||||
|
@ -290,6 +297,13 @@ def restore_saved_messages():
|
|||
for realm in Realm.objects.all():
|
||||
realms[realm.domain] = realm
|
||||
|
||||
print datetime.datetime.now(), "Creating clients..."
|
||||
bulk_create_clients(client_set)
|
||||
|
||||
clients = {}
|
||||
for client in Client.objects.all():
|
||||
clients[client.name] = client
|
||||
|
||||
print datetime.datetime.now(), "Creating streams..."
|
||||
bulk_create_streams(realms, stream_set)
|
||||
|
||||
|
@ -350,6 +364,19 @@ def restore_saved_messages():
|
|||
type_hash = {"stream": Recipient.STREAM,
|
||||
"huddle": Recipient.HUDDLE,
|
||||
"personal": Recipient.PERSONAL}
|
||||
|
||||
if 'sending_client' in old_message:
|
||||
message.sending_client = clients[old_message['sending_client']]
|
||||
elif sender_email in ["othello@humbughq.com", "iago@humbughq.com", "prospero@humbughq.com",
|
||||
"cordelia@humbughq.com", "hamlet@humbughq.com"]:
|
||||
message.sending_client = clients['populate_db']
|
||||
elif realm.domain == "humbughq.com":
|
||||
message.sending_client = clients["website"]
|
||||
elif realm.domain == "mit.edu":
|
||||
message.sending_client = clients['zephyr_mirror']
|
||||
else:
|
||||
message.sending_client = clients['populate_db']
|
||||
|
||||
message.type = type_hash[old_message["type"]]
|
||||
message.content = old_message["content"]
|
||||
message.subject = old_message["subject"]
|
||||
|
@ -484,6 +511,7 @@ def send_messages(data):
|
|||
with transaction.commit_on_success():
|
||||
saved_data = ''
|
||||
message = Message()
|
||||
message.sending_client = get_client('populate_db')
|
||||
length = random.randint(1, 5)
|
||||
lines = (t.strip() for t in texts[offset: offset + length])
|
||||
message.content = '\n'.join(lines)
|
||||
|
|
|
@ -284,12 +284,32 @@ class Recipient(models.Model):
|
|||
display_recipient = get_display_recipient(self)
|
||||
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)
|
||||
|
||||
class Client(models.Model):
|
||||
name = models.CharField(max_length=30)
|
||||
|
||||
def get_client(name):
|
||||
(client, _) = Client.objects.get_or_create(name=name)
|
||||
return client
|
||||
|
||||
def bulk_create_clients(client_list):
|
||||
existing_clients = set()
|
||||
for client in Client.objects.select_related().all():
|
||||
existing_clients.add(client.name)
|
||||
|
||||
clients_to_create = []
|
||||
for name in client_list:
|
||||
if name not in existing_clients:
|
||||
clients_to_create.append(Client(name=name))
|
||||
existing_clients.add(name)
|
||||
batch_bulk_create(Client, clients_to_create)
|
||||
|
||||
class Message(models.Model):
|
||||
sender = models.ForeignKey(UserProfile)
|
||||
recipient = models.ForeignKey(Recipient)
|
||||
subject = models.CharField(max_length=60)
|
||||
content = models.TextField()
|
||||
pub_date = models.DateTimeField('date published')
|
||||
sending_client = models.ForeignKey(Client)
|
||||
|
||||
def __repr__(self):
|
||||
display_recipient = get_display_recipient(self.recipient)
|
||||
|
@ -321,6 +341,7 @@ class Message(models.Model):
|
|||
'sender_email' : self.sender.user.email,
|
||||
'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(),
|
||||
'recipient' : get_log_recipient(self.recipient),
|
||||
'subject' : self.subject,
|
||||
|
|
|
@ -3,4 +3,4 @@ mkdir -p zephyr/fixtures
|
|||
rm -f zephyr/tests/zephyrdb.test
|
||||
python manage.py syncdb --noinput --settings=humbug.test_settings
|
||||
python manage.py populate_db --settings=humbug.test_settings -n20
|
||||
python manage.py dumpdata --settings=humbug.test_settings auth.User zephyr.UserProfile zephyr.Stream zephyr.Recipient zephyr.Subscription zephyr.Message zephyr.Huddle zephyr.Realm zephyr.UserMessage > zephyr/fixtures/messages.json
|
||||
python manage.py dumpdata --settings=humbug.test_settings auth.User zephyr.UserProfile zephyr.Stream zephyr.Recipient zephyr.Subscription zephyr.Message zephyr.Huddle zephyr.Realm zephyr.UserMessage zephyr.Client > zephyr/fixtures/messages.json
|
||||
|
|
|
@ -14,7 +14,7 @@ from zephyr.models import Message, UserProfile, Stream, Subscription, \
|
|||
Recipient, get_display_recipient, get_huddle, Realm, UserMessage, \
|
||||
do_add_subscription, do_remove_subscription, \
|
||||
create_user, do_send_message, mit_sync_table, create_user_if_needed, \
|
||||
create_stream_if_needed, PreregistrationUser
|
||||
create_stream_if_needed, PreregistrationUser, get_client
|
||||
from zephyr.forms import RegistrationForm, HomepageForm, is_unique
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
|
@ -400,6 +400,8 @@ def send_message_backend(request, user_profile, sender):
|
|||
return json_error("Missing type")
|
||||
if "content" not in request.POST:
|
||||
return json_error("Missing message contents")
|
||||
if "client" not in request.POST:
|
||||
return json_error("Missing client")
|
||||
if "forged" in request.POST:
|
||||
if not is_super_user_api(request):
|
||||
return json_error("User not authorized for this query")
|
||||
|
@ -475,6 +477,7 @@ def send_message_backend(request, user_profile, sender):
|
|||
message.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc)
|
||||
else:
|
||||
message.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
|
||||
message.sending_client = get_client(request.POST['client'])
|
||||
|
||||
# To avoid message loops, we must pass whether the message was
|
||||
# synced from MIT message here.
|
||||
|
|
Loading…
Reference in New Issue