def get_notification_forms(request): user = request.user if request.method == 'POST': for i in range(1000): prefix = NOTIFICATION_PREFIX_TEMPLATE.format(i) if prefix + '-scope' in request.POST: yield NotificationForm(request.user, i > 0, {}, i == 0, prefix=prefix, data=request.POST) else: subscriptions = defaultdict(dict) initials = {} # Ensure default and admin scopes are visible for needed in (SCOPE_DEFAULT, SCOPE_ADMIN): key = (needed, None, None) subscriptions[key] = {} initials[key] = { 'scope': needed, 'project': None, 'component': None } active = (SCOPE_DEFAULT, None, None) # Include additional scopes from request if 'notify_project' in request.GET: try: project = user.allowed_projects.get( pk=request.GET['notify_project']) active = key = (SCOPE_PROJECT, project.pk, None) subscriptions[key] = {} initials[key] = { 'scope': SCOPE_PROJECT, 'project': project, 'component': None, } except (ObjectDoesNotExist, ValueError): pass if 'notify_component' in request.GET: try: component = Component.objects.get( project__in=user.allowed_projects, pk=request.GET['notify_component'], ) active = key = (SCOPE_COMPONENT, None, component.pk) subscriptions[key] = {} initials[key] = { 'scope': SCOPE_COMPONENT, 'project': None, 'component': component, } except (ObjectDoesNotExist, ValueError): pass # Popupate scopes from the database for subscription in user.subscription_set.iterator(): key = ( subscription.scope, subscription.project_id, subscription.component_id, ) subscriptions[key][ subscription.notification] = subscription.frequency initials[key] = { 'scope': subscription.scope, 'project': subscription.project, 'component': subscription.component, } # Generate forms for i, details in enumerate(sorted(subscriptions.items())): yield NotificationForm( user, i > 0, details[1], details[0] == active, initial=initials[details[0]], prefix=NOTIFICATION_PREFIX_TEMPLATE.format(i), )
def get_notification_forms(request): user = request.user if request.method == "POST": for i in range(200): prefix = NOTIFICATION_PREFIX_TEMPLATE.format(i) if prefix + "-scope" in request.POST: yield NotificationForm(request.user, i > 1, {}, i == 0, prefix=prefix, data=request.POST) else: subscriptions = defaultdict(dict) initials = {} # Ensure watched, admin and all scopes are visible for needed in (SCOPE_WATCHED, SCOPE_ADMIN, SCOPE_ALL): key = (needed, -1, -1) subscriptions[key] = {} initials[key] = { "scope": needed, "project": None, "component": None } active = (SCOPE_WATCHED, -1, -1) # Include additional scopes from request if "notify_project" in request.GET: try: project = user.allowed_projects.get( pk=request.GET["notify_project"]) active = key = (SCOPE_PROJECT, project.pk, -1) subscriptions[key] = {} initials[key] = { "scope": SCOPE_PROJECT, "project": project, "component": None, } except (ObjectDoesNotExist, ValueError): pass if "notify_component" in request.GET: try: component = Component.objects.filter_access(user).get( pk=request.GET["notify_component"], ) active = key = (SCOPE_COMPONENT, -1, component.pk) subscriptions[key] = {} initials[key] = { "scope": SCOPE_COMPONENT, "component": component, } except (ObjectDoesNotExist, ValueError): pass # Populate scopes from the database for subscription in user.subscription_set.iterator(): key = ( subscription.scope, subscription.project_id or -1, subscription.component_id or -1, ) subscriptions[key][ subscription.notification] = subscription.frequency initials[key] = { "scope": subscription.scope, "project": subscription.project, "component": subscription.component, } # Generate forms for i, details in enumerate(sorted(subscriptions.items())): yield NotificationForm( user, i > 1, details[1], details[0] == active, initial=initials[details[0]], prefix=NOTIFICATION_PREFIX_TEMPLATE.format(i), )