<s> from urllib . parse import urlsplit <EOL> from django . contrib import messages <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import IntegrityError , transaction <EOL> from django . shortcuts import redirect <EOL> from django . utils . translation import ugettext as _ <EOL> from connect . accounts . models import UserLink , UserSkill , LinkBrand <EOL> def save_paired_items ( request , user , formset , Model , <EOL> item_name , counterpart_name ) : <EOL> """<STR_LIT>""" <EOL> paired_items = [ ] <EOL> for form in formset : <EOL> if form . is_valid ( ) : <EOL> item = form . cleaned_data . get ( item_name , None ) <EOL> counterpart = form . cleaned_data . get ( counterpart_name , None ) <EOL> if item and counterpart : <EOL> model_instance = Model ( user = user ) <EOL> setattr ( model_instance , item_name , item ) <EOL> setattr ( model_instance , counterpart_name , counterpart ) <EOL> paired_items . append ( model_instance ) <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> Model . objects . filter ( user = user ) . delete ( ) <EOL> Model . objects . bulk_create ( paired_items ) <EOL> except IntegrityError : <EOL> messages . error ( request , _ ( '<STR_LIT>' ) ) <EOL> return redirect ( reverse ( '<STR_LIT>' ) ) <EOL> def save_skills ( request , user , formset ) : <EOL> """<STR_LIT>""" <EOL> save_paired_items ( request , user , formset , UserSkill , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def save_links ( request , user , formset ) : <EOL> """<STR_LIT>""" <EOL> save_paired_items ( request , user , formset , UserLink , '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> def match_link_to_brand ( user_links ) : <EOL> """<STR_LIT>""" <EOL> for link in user_links : <EOL> domain = urlsplit ( link . url ) . netloc <EOL> try : <EOL> brand = LinkBrand . objects . get ( domain = domain ) <EOL> link . icon = brand <EOL> link . save ( ) <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> return user_links </s>
