'Handler to save choosen tags with connected XBlock'

@XBlock.handler
def save_tags(self, request=None, suffix=None):

    try:
        posted_data = request.json
    except ValueError:
        return Response('Invalid    request    body', status=400)
    saved_tags = {}
    need_update = False
    for av_tag in self.get_available_tags():
        if ((av_tag.name in posted_data) and posted_data[av_tag.name]):
            tag_available_values = av_tag.get_values()
            tag_current_values = self.saved_tags.get(av_tag.name, [])
            if isinstance(tag_current_values, basestring):
                tag_current_values = [tag_current_values]
            for posted_tag_value in posted_data[av_tag.name]:
                if ((posted_tag_value not in tag_available_values) and (posted_tag_value not in tag_current_values)):
                    return Response(('Invalid    tag    value    was    passed:    %s' % posted_tag_value), status=400)
            saved_tags[av_tag.name] = posted_data[av_tag.name]
            need_update = True
        if (av_tag.name in posted_data):
            need_update = True
    if need_update:
        self.saved_tags = saved_tags
        return Response()
    else:
        return Response('Tags    parameters    were    not    passed', status=400)
