def add2workingset(username): W = getW(username) T = getT(username) tags = T.objects.all() w, created = W.objects.get_or_create(name="snippetbook") w.tags = tags # .values_list('id', flat=True) w.save() print "a workingset is saved:", w.name, " with tags: ", w.tags
def add2workingset(username): W = getW(username) T = getT(username) tags = T.objects.all() w, created = W.objects.get_or_create(name='snippetbook') w.tags = tags#.values_list('id', flat=True) w.save() print 'a workingset is saved:', w.name, ' with tags: ', w.tags
def add_bookmark(request): username = request.user.username N = getNote(username, 'bookmarkbook') T = getT(username) #W = getW(username) #w = W.objects.get(name='bookmarkbook') if request.method == 'POST': tags = T.objects.all() #the same as in add scrap code, here we don't use form because tags in from is required #AddNForm = create_model_form("AddNForm_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)}) n = N() post = request.POST.copy() url = post.get('url') if N.objects.filter(url=url).exists(): return render_to_response("include/notes/addNote_result.html",{'message':_('This bookmark aready exists! You can close this window, or it will be closed for you in 2 seconds.')}) tag_names = post.getlist('item[tags][]') tags = [] for tag_name in tag_names: t, created = T.objects.get_or_create(name=tag_name) #=============================================================================== # # if created or not w.tags.filter(name=t.name).exists(): # w.tags.add(t) #=============================================================================== #tags.append(t.id) tags.append(t.name) #=============================================================================== # if not tag_names: # tags = [T.objects.get(name='untagged').id] #=============================================================================== #=============================================================================== # post.setlist('tags', tags) # # f = AddNForm(post, instance=n) # log.debug("f.errors:"+str(f.errors)) # f.save() #=============================================================================== if not tags or (len(tags) == 1 and tags[0] == u''): tags = None n.title = post.get('title') n.desc = post.get('desc') n.url = post.get('url') private = post.get('private', False) if private in ['true', 'on']: n.private = True else: n.private = False #n.private = post.get('private', False) n.vote = post.get('vote') n.save() n.add_tags(tags, 'bookmarkbook') n.save() return render_to_response("include/notes/addNote_result.html",{'message':_('Bookmark is successfully added! You can close this window, or it will be closed for you in 1 second.')}) else: tags = __get_ws_tags(request, username, 'bookmarkbook') from django.forms import TextInput #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names) AddNForm = create_model_form("AddNForm_"+str(username), N, fields={},#{'tags':forms.ModelMultipleChoiceField(queryset=tags)}, options={'exclude':['deleted'], 'fields':['url','title','tags','desc','vote','private'], 'widgets':{'title': TextInput(attrs={'size': 60}), }}) url = request.GET.get('url') if N.objects.filter(url=url).exists(): b = N.objects.get(url=url) #addNoteForm = AddNForm(initial={'url': url, 'title':b.title, 'tags': b.tags.all()}) existing = True return render_to_response('bookmarkbook/notes/addNote.html', {'existing':existing, 'url':url, 'id':b.id, 'title':b.title}, context_instance=RequestContext(request)) title = request.GET.get('title') #default_tag_id = T.objects.get(name='untagged').id addNoteForm = AddNForm(initial={'url': url, 'title':title#, 'tags': [default_tag_id] }) return render_to_response('bookmarkbook/notes/addNote.html', {'addNoteForm': addNoteForm, 'url':url, 'tags':tags}) #TODO:how to display url using the form's initial
def import_with_tags2(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None): """This not only gets all the urls, but also turns the folders in the file into tags""" T = getT(username) W = getW(username) urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(bookmark_file).findAll('a')] count_urls_in_file = len(urls) #print count_urls_in_file, ' urls found in the bookmark file.' count_tag_created = 0 w = W.objects.get(name="bookmarkbook") count_note_created = 0 duplicate = [] #move the pointer back to the beginning of the file bookmark_file.seek(0) b = None folder_list = [] for line in bookmark_file: if line.find('<DT><H3') != -1: tname = [tag.string for tag in BeautifulSoup(line).findAll('h3')][0] folder_list.append({tname:[]}) #print 'one folder with folder name ',tname,' pushed to stack.' if line.find('</DL><P>') != -1 or line.find('</DL><p>') != -1:#FF and Chrome use <p> while Opera uses <P> #there is one extra '</DL><P>' at the end of the file for <H1>Bookmarks</H1>. So when it comes to the #it, just skip if len(folder_list) == 0: continue folder_of_urls = folder_list.pop() #print 'one folder ',folder_of_urls,' popped out of stack.' folder = folder_of_urls.keys()[0] urls = folder_of_urls.get(folder) folderstr = unicode(folder) if folderstr not in [u'Unsorted Bookmarks', u'[Folder Name]', u'Bookmarks Toolbar']: t, created = T.objects.get_or_create(name = folderstr) if created: count_tag_created += 1 w.tags.add(t) w.save() for url in urls: #print 'url in the popped out stack is: ', url url.tags.add(t) num_of_tags_created = url.add_tags(common_tag, 'bookmarkbook') count_tag_created = count_tag_created + num_of_tags_created url.save() if line.find('<DT><A') != -1: u = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(line).findAll('a')][0] b, created = build_one_bookmark(username, u, default_vote) if not created: duplicate.append((u[0], u[1])) else: count_note_created +=1 #for url that is at the top, simply create the bookmark without adding it to any tag if len(folder_list) == 0: pass else: for i in range(len(folder_list)):#add this url to every folder on the stack f_of_bs = folder_list[i] f = f_of_bs.keys()[0] bs = f_of_bs.get(f) bs.append(b) f_of_bs.update({f:bs}) folder_list[i] = f_of_bs #print 'one url ', b, 'is added to a folder on stack ', f if line.find('<DD>') != -1: if b: desc = line.strip('<DD>').strip('</DD>') b.desc = desc b.save() print 'b.desc:', b.desc #print count_note_created, ' bookmarks created' #print len(duplicate), ' duplicated bookmarks.' #print 'duplicate is:', duplicate duplicate.sort() return count_urls_in_file, count_note_created, duplicate, count_tag_created
def import_with_tags(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None): """This not only gets all the urls, but also turns the folders in the file into tags""" T = getT(username) W = getW(username) urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(bookmark_file).findAll('a')] count_urls_in_file = len(urls) print count_urls_in_file, ' urls found in the bookmark file.' bookmark_file.seek(0) folders = [tag.string for tag in BeautifulSoup(bookmark_file).findAll('h3')] print 'folders:', folders print len(folders), ' folders found in the bookmark file.' count_tag_created = 0 w = W.objects.get(name="bookmarks") #make each of them into a tag for folder in folders: print 'type(folder):', type(folder) print 'folder:', folder #some bug with BeautifulSoup's custom unicode. So upcast back to unicode itself. See http://code.djangoproject.com/ticket/11932 folderstr = unicode(folder) print 'type(folderstr):', type(folderstr) print 'folderstr:', folderstr if folderstr not in [u'Unsorted Bookmarks', u'[Folder Name]', u'Bookmarks Toolbar']: t, created = T.objects.get_or_create(name = folderstr) print 'tag ', t, ' created ', created print 't.name:', t.name if created: #print 'tag:', t, ' is created.' count_tag_created += 1 w.tags.add(t) w.save() print count_tag_created, 'tags are created.' count_note_created = 0 duplicate = [] #move the ponter back to the beginning of the file bookmark_file.seek(0) t = None for line in bookmark_file: if line.find('<DT><A') != -1: url = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(line).findAll('a')][0] n, created = build_one_bookmark(username, url, default_vote) if not created: duplicate.append((url[0], url[1])) else: count_note_created +=1 if t: n.tags.add(t) n.save() elif line.find('<DT><H3') != -1: tname = [tag.string for tag in BeautifulSoup(line).findAll('h3')][0] if unicode(tname) not in [u'Unsorted Bookmarks', u'[Folder Name]', u'Bookmarks Toolbar']: print 'unicode(tname) is:', unicode(tname) t = T.objects.get(name__exact=unicode(tname)) else: continue print count_note_created, ' bookmarks created' print len(duplicate), ' duplicated bookmarks.' #print 'duplicate is:', duplicate duplicate.sort() return count_urls_in_file, count_note_created, duplicate, count_tag_created
def add_scrap(request): username = request.user.username N = getNote(username, 'scrapbook') T = getT(username) #W = getW(username) #w = W.objects.get(name='scrapbook') if request.method == 'POST': tags = T.objects.all() #form require tags to be required. So don't use form now, and use the code from add_note in notebook.notes.views for adding a snippet #AddNForm = create_model_form("AddNForm_add_scrap_post_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)}) n = N() post = request.POST.copy() tag_names = post.getlist('item[tags][]') tags = [] for tag_name in tag_names: t, created = T.objects.get_or_create(name=tag_name) #==============Don't need below any more since add_tags will do this logic================================================================= # if created or not w.tags.filter(name=t.name).exists(): # w.tags.add(t) #=============================================================================== #tags.append(t.id) tags.append(t.name) #if not tag_names: # tags = [T.objects.get(name='untagged').id] if not tags or (len(tags) == 1 and tags[0] == u''): tags = None #f = AddNForm(post, instance=n) #log.debug("f.errors:"+str(f.errors)) #TODO:handle errors such as url broken #n = f.save(commit=False) n.title = post.get('title') n.desc = post.get('desc') n.url = post.get('url') private = post.get('private', False) if private in ['true', 'on']: n.private = True else: n.private = False n.vote = post.get('vote') n.save() n.add_tags(tags, 'scrapbook') n.save() #called this specifically to save the url to the social db as well return render_to_response("include/notes/addNote_result.html",\ {'message':_('Scrap is successfully added! You can close this window, or it will be closed for you in 1 second.')}) else: tags = __get_ws_tags(request, username, 'scrapbook') from django.forms import TextInput #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names) AddNForm_scrap = create_model_form("AddNForm_add_scrap_get_"+str(username), N, fields={#'tags':forms.ModelMultipleChoiceField(queryset=tags) }, options={'exclude':['deleted'], 'fields':['url','title','tags','desc','vote','private'], 'widgets':{'title': TextInput(attrs={'size': 80}), }}) url = request.GET.get('url') title = request.GET.get('title') desc = request.GET.get('desc') #default_tag_id = T.objects.get(name='untagged').id addNoteForm = AddNForm_scrap(initial={'url': url, 'title':title, 'desc':desc#, 'tags': [default_tag_id] }) #no need of the custimized form in the scrapbook template return render_to_response('scrapbook/notes/addNote.html', {'addNoteForm': addNoteForm, 'desc':desc, 'url':url, 'tags':tags})
def add_tags_2_ws(username, ws_name): T = getT(username) W = getW(username) w = W.objects.get(name=ws_name) w.tags = T.objects.all()
def add_bookmark(request): username = request.user.username N = getNote(username, 'bookmarkbook') T = getT(username) #W = getW(username) #w = W.objects.get(name='bookmarkbook') if request.method == 'POST': tags = T.objects.all() #the same as in add scrap code, here we don't use form because tags in from is required #AddNForm = create_model_form("AddNForm_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)}) n = N() post = request.POST.copy() url = post.get('url') if N.objects.filter(url=url).exists(): return render_to_response( "include/notes/addNote_result.html", { 'message': _('This bookmark aready exists! You can close this window, or it will be closed for you in 2 seconds.' ) }) tag_names = post.getlist('item[tags][]') tags = [] for tag_name in tag_names: t, created = T.objects.get_or_create(name=tag_name) #=============================================================================== # # if created or not w.tags.filter(name=t.name).exists(): # w.tags.add(t) #=============================================================================== #tags.append(t.id) tags.append(t.name) #=============================================================================== # if not tag_names: # tags = [T.objects.get(name='untagged').id] #=============================================================================== #=============================================================================== # post.setlist('tags', tags) # # f = AddNForm(post, instance=n) # log.debug("f.errors:"+str(f.errors)) # f.save() #=============================================================================== if not tags or (len(tags) == 1 and tags[0] == u''): tags = None n.title = post.get('title') n.desc = post.get('desc') n.url = post.get('url') private = post.get('private', False) if private in ['true', 'on']: n.private = True else: n.private = False #n.private = post.get('private', False) n.vote = post.get('vote') n.save() n.add_tags(tags, 'bookmarkbook') n.save() return render_to_response( "include/notes/addNote_result.html", { 'message': _('Bookmark is successfully added! You can close this window, or it will be closed for you in 1 second.' ) }) else: tags = __get_ws_tags(request, username, 'bookmarkbook') from django.forms import TextInput #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names) AddNForm = create_model_form( "AddNForm_" + str(username), N, fields={}, #{'tags':forms.ModelMultipleChoiceField(queryset=tags)}, options={ 'exclude': ['deleted'], 'fields': ['url', 'title', 'tags', 'desc', 'vote', 'private'], 'widgets': { 'title': TextInput(attrs={'size': 60}), } }) url = request.GET.get('url') if N.objects.filter(url=url).exists(): b = N.objects.get(url=url) #addNoteForm = AddNForm(initial={'url': url, 'title':b.title, 'tags': b.tags.all()}) existing = True return render_to_response('bookmarkbook/notes/addNote.html', { 'existing': existing, 'url': url, 'id': b.id, 'title': b.title }, context_instance=RequestContext(request)) title = request.GET.get('title') #default_tag_id = T.objects.get(name='untagged').id addNoteForm = AddNForm(initial={ 'url': url, 'title': title #, 'tags': [default_tag_id] }) return render_to_response('bookmarkbook/notes/addNote.html', { 'addNoteForm': addNoteForm, 'url': url, 'tags': tags }) #TODO:how to display url using the form's initial
def import_with_tags2(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None): """This not only gets all the urls, but also turns the folders in the file into tags""" T = getT(username) W = getW(username) urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(bookmark_file).findAll('a')] count_urls_in_file = len(urls) #print count_urls_in_file, ' urls found in the bookmark file.' count_tag_created = 0 w = W.objects.get(name="bookmarkbook") count_note_created = 0 duplicate = [] #move the pointer back to the beginning of the file bookmark_file.seek(0) b = None folder_list = [] for line in bookmark_file: if line.find('<DT><H3') != -1: tname = [tag.string for tag in BeautifulSoup(line).findAll('h3')][0] folder_list.append({tname: []}) #print 'one folder with folder name ',tname,' pushed to stack.' if line.find('</DL><P>') != -1 or line.find( '</DL><p>') != -1: #FF and Chrome use <p> while Opera uses <P> #there is one extra '</DL><P>' at the end of the file for <H1>Bookmarks</H1>. So when it comes to the #it, just skip if len(folder_list) == 0: continue folder_of_urls = folder_list.pop() #print 'one folder ',folder_of_urls,' popped out of stack.' folder = folder_of_urls.keys()[0] urls = folder_of_urls.get(folder) folderstr = unicode(folder) if folderstr not in [ u'Unsorted Bookmarks', u'[Folder Name]', u'Bookmarks Toolbar' ]: t, created = T.objects.get_or_create(name=folderstr) if created: count_tag_created += 1 w.tags.add(t) w.save() for url in urls: #print 'url in the popped out stack is: ', url url.tags.add(t) num_of_tags_created = url.add_tags(common_tag, 'bookmarkbook') count_tag_created = count_tag_created + num_of_tags_created url.save() if line.find('<DT><A') != -1: u = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(line).findAll('a')][0] b, created = build_one_bookmark(username, u, default_vote) if not created: duplicate.append((u[0], u[1])) else: count_note_created += 1 #for url that is at the top, simply create the bookmark without adding it to any tag if len(folder_list) == 0: pass else: for i in range(len(folder_list) ): #add this url to every folder on the stack f_of_bs = folder_list[i] f = f_of_bs.keys()[0] bs = f_of_bs.get(f) bs.append(b) f_of_bs.update({f: bs}) folder_list[i] = f_of_bs #print 'one url ', b, 'is added to a folder on stack ', f if line.find('<DD>') != -1: if b: desc = line.strip('<DD>').strip('</DD>') b.desc = desc b.save() print 'b.desc:', b.desc #print count_note_created, ' bookmarks created' #print len(duplicate), ' duplicated bookmarks.' #print 'duplicate is:', duplicate duplicate.sort() return count_urls_in_file, count_note_created, duplicate, count_tag_created
def import_with_tags(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None): """This not only gets all the urls, but also turns the folders in the file into tags""" T = getT(username) W = getW(username) urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(bookmark_file).findAll('a')] count_urls_in_file = len(urls) print count_urls_in_file, ' urls found in the bookmark file.' bookmark_file.seek(0) folders = [ tag.string for tag in BeautifulSoup(bookmark_file).findAll('h3') ] print 'folders:', folders print len(folders), ' folders found in the bookmark file.' count_tag_created = 0 w = W.objects.get(name="bookmarks") #make each of them into a tag for folder in folders: print 'type(folder):', type(folder) print 'folder:', folder #some bug with BeautifulSoup's custom unicode. So upcast back to unicode itself. See http://code.djangoproject.com/ticket/11932 folderstr = unicode(folder) print 'type(folderstr):', type(folderstr) print 'folderstr:', folderstr if folderstr not in [ u'Unsorted Bookmarks', u'[Folder Name]', u'Bookmarks Toolbar' ]: t, created = T.objects.get_or_create(name=folderstr) print 'tag ', t, ' created ', created print 't.name:', t.name if created: #print 'tag:', t, ' is created.' count_tag_created += 1 w.tags.add(t) w.save() print count_tag_created, 'tags are created.' count_note_created = 0 duplicate = [] #move the ponter back to the beginning of the file bookmark_file.seek(0) t = None for line in bookmark_file: if line.find('<DT><A') != -1: url = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in BeautifulSoup(line).findAll('a')][0] n, created = build_one_bookmark(username, url, default_vote) if not created: duplicate.append((url[0], url[1])) else: count_note_created += 1 if t: n.tags.add(t) n.save() elif line.find('<DT><H3') != -1: tname = [tag.string for tag in BeautifulSoup(line).findAll('h3')][0] if unicode(tname) not in [ u'Unsorted Bookmarks', u'[Folder Name]', u'Bookmarks Toolbar' ]: print 'unicode(tname) is:', unicode(tname) t = T.objects.get(name__exact=unicode(tname)) else: continue print count_note_created, ' bookmarks created' print len(duplicate), ' duplicated bookmarks.' #print 'duplicate is:', duplicate duplicate.sort() return count_urls_in_file, count_note_created, duplicate, count_tag_created
def add_scrap(request): username = request.user.username N = getNote(username, 'scrapbook') T = getT(username) #W = getW(username) #w = W.objects.get(name='scrapbook') if request.method == 'POST': tags = T.objects.all() #form require tags to be required. So don't use form now, and use the code from add_note in notebook.notes.views for adding a snippet #AddNForm = create_model_form("AddNForm_add_scrap_post_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)}) n = N() post = request.POST.copy() tag_names = post.getlist('item[tags][]') tags = [] for tag_name in tag_names: t, created = T.objects.get_or_create(name=tag_name) #==============Don't need below any more since add_tags will do this logic================================================================= # if created or not w.tags.filter(name=t.name).exists(): # w.tags.add(t) #=============================================================================== #tags.append(t.id) tags.append(t.name) #if not tag_names: # tags = [T.objects.get(name='untagged').id] if not tags or (len(tags) == 1 and tags[0] == u''): tags = None #f = AddNForm(post, instance=n) #log.debug("f.errors:"+str(f.errors)) #TODO:handle errors such as url broken #n = f.save(commit=False) n.title = post.get('title') n.desc = post.get('desc') n.url = post.get('url') private = post.get('private', False) if private in ['true', 'on']: n.private = True else: n.private = False n.vote = post.get('vote') n.save() n.add_tags(tags, 'scrapbook') n.save( ) #called this specifically to save the url to the social db as well return render_to_response("include/notes/addNote_result.html",\ {'message':_('Scrap is successfully added! You can close this window, or it will be closed for you in 1 second.')}) else: tags = __get_ws_tags(request, username, 'scrapbook') from django.forms import TextInput #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names) AddNForm_scrap = create_model_form( "AddNForm_add_scrap_get_" + str(username), N, fields={ #'tags':forms.ModelMultipleChoiceField(queryset=tags) }, options={ 'exclude': ['deleted'], 'fields': ['url', 'title', 'tags', 'desc', 'vote', 'private'], 'widgets': { 'title': TextInput(attrs={'size': 80}), } }) url = request.GET.get('url') title = request.GET.get('title') desc = request.GET.get('desc') #default_tag_id = T.objects.get(name='untagged').id addNoteForm = AddNForm_scrap(initial={ 'url': url, 'title': title, 'desc': desc #, 'tags': [default_tag_id] }) #no need of the custimized form in the scrapbook template return render_to_response('scrapbook/notes/addNote.html', { 'addNoteForm': addNoteForm, 'desc': desc, 'url': url, 'tags': tags })