def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) for group in new_site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in new_site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if 'groups' in new_site: # Add new groups for group_name in new_site.get('groups', []): if group_name not in site['groups']: groups.update({'name':group_name},{'$addToSet': {'sites': site['url']}}) # Remove old groups for group_name in site['groups']: if group_name not in new_site.get('groups', []): groups.update({'name':group_name},{'$pull': {'sites': site['url']}}) if 'plans' in new_site: # Update the site. At this point we can only update plans. sites.update({'id': site_id}, {'$set': {'plans': new_site.get('plans')}}) # Return the updated site site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) return jsonify(success=True, site=sanitize_site(site))
def create_site(): site = request.json # Verify incoming site: url must be valid, groups must exist, plans must exist if not _check_site_url(site.get('url')): return jsonify(success=False, reason='invalid-url') if not _check_required_fields(site, ['url']): return jsonify(success=False, reason='missing-required-field') for group in site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if sites.find_one({'url': site['url']}) is not None: return jsonify(success=False, reason='site-already-exists') # Create the site new_site = { 'id': str(uuid.uuid4()), 'url': site['url'], 'plans': site.get('plans', []), 'created': datetime.datetime.utcnow() } sites.insert(new_site) # Add the site to the groups - group membership is stored in the group object, not in the site for group_name in site.get('groups', []): # No need to check if the site is already in the group as we just added the site groups.update({'name':group_name},{'$addToSet': {'sites': site['url']}}) new_site['groups'] = site.get('groups', []) # Return the new site return jsonify(success=True, site=sanitize_site(new_site))
def create_site(): site = request.json # Verify incoming site: url must be valid, groups must exist, plans must exist if not _check_site_url(site.get("url")): return jsonify(success=False, reason="invalid-url") if not _check_required_fields(site, ["url"]): return jsonify(success=False, reason="missing-required-field") for group in site.get("groups", []): if not _check_group_exists(group): return jsonify(success=False, reason="unknown-group") for plan_name in site.get("plans", []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason="unknown-plan") if sites.find_one({"url": site["url"]}) is not None: return jsonify(success=False, reason="site-already-exists") # Create the site new_site = { "id": str(uuid.uuid4()), "url": site["url"], "plans": site.get("plans", []), "created": datetime.datetime.utcnow(), } sites.insert(new_site) # Add the site to the groups - group membership is stored in the group object, not in the site for group_name in site.get("groups", []): # No need to check if the site is already in the group as we just added the site groups.update({"name": group_name}, {"$addToSet": {"sites": site["url"]}}) new_site["groups"] = site.get("groups", []) # Return the new site return jsonify(success=True, site=sanitize_site(new_site))
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({"id": site_id}) if not site: return jsonify(success=False, reason="no-such-site") site["groups"] = _find_groups_for_site(site["url"]) for group in new_site.get("groups", []): if not _check_group_exists(group): return jsonify(success=False, reason="unknown-group") for plan_name in new_site.get("plans", []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason="unknown-plan") if "groups" in new_site: # Add new groups for group_name in new_site.get("groups", []): if group_name not in site["groups"]: groups.update({"name": group_name}, {"$addToSet": {"sites": site["url"]}}) # Remove old groups for group_name in site["groups"]: if group_name not in new_site.get("groups", []): groups.update({"name": group_name}, {"$pull": {"sites": site["url"]}}) if "plans" in new_site: # Update the site. At this point we can only update plans. sites.update({"id": site_id}, {"$set": {"plans": new_site.get("plans")}}) # Return the updated site site = sites.find_one({"id": site_id}) if not site: return jsonify(success=False, reason="no-such-site") site["groups"] = _find_groups_for_site(site["url"]) return jsonify(success=True, site=sanitize_site(site))
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) for group in new_site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in new_site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if 'groups' in new_site: # Add new groups for group_name in new_site.get('groups', []): if group_name not in site['groups']: groups.update({'name': group_name}, {'$addToSet': { 'sites': site['url'] }}) # Remove old groups for group_name in site['groups']: if group_name not in new_site.get('groups', []): groups.update({'name': group_name}, {'$pull': { 'sites': site['url'] }}) if 'plans' in new_site: # Update the site. At this point we can only update plans. sites.update({'id': site_id}, {'$set': { 'plans': new_site.get('plans') }}) new_verification = new_site['verification'] old_verification = site.get('verification') # if site doesn't have 'verification', do us a favor, update the document as it is outdated! if not old_verification or old_verification['enabled'] != new_verification[ 'enabled']: # to make logic simpler, even if the new request wants to # disable verification, generate a new value anyway. sites.update({'id': site_id}, { '$set': { 'verification': { 'enabled': new_verification['enabled'], 'value': str(uuid.uuid4()) } } }) # Return the updated site site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) return jsonify(success=True, site=sanitize_site(site))
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) for group in new_site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in new_site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if 'groups' in new_site: # Add new groups for group_name in new_site.get('groups', []): if group_name not in site['groups']: groups.update({'name':group_name},{'$addToSet': {'sites': site['url']}}) # Remove old groups for group_name in site['groups']: if group_name not in new_site.get('groups', []): groups.update({'name':group_name},{'$pull': {'sites': site['url']}}) if 'plans' in new_site: # Update the site. At this point we can only update plans. sites.update({'id': site_id}, {'$set': {'plans': new_site.get('plans')}}) new_verification = new_site['verification'] old_verification = site.get('verification') # if site doesn't have 'verification', do us a favor, update the document as it is outdated! if not old_verification or old_verification['enabled'] != new_verification['enabled']: # to make logic simpler, even if the new request wants to # disable verification, generate a new value anyway. sites.update({'id': site_id}, {'$set': { 'verification': { 'enabled': new_verification['enabled'], 'value': str(uuid.uuid4())}}}) # Return the updated site site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) return jsonify(success=True, site=sanitize_site(site))
def create_site(): site = request.json # Verify incoming site: url must be valid, groups must exist, plans must exist if not _check_site_url(site.get('url')): return jsonify(success=False, reason='invalid-url') if not _check_required_fields(site, ['url']): return jsonify(success=False, reason='missing-required-field') for group in site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if sites.find_one({'url': site['url']}) is not None: return jsonify(success=False, reason='site-already-exists') # Create the site new_site = { 'id': str(uuid.uuid4()), 'url': site['url'], 'plans': site.get('plans', []), 'created': datetime.datetime.utcnow() } if site.get('verification', {}).get('enabled', False): new_site['verification'] = { 'enabled': True, 'value': str(uuid.uuid4()) } else: new_site['verification'] = {'enabled': False, 'value': None} sites.insert(new_site) # Add the site to the groups - group membership is stored in the group object, not in the site for group_name in site.get('groups', []): # No need to check if the site is already in the group as we just added the site groups.update({'name': group_name}, {'$addToSet': { 'sites': site['url'] }}) new_site['groups'] = site.get('groups', []) # Return the new site return jsonify(success=True, site=sanitize_site(new_site))