def edit(id, user_id, title=None, description=None, tags=None): edit_bookmark = Bookmark.query.get(id) if title is not None: edit_bookmark.title = title[:1024] if description is not None: edit_bookmark.description = description[:256] if tags != "" or tags is not None: if type(tags) is unicode: ls1 = edit_bookmark.tags ls2 = tags.split(',') # Compute deltas between new and current tags added_tags = set(ls1 + ls2) - set(ls1) removed_tags = set(ls1 + ls2) - set(ls2) for tag in added_tags: get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: new_tag = Tag(text=tag, user=user_id) new_tag.count = 1 db.session.add(new_tag) else: get_tag.count += 1 for tag in removed_tags: get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: pass else: get_tag.count -= 1 edit_bookmark.tags = ls2 db.session.commit()
def api_edit(id, tags, user_id): edit_bookmark = Bookmark.query.get(id) if tags != ['']: ls1 = edit_bookmark.tags ls2 = tags added_tags = set(ls1 + ls2) - set(ls1) removed_tags = set(ls1 + ls2) - set(ls2) for tag in added_tags: get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: new_tag = Tag(text=tag, user=user_id) new_tag.count = 1 db.session.add(new_tag) else: get_tag.count += 1 for tag in removed_tags: get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: pass else: get_tag.count -= 1 edit_bookmark.tags = ls2 db.session.commit()
def process(self): tags_query = Tag.query.all() tags_dictionary = {} for individ_tag in tags_query: tags_dictionary[individ_tag.text] = individ_tag created_tags = {} bookmarks = self.data['bookmarks'] for bookmark in bookmarks: title = bookmark['article__title'] url = bookmark['article__url'] dt = arrow.get(bookmark['date_added']).naive tags = ['Imported'] if bookmark['article__excerpt'] is not None: description = self.html_parser.unescape(bookmark['article__excerpt']) else: description = None self.urls[url] = { 'title': title, 'added_on': dt, 'tags': tags, 'description': description } if 'Imported' in tags_dictionary: self.tags_dict['Imported'] = {'obj': tags_dictionary['Imported']} else: new_tag = Tag() new_tag.text = 'Imported' db.session.add(new_tag) db.session.commit() self.tags_dict[new_tag.text] = {'obj': new_tag}
def new(url, user_id, description=None, tags=None, title=None, added=None): new_bookmark = Bookmark() new_bookmark.main_url = url[:2000] if title is not None: new_bookmark.title = title[:1024] if description is not None: new_bookmark.description = description[:256] new_bookmark.user = user_id if added is None: new_bookmark.added_on = datetime.utcnow() else: try: datetime.datetime.utcfromtimestamp(added) new_bookmark.added_on = added # UNIX timestamp in seconds since epoch, only except ValueError: new_bookmark.added_on = datetime.utcnow() new_bookmark.deleted = False if tags is not None: tags = tags.split(',') new_bookmark.tags = tags for tag in tags: # If tag is present, increment counter by one, or create if not present get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: new_tag = Tag(text=tag, user=user_id) new_tag.count = 1 db.session.add(new_tag) else: get_tag.count += 1 db.session.add(new_bookmark) db.session.commit() # Send off for archiving archive.do_archives(new_bookmark) return new_bookmark
def api_edit(id, tags, user_id): edit_bookmark = Bookmark.query.get(id) ls1 = edit_bookmark.tags or [] ls2 = tags added_tags = None removed_tags = None if tags != ['']: if ls1: added_tags = set(ls1 + ls2) - set(ls1) removed_tags = set(ls1 + ls2) - set(ls2) else: added_tags = set(ls2) if added_tags: for tag in added_tags: get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: new_tag = Tag(text=tag, user=user_id) new_tag.count = 1 db.session.add(new_tag) else: get_tag.count += 1 edit_bookmark.tags = ls2 else: removed_tags = set(edit_bookmark.tags) edit_bookmark.tags = [] if removed_tags: for tag in removed_tags: get_tag = Tag.query.filter_by(text=tag, user=user_id).first() if not get_tag: pass else: get_tag.count -= 1 db.session.commit()
def process(self): tags_query = Tag.query.all() # Get all the tags tags_dictionary = {} # Storing objects for existing tags in here. for individ_tag in tags_query: tags_dictionary[individ_tag.text] = individ_tag # Store all existing tags in dict created_tags = {} # Store all tags that had to be created for link in self.data: url = link['href'] if not self.valid_url.match(url): # If the url is not valid, burn it pass else: try: title = link['description'] except KeyError: title = link['href'] try: tags = link['tags'].split(' ') except KeyError: tags = [] tags = filter(None, tags) # Remove empty strings from tag list tags.append('Imported') # This is used as a way of keeping track of Imported bookmarks in the db try: # Pinboard stores timestamps in ISO format in UTC added_timestamp = datetime.datetime.strptime(link['time'], '%Y-%m-%dT%H:%M:%SZ') except KeyError: added_timestamp = datetime.datetime.utcnow() # This should never happen, but just in case try: # Don't want empty strings in the database, creates need for extra logic in the templates description = link['extended'] if link['extended'] != '' else None except KeyError: description = None self.urls[url] = { 'title': title, 'tags': tags, 'added_on': added_timestamp, 'description': description } for tag in tags: self.tags_set.add(tag) # Add all tags from current bookmark to the master tag set. No dupes. for tag in self.tags_set: if tag in self.tags_dict: # This tag has already been processed, and is in dict pass elif tag in created_tags: # We won't be handle created tags in this loop pass elif tag in tags_dictionary: # This tag already exists in the database self.tags_dict[tag] = {'obj': tags_dictionary[tag]} # Add the tag to the main tags object dictionary else: # If all else fails, create this tag created_tags[tag] = '' new_tag = Tag() new_tag.text = tag db.session.add(new_tag) db.session.commit() # This takes pretty long q = Tag.query.all() for tag in q: # Here we check if this was one of the tags that had to be created, and add to main tags dict if tag.text in created_tags: self.tags_dict[tag.text] = {'obj': tag}
def process(self): tags_query = Tag.query.all() # Get all the tags tags_dictionary = {} # Storing objects for existing tags in this dict for this_tag in tags_query: tags_dictionary[this_tag.text] = this_tag # Store all existing tags in dict created_tags = {} # Store all tags that had to be created urls = self.soup.findAll('a') for link in urls: url = link['href'] if not self.valid_url.match(url): pass else: if url in self.urls: pass else: tags = ['Imported'] if link.has_key('add_date'): added_timestamp = int(link['add_date']) else: added_timestamp = time.time() self.urls[url] = { 'title': link.text, 'tags': tags, 'added_on': datetime.datetime.utcfromtimestamp( added_timestamp) } self.tags_set.add('Imported') for tag in self.tags_set: if tag in self.tags_dict: # Tag has already been processed pass elif tag in created_tags: pass elif tag in tags_dictionary: self.tags_dict[tag] = {'obj': tags_dictionary[tag]} else: created_tags[tag] = '' new_tag = Tag() new_tag.text = tag db.session.add(new_tag) db.session.commit() q = Tag.query.all() for tag in q: if tag.text in created_tags: self.tags_dict[tag.text] = {'obj': tag}
def process(self): tags_query = Tag.query.all() tags_dictionary = dict() for this_tag in tags_query: tags_dictionary[this_tag.text] = this_tag created_tags = dict() for tag in self.soup.find_all('h1'): self.tags_set.add(tag.text) parent_elem = tag.find_next_sibling('ol') links = parent_elem.find_all('a') for link in links: if not self.valid_url.match(link['href']): pass else: title = link.text url = link['href'] tags = [tag.text] tags.append('Imported') # Thanks Instapaper for not adding timestamps added_timestamp = datetime.datetime.utcnow() self.urls[url] = { 'title': title, 'tags': tags, 'added_on': added_timestamp } self.tags_set.add('Imported') for tag in self.tags_set: if tag in self.tags_dict: pass elif tag in created_tags: pass elif tag in tags_dictionary: self.tags_dict[tag] = {'obj': tags_dictionary[tag]} else: created_tags[tag] = '' new_tag = Tag() new_tag.text = tag db.session.add(new_tag) db.session.commit() q = Tag.query.all() for tag in q: if tag.text in created_tags: self.tags_dict[tag.text] = {'obj': tag}
def process(self): tags_query = Tag.query.all() tags_dictionary = dict() for this_tag in tags_query: tags_dictionary[this_tag.text] = this_tag created_tags = dict() for link in self.soup.find_all('a'): title = link.text url = link['href'] dt = arrow.get(link['time_added']).naive tags = link['tags'].split(',') tags.append('Imported') tags = filter(None, tags) for tag in tags: self.tags_set.add(tag) self.urls[url] = { 'title': title, 'tags': tags, 'added_on': dt } for tag in self.tags_set: if tag in self.tags_dict: # Tag has already been processed pass elif tag in created_tags: pass elif tag in tags_dictionary: self.tags_dict[tag] = {'obj': tags_dictionary[tag]} else: created_tags[tag] = '' new_tag = Tag() new_tag.text = tag db.session.add(new_tag) db.session.commit() q = Tag.query.all() for tag in q: if tag.text in created_tags: self.tags_dict[tag.text] = {'obj': tag}