87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
"""
|
|
Dynamic Link Page Manager utilities
|
|
"""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
# In-memory storage for dynamic link pages (in production, use a database)
|
|
link_pages_db = {}
|
|
|
|
class LinkPageManager:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def create_link_page(self, title="My Links", description="Collection of useful links"):
|
|
"""Create a new dynamic link page"""
|
|
page_id = str(uuid.uuid4())
|
|
page_data = {
|
|
'id': page_id,
|
|
'title': title,
|
|
'description': description,
|
|
'links': [],
|
|
'created_at': datetime.now().isoformat(),
|
|
'updated_at': datetime.now().isoformat(),
|
|
'view_count': 0
|
|
}
|
|
link_pages_db[page_id] = page_data
|
|
return page_id
|
|
|
|
def add_link(self, page_id, title, url, description=""):
|
|
"""Add a link to a page"""
|
|
if page_id not in link_pages_db:
|
|
return False
|
|
|
|
link_data = {
|
|
'id': str(uuid.uuid4()),
|
|
'title': title,
|
|
'url': url if url.startswith(('http://', 'https://')) else f'https://{url}',
|
|
'description': description,
|
|
'created_at': datetime.now().isoformat()
|
|
}
|
|
|
|
link_pages_db[page_id]['links'].append(link_data)
|
|
link_pages_db[page_id]['updated_at'] = datetime.now().isoformat()
|
|
return True
|
|
|
|
def update_link(self, page_id, link_id, title=None, url=None, description=None):
|
|
"""Update a specific link"""
|
|
if page_id not in link_pages_db:
|
|
return False
|
|
|
|
for link in link_pages_db[page_id]['links']:
|
|
if link['id'] == link_id:
|
|
if title is not None:
|
|
link['title'] = title
|
|
if url is not None:
|
|
link['url'] = url if url.startswith(('http://', 'https://')) else f'https://{url}'
|
|
if description is not None:
|
|
link['description'] = description
|
|
|
|
link_pages_db[page_id]['updated_at'] = datetime.now().isoformat()
|
|
return True
|
|
return False
|
|
|
|
def delete_link(self, page_id, link_id):
|
|
"""Delete a specific link"""
|
|
if page_id not in link_pages_db:
|
|
return False
|
|
|
|
links = link_pages_db[page_id]['links']
|
|
link_pages_db[page_id]['links'] = [link for link in links if link['id'] != link_id]
|
|
link_pages_db[page_id]['updated_at'] = datetime.now().isoformat()
|
|
return True
|
|
|
|
def increment_view_count(self, page_id):
|
|
"""Increment view count for a page"""
|
|
if page_id in link_pages_db:
|
|
link_pages_db[page_id]['view_count'] += 1
|
|
|
|
def get_page(self, page_id):
|
|
"""Get page data"""
|
|
return link_pages_db.get(page_id)
|
|
|
|
def page_exists(self, page_id):
|
|
"""Check if page exists"""
|
|
return page_id in link_pages_db
|