0

私は電子メール/ SMS通知エンジンを構築しています。人またはグループはオブジェクトに登録することができ、オブジェクトが更新されると、その人/グループに電子メール/ SMSによって変更が通知されます。Django:通知エンジンを作成する

現在、私は以下のようにそれを実装しました:

models.py

class Subscription(models.Model): 
    # subscribers 
    people = models.ManyToManyField(Person) 
    groups = models.ManyToManyField(Group) 

    # mandatory fields for generic relation 
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) 
    object_id = models.PositiveIntegerField() 
    content_object = GenericForeignKey() 

mixins.py

class NotificationMixin(object): 

    def perform_update(self, serializer): 
     model_name = str.lower(serializer.Meta.model) 
     old_obj = model.objects.get(id=serializer.data['id']) 
     obj = serializer.save() 
     self.notify(model_name, old_obj, obj) 

    def notify(self, model_name, old_obj, obj): 
     # All models have a GenericRelation field for reverse searching 
     subscriptions = Subscription.objects.filter(**{ model_name: obj }) 

     // *rest of logic to iterate over subscriptions and email people/groups 

DjangoのContentTypeを一般的な関係を使用して、私は人を購読することができます/グループを任意のオブジェクトに追加します。

同じサブスクリプションモデルを使用してグローバルサブスクリプションを作成する機能を追加して、すべてが同じテーブルに格納されるようにしたいとします。グローバルサブスクリプションには追跡対象のオブジェクトはありませんが、特定のモデルのオブジェクトがトリガーされると、電子メールが送信されます。

サブスクリプションモデルを一般化してモデルインスタンスを受け入れるか、レスポンスをトリガーするモデルが問題になります。

機能は、私がしたい:

  1. グローバルサブスクリプションモデルXの任意のオブジェクトが

  2. オブジェクト・レベルのサブスクリプション

    を変更された場合によって更新

    • 人々/グループ
      • 特定のオブジェクトが

を更新している場合、更新者/グループが、私はこの問題について移動する良い方法を持っている現在のモデル/アーキテクチャですか、私は異なり、これをアプローチする必要がありますか?

ノートフロントエンドはAngularJsであるため、これは私たちのdjango apiとの排他的な相互作用です。これに対する解決策を見つけたいことがあり誰のために

答えて

0

、私がやってしまった:

class Subscription(models.Model): 
    """ 
    Model for subscribing to object changes. 

    Can be subscribed to any object or any model type. 
    Subcriptions: 
    model - if any object changes of this type that belongs to the company, update 
    object - if that specific object changes, update 

    To create: 
    Either give a content_object or a content_type. Content object is a model instance. Content type is a ContentType (i.e. Study, Product, etc.) 

    If it is created wrong, will just be lost in database forever (unless you know it's there, then delete it.) 
    """ 
    people = models.ManyToManyField(Person) 
    groups = models.ManyToManyField(Group) 
    trigger = models.CharField(max_length=50) 

    APP_LABELS = [apps to limit the available choices] 

    # for object subscription 
    # mandatory fields for generic relation 
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) 
    object_id = models.PositiveIntegerField(null=True) 
    content_object = GenericForeignKey('content_type', 'object_id') 

    def save(self, *args, **kwargs): 
     ''' 
     Save logic to validate the Subscription model. This is run after the native create() method. 
     ''' 
     # check if no content_object, then content_type must be defined by user 
     # probably not necessary since it will fail at creation if content_type isn't an instance of a ContentType, but good to double check 
     if not self.content_object: 
      if self.content_type.__class__ != ContentType: 
       if type(self.content_type) == str: 
        # if content_type is a string designating a model 
        self.content_type = ContentType.objects.get(model=self.content_type) 
       else: 
        # if content_type is a model class 
        self.content_type = ContentType.objects.get_for_model(Study) 

     apps = ', '.join(map(str, self.APP_LABELS)) 

     # check if content_type in our defined apps 
     if self.content_type.app_label not in apps: 
      raise ValidationError('Please select a content_object or content_type in apps: {0}'.format(apps)) 

     super(Subscription, self).save(*args, **kwargs) 
関連する問題