From 413c5f93bb7bfa94deb491c4d04a9611b9118c6c Mon Sep 17 00:00:00 2001 From: rahuldeve Date: Fri, 15 Jul 2016 22:27:37 +0530 Subject: [PATCH] Outgoing Webhook System: Add Service model. --- zerver/migrations/0078_service.py | 28 +++++++++++++++++++++ zerver/models.py | 41 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 zerver/migrations/0078_service.py diff --git a/zerver/migrations/0078_service.py b/zerver/migrations/0078_service.py new file mode 100644 index 0000000000..8140eea32b --- /dev/null +++ b/zerver/migrations/0078_service.py @@ -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)), + ], + ), + ] diff --git a/zerver/models.py b/zerver/models.py index 498ba1f8bb..3b4d0862bc 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -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)