def _update_admin_subscription(self,
                                   subscription,
                                   pe_id,
                                   alert_editor_id,
                                   user_id=None,
                                   group_id=None,
                                   filter_id=None,
                                   subscription_id=None,
                                   ):

        db = current.db
        s3db = current.s3db
        stable = s3db.pr_subscription
        # Save filters
        # Filter Public and Restricted alert
        # Private alert are not sent through subscription
        filters = subscription.get("filters")

        if user_id is not None:
            comment_filter = {"auth_user.id": str(user_id)}
        elif group_id is not None:
            comment_filter = {"pr_group.id": str(group_id)}
        else:
            comment_filter = None

        ftable = s3db.pr_filter

        filters = subscription.get("filters")
        filters = json.loads(filters)
        scope_filter = ["scope__belongs", "Public,Restricted"]
        filters.append(scope_filter)
        filters = json.dumps(filters)
        filters_ = json.loads(filters)
        filters_ = [filter_ for filter_ in filters_
                    if filter_[0] != "id__belongs"]
        filters_ = json.dumps(filters_)
        if filter_id is None:
            success = ftable.insert(pe_id=pe_id, query=filters_)
            filter_id = success
        else:
            success = db(ftable.id == filter_id).update(query=filters_)
        if not success:
            return None
        # Save subscription settings
        frequency = subscription["frequency"]
        if subscription_id is None:
            if comment_filter is not None:
                comment_filter = json.dumps(comment_filter)
                success = stable.insert(pe_id=pe_id,
                                        filter_id=filter_id,
                                        notify_on=subscription["notify_on"],
                                        frequency=frequency,
                                        method=subscription["method"],
                                        comments=comment_filter,
                                        owned_by_group=alert_editor_id,
                                        )
            else:
                success = stable.insert(pe_id=pe_id,
                                        filter_id=filter_id,
                                        notify_on=subscription["notify_on"],
                                        frequency=frequency,
                                        method=subscription["method"],
                                        owned_by_group=alert_editor_id,
                                        )
            subscription_id = success
        else:
            if comment_filter is not None:
                comment_filter = json.dumps(comment_filter)
                success = db(stable.id == subscription_id).update(pe_id=pe_id,
                                            filter_id=filter_id,
                                            notify_on=subscription["notify_on"],
                                            frequency=frequency,
                                            method=subscription["method"],
                                            comments=comment_filter,
                                            owned_by_group=alert_editor_id,
                                            )
            else:
                success = db(stable.id == subscription_id).update(pe_id=pe_id,
                                            filter_id=filter_id,
                                            notify_on=subscription["notify_on"],
                                            frequency=frequency,
                                            method=subscription["method"],
                                            owned_by_group=alert_editor_id,
                                            )
        if not success:
            return None

        # Save subscription
        rtable = s3db.pr_subscription_resource
        subscribe = subscription.get("subscribe")
        if subscribe:
            subscribed = {}
            timestamps = {}
            intervals = s3db.pr_subscription_check_intervals
            interval = timedelta(minutes=intervals.get(frequency, 0))
            now = datetime.utcnow()
            row = db(rtable.subscription_id == subscription_id).select(\
                                                        limitby=(0,1)).first()
            if row:
                subscribed[(row.resource, row.url)] = row.id
                timestamps[row.id] = (row.last_check_time,
                                      row.next_check_time)

            fk = '''{"subscription_id": %s}''' % subscription_id
            for new in subscribe:
                resource, url = new["resource"], new["url"]
                if (resource, url) not in subscribed:
                    # Restore subscription if previously unsubscribed, else
                    # insert new record
                    unsubscribed = {"deleted": True,
                                    "deleted_fk": fk,
                                    "resource": resource,
                                    "url": url,
                                    }

                    rtable.update_or_insert(_key=unsubscribed,
                                            deleted=False,
                                            deleted_fk=None,
                                            subscription_id=subscription_id,
                                            resource=resource,
                                            url=url,
                                            last_check_time=now,
                                            next_check_time=None)
                else:
                    # Keep it
                    record_id = subscribed[(resource, url)]
                    last_check_time, next_check_time = timestamps[record_id]
                    data = {}
                    if not last_check_time:
                        # Someone has tampered with the timestamps, so
                        # we need to reset them and start over
                        last_check_time = now
                        data["last_check_time"] = last_check_time
                    due = last_check_time + interval
                    if next_check_time != due:
                        # Time interval has changed
                        data["next_check_time"] = due
                    if data:
                        db(rtable.id == record_id).update(**data)

        # Update subscription
        subscription["id"] = subscription_id
        subscription["filter_id"] = filter_id
        return subscription

    # -------------------------------------------------------------------------
Update Admin subscription
dataset/ETHPy150Open sahana/eden/modules/templates/SAMBRO/controllers.py/subscriptions._update_admin_subscription