Outgoing Webhook System: Add Service model.

This commit is contained in:
rahuldeve 2016-07-15 22:27:37 +05:30 committed by Tim Abbott
parent c388baa431
commit 413c5f93bb
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-04-27 16:55
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('zerver', '0077_add_file_name_field_to_realm_emoji'),
]
operations = [
migrations.CreateModel(
name='Service',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('base_url', models.TextField()),
('token', models.TextField()),
('interface', models.PositiveSmallIntegerField(default=1)),
('user_profile', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -517,6 +517,7 @@ class UserProfile(ModelReprMixin, AbstractBaseUser, PermissionsMixin):
since they can't be used to read messages.
"""
INCOMING_WEBHOOK_BOT = 2
OUTGOING_WEBHOOK_BOT = 3
# Fields from models.AbstractUser minus last_name and first_name,
# which we don't use; email is modified to make it indexed and unique.
@ -697,6 +698,11 @@ class UserProfile(ModelReprMixin, AbstractBaseUser, PermissionsMixin):
# type: () -> bool
return self.bot_type == UserProfile.INCOMING_WEBHOOK_BOT
@property
def is_outgoing_webhook_bot(self):
# type: () -> bool
return self.bot_type == UserProfile.OUTGOING_WEBHOOK_BOT
@staticmethod
def emojiset_choices():
# type: () -> Dict[Text, Text]
@ -1697,3 +1703,38 @@ class CustomProfileFieldValue(models.Model):
class Meta(object):
unique_together = ('user_profile', 'field')
class Service(models.Model):
name = models.CharField(max_length=UserProfile.MAX_NAME_LENGTH) # type: Text
# owner of service/bot user corresponding to the service
user_profile = models.ForeignKey(UserProfile) # type: UserProfile
# address of the third party site
base_url = models.TextField() # type: Text
# used for authentication to third party site
token = models.TextField() # type: Text
# the interface used to send data to third party site
interface = models.PositiveSmallIntegerField(default=1) # type: int
# Valid interfaces are {}
# N.B. If we used Django's choice=... we would get this for free (kinda)
_interfaces = {} # type: Dict[int, Text]
def interface_name(self):
# type: () -> Text
# Raises KeyError if invalid
return self._interfaces[self.interface]
def get_realm_outgoing_webhook_services_name(realm):
# type: (Realm) -> List[Any]
return list(Service.objects.filter(user_profile__realm=realm, user_profile__is_bot=True,
user_profile__bot_type=UserProfile.OUTGOING_WEBHOOK_BOT).values('name'))
def get_realm_bot_services(email, realm):
# type: (str, Realm) -> List[Any]
return list(Service.objects.filter(user_profile__email=email, user_profile__realm=realm).values())
def get_service_profile(email, realm, service_name):
# type: (str, Realm, str) -> Service
return Service.objects.get(user_profile__email=email, user_profile__realm=realm, name=service_name)