def login(request): res = SiteResponse(request) next_url = '/' user = None if 'next' in request.GET: next_url = request.GET['next'] if request.user.is_authenticated: return redirect(next_url) if request.method == 'POST' and _contains(request.POST, ['user', 'password']): user = authenticate(username=_clean(request.POST, 'user'), password=_clean(request.POST, 'password')) if user is not None: auth_login(request, user) return redirect(next_url) else: res.errors.append('Invalid username/password.') return render( request, settings.THEME_TEMPLATES['login'], { 'response': res, 'page_id': 0, 'next_url': next_url } )
def podcast(request): res = SiteResponse(request) user = None is_admin = False can_cast = False if request.user.is_authenticated: user = User.objects.get(id=request.user.id) is_admin = user.is_superuser can_cast = user.profile.can_cast podcast_list = [] page = int(_clean(request.GET, 'page', '1')) per_page = int(_clean(request.GET, 'perpage', '5')) if is_admin or can_cast: podcast_list = PodCast.objects.all().order_by('title') else: podcast_list = PodCast.objects.filter( published=True, pub_date__lte=datetime.now()).order_by('title') podcasts, paginator = _paginate(podcast_list, per_page, page) page, sections, editing, res = process_CMS(request, res, '/podcast') return render( request, settings.THEME_TEMPLATES['podcast'], { 'page_id': 0, 'podcasts': podcasts, 'paginator': paginator, 'is_admin': is_admin, 'can_cast': can_cast, 'response': res, 'page': page, 'sections': sections, 'editing': editing })
def create_resource(request): res = SiteResponse(request) page = '1' resource = { 'id': 'new', 'title': '', 'tags': '', 'content': 'Lorem ipsum...', 'published': False, 'pub_date': datetime.now() } user = User.objects.get(id=request.user.id) # DELETE EXISTING RESOURCE if request.method == 'GET' and _contains(request.GET, ['resource-id', 'delete']): resource = Resource.objects.get(id=_clean(request.GET, 'resource-id')) resource.delete() return redirect('/resources') # EDIT EXISTING RESOURCE if request.method == 'GET' and _contains(request.GET, ['resource-id']): resource = Resource.objects.get(id=_clean(request.GET, 'resource-id')) # SAVE POST elif request.method == 'POST' and _contains( request.POST, ['resource-id', 'title', 'nice-url', 'tags', 'content', 'pub-date']): page = _clean(request.GET, 'page', page) if _clean(request.POST, 'resource-id') == 'new': resource = Resource.objects.create(user=user) else: resource = Resource.objects.get( id=_clean(request.POST, 'resource-id')) resource.user = user resource.title = unescape(_clean(request.POST, 'title')) resource.nice_url = _clean(request.POST, 'nice-url') resource.content = unescape(_clean(request.POST, 'content')) resource.set_tags(_clean(request.POST, 'tags')) resource.published = 'published' in request.POST resource.sticky = 'sticky' in request.POST resource.pub_date = datetime.strptime(_clean(request.POST, 'pub-date'), '%m/%d/%Y %I:%M %p') resource.save() return redirect('/resources?page=' + page) return render( request, settings.THEME_TEMPLATES['create_resource'], { 'page_id': 0, 'is_admin': user.is_superuser, 'user_id': user.id, 'response': res, 'resource': resource, })
def resources(request): res = SiteResponse(request) user = None is_admin = False tag_id = None if request.user.is_authenticated: user = User.objects.get(id=request.user.id) is_admin = user.is_superuser resource_list = [] tag_list = [] page = int(_clean(request.GET, 'page', '1')) per_page = int(_clean(request.GET, 'perpage', '6')) if request.method == 'GET' and 'tag-id' in request.GET: tag_id = _clean(request.GET, 'tag-id') if is_admin: resource_list = Resource.objects.filter(tags__id=tag_id).order_by( '-sticky', 'title') tag_list = Tag.objects.filter( post__in=Resource.objects.all()).order_by('text').distinct() else: resource_list = Resource.objects.filter(tags__id=tag_id, published=True).order_by( '-sticky', 'title') tag_list = Tag.objects.filter(post__in=Resource.objects.filter( published=True)).order_by('text').distinct() else: if is_admin: resource_list = Resource.objects.all().order_by( 'tags__text', '-sticky', 'title') else: resource_list = Resource.objects.filter(published=True).order_by( 'tags__text', '-sticky', 'title') tag_list = Tag.objects.filter( post__in=resource_list).order_by('text').distinct() resources, paginator = _paginate(resource_list, per_page, page) page, sections, editing, res = process_CMS(request, res, '/resources') return render( request, settings.THEME_TEMPLATES['resources'], { 'page_id': 0, 'resources': resources, 'tags': tag_list, 'tag_id': tag_id, 'paginator': paginator, 'is_admin': is_admin, 'response': res, 'page': page, 'sections': sections, 'editing': editing })
def source_detail(request): res = SiteResponse(request) source = None if request.method == 'GET': id = _clean(request, 'id') source = Source.objects.get(id=id) return render(request, settings.THEME_TEMPLATES['bib_detail'], { 'source': source, 'response': res })
def pages(request, nice_url='/'): res = SiteResponse(request) page, sections, editing, res = process_CMS(request, res, nice_url) return render( request, settings.THEME_TEMPLATES['pages'], { 'response': res, 'page': page, 'sections': sections, 'editing': editing } )
def index(request): res = SiteResponse(request) page, sections, editing, res = process_CMS(request, res, '/') # GET RECENT POST, PODCAST, BIB ENTRY, AND RESOURCE try: post = BlogEntry.objects.filter(published=True, pub_date__lte=datetime.now()).order_by('-pub_date')[0:1][0] except: post = None try: podcast = PodCast.objects.filter(published=True, pub_date__lte=datetime.now()).order_by('-pub_date')[0:1][0] except: podcast = None try: source = Source.objects.filter().order_by('-id')[0:1][0] except: source = None try: resource = Resource.objects.filter(published=True, pub_date__lte=datetime.now()).order_by('-pub_date')[0:1][0] except: resource = None recent = { 'post': post, 'podcast': podcast, 'source': source, 'resource': resource } return render( request, settings.THEME_TEMPLATES['home_index'], { 'response': res, 'recent': recent, 'page': page, 'sections': sections, 'editing': editing } )
def view_resource(request, nice_url): res = SiteResponse(request) user = None resource = None related_resources = [] num_related = 5 user = None is_admin = False if request.user.is_authenticated: user = User.objects.get(id=request.user.id) is_admin = user.is_superuser full_url = "{0}{1}/resources/{2}".format(settings.PROTOCOL_PREFIX, settings.ALLOWED_HOSTS[0], nice_url) try: resource = Resource.objects.get(nice_url=escape(nice_url)) related_resources = Resource.objects.filter( tags__in=resource.tags.all(), published=True).exclude(id=resource.id).order_by( 'tags__text', 'title')[0:num_related] except: res.errors.append(traceback.format_exc()) if resource and user and request.method == 'POST' and _contains( request.POST, ['comment-text']): comment = Comment.objects.create(user=user, text=_clean(request.POST, 'comment-text')) resource.comments.add(comment) return render( request, settings.THEME_TEMPLATES['view_resource'], { 'response': res, 'page_id': 0, 'user': user, 'is_admin': is_admin, 'resource': resource, 'related_resources': related_resources, 'full_url': full_url })
def index(request): res = SiteResponse(request) is_admin = False can_blog = False if request.user.is_authenticated: is_admin = request.user.is_superuser user = User.objects.get(id=request.user.id) can_blog = user.profile.can_blog page, sections, editing, res = process_CMS(request, res, '/bibliography') return render( request, settings.THEME_TEMPLATES['bib_index'], { 'page_id': 40, 'response': res, 'is_admin': is_admin, 'can_blog': can_blog, 'page': page, 'sections': sections, 'editing': editing })
def view_blog(request, nice_url): res = SiteResponse(request) user = None post = None full_url = "{0}/blog/{1}".format(settings.SITE_PREFIX, nice_url) related_posts = [] num_related = 10 user = None is_admin = False if request.user.is_authenticated: user = User.objects.get(id=request.user.id) is_admin = user.is_superuser try: post = BlogEntry.objects.get(nice_url=escape(nice_url)) related_posts = BlogEntry.objects.filter( tags__in=post.tags.all(), published=True).exclude(id=post.id).order_by( 'tags__text', 'title')[0:num_related] except: res.errors.append(traceback.format_exc()) if post and user and request.method == 'POST' and _contains( request.POST, ['comment-text']): comment = Comment.objects.create(user=user, text=_clean(request.POST, 'comment-text')) post.comments.add(comment) return render( request, settings.THEME_TEMPLATES['view_blog'], { 'response': res, 'page_id': 0, 'user': user, 'is_admin': is_admin, 'post': post, 'full_url': full_url, 'related_posts': related_posts })
def search(request): res = SiteResponse(request) results = [] if 'q' in request.GET: query = _clean(request.GET, 'q') if 'pages' in settings.SEARCH_INCLUDES: results += search_pages(query) if 'posts' in settings.SEARCH_INCLUDES: # importing like this because post module optional from post.models import search_blog, search_podcasts, search_resources results += search_blog(query) results += search_resources(query) results += search_podcasts(query) return render( request, settings.THEME_TEMPLATES['search'], { 'response': res, 'results': results } )
def create_blog_post(request): res = SiteResponse(request) page = '1' blog_post = { 'id': 'new', 'title': '', 'tags': '', 'content': 'Lorem ipsum...', 'published': False, 'pub_date': datetime.now() } authors = User.objects.filter( Q(is_superuser=True) | Q(profile__can_blog=True)).order_by( 'last_name', 'first_name') # DELETE EXISTING POST if request.method == 'GET' and _contains(request.GET, ['post-id', 'delete']): blog_post = BlogEntry.objects.get(id=_clean(request.GET, 'post-id')) blog_post.delete() return redirect('/blog') # EDIT EXISTING POST if request.method == 'GET' and _contains(request.GET, ['post-id']): blog_post = BlogEntry.objects.get(id=_clean(request.GET, 'post-id')) # SAVE POST elif request.method == 'POST' and _contains(request.POST, [ 'post-id', 'title', 'nice-url', 'author-id', 'tags', 'content', 'pub-date' ]): page = _clean(request.GET, 'page', page) author = User.objects.get(id=_clean(request.POST, 'author-id')) if _clean(request.POST, 'post-id') == 'new': blog_post = BlogEntry.objects.create(user=author) else: blog_post = BlogEntry.objects.get( id=_clean(request.POST, 'post-id')) blog_post.user = author blog_post.title = unescape(_clean(request.POST, 'title')) blog_post.nice_url = _clean(request.POST, 'nice-url') blog_post.content = unescape(_clean(request.POST, 'content')) blog_post.pub_date = datetime.strptime( _clean(request.POST, 'pub-date'), '%m/%d/%Y %I:%M %p') blog_post.set_tags(_clean(request.POST, 'tags')) blog_post.published = 'published' in request.POST blog_post.sticky = 'sticky' in request.POST if 'featured-img' in request.POST: blog_post.featured_image = _clean(request.POST, 'featured-img') blog_post.save() return redirect('/blog?page=' + page) elif request.method == 'POST': res.errors.append('Please fill out all required fields.') return render( request, settings.THEME_TEMPLATES['create_blog'], { 'page_id': 0, 'authors': authors, 'is_admin': request.user.is_superuser, 'user_id': request.user.id, 'response': res, 'blog_post': blog_post, })
def create_podcast(request): res = SiteResponse(request) podcast = { 'id': 'new', 'title': '', 'tags': '', 'summary': 'Lorem ipsum...', 'url': '', 'published': False, 'pub_date': datetime.now() } authors = User.objects.filter( Q(is_superuser=True) | Q(profile__can_cast=True)).order_by( 'last_name', 'first_name') # DELETE EXISTING POST if request.method == 'GET' and _contains(request.GET, ['podcast-id', 'delete']): podcast = PodCast.objects.get(id=_clean(request.GET, 'podcast-id')) podcast.delete() return redirect('/podcast') # EDIT EXISTING POST if request.method == 'GET' and _contains(request.GET, ['podcast-id']): podcast = PodCast.objects.get(id=_clean(request.GET, 'podcast-id')) # SAVE POST elif request.method == 'POST' and _contains(request.POST, [ 'podcast-id', 'title', 'nice-url', 'author-id', 'url', 'summary', 'pub-date' ]): author = User.objects.get(id=_clean(request.POST, 'author-id')) if _clean(request.POST, 'podcast-id') == 'new': podcast = PodCast.objects.create(user=author) else: podcast = PodCast.objects.get( id=_clean(request.POST, 'podcast-id')) podcast.user = author podcast.title = unescape(_clean(request.POST, 'title')) podcast.nice_url = _clean(request.POST, 'nice-url') podcast.summary = unescape(_clean(request.POST, 'summary')) if 'itunes-summary' in request.POST: podcast.itunes_summary = _clean(request.POST, 'itunes-summary') if _clean(request.POST, 'url'): podcast_path = settings.MEDIA_ROOT + '/' + _clean( request.POST, 'url') audio = MP3(podcast_path) podcast.set_duration(audio.info.length) podcast.byte_size = str(os.path.getsize(podcast_path)) podcast.pub_date = datetime.strptime(_clean(request.POST, 'pub-date'), '%m/%d/%Y %I:%M %p') podcast.url = _clean(request.POST, 'url') podcast.published = 'published' in request.POST if not podcast.guid: podcast.guid = str(uuid.uuid4()) podcast.save() return redirect('/podcast') return render( request, settings.THEME_TEMPLATES['create_podcast'], { 'page_id': 0, 'authors': authors, 'is_admin': request.user.is_superuser, 'user_id': request.user.id, 'response': res, 'podcast': podcast, })
def blog(request): res = SiteResponse(request) user = None tag_id = None is_admin = False can_blog = False if request.user.is_authenticated: user = User.objects.get(id=request.user.id) is_admin = user.is_superuser can_blog = user.profile.can_blog post_list = [] tag_list = [] page = int(_clean(request.GET, 'page', '1')) per_page = int(_clean(request.GET, 'perpage', '6')) if request.method == 'GET' and ('tag-id' in request.GET or 'tag' in request.GET): filtered = True filtering_tag = None if 'tag-id' in request.GET: filtering_tag = Tag.objects.get(id=_clean(request.GET, 'tag-id')) elif 'tag' in request.GET: filtering_tag = Tag.objects.get(text=_clean(request.GET, 'tag')) tag_id = filtering_tag.id if is_admin or can_blog: post_list = BlogEntry.objects.filter( tags__in=[filtering_tag]).order_by('-sticky', '-pub_date') tag_list = Tag.objects.filter( post__in=BlogEntry.objects.all()).order_by('text').distinct() else: post_list = BlogEntry.objects.filter( tags__in=[filtering_tag], published=True, pub_date__lte=datetime.now()).order_by('-sticky', '-pub_date') tag_list = Tag.objects.filter(post__in=BlogEntry.objects.filter( published=True, pub_date__lte=datetime.now())).order_by( 'text').distinct() else: if is_admin or can_blog: post_list = BlogEntry.objects.all().order_by( '-sticky', '-pub_date') else: post_list = BlogEntry.objects.filter( published=True, pub_date__lte=datetime.now()).exclude( tags__text__in=settings.BLOG_HIDDEN_TAGS).order_by( '-sticky', '-pub_date') tag_list = Tag.objects.filter( post__in=post_list).order_by('text').distinct() posts, paginator = _paginate(post_list, per_page, page) page, sections, editing, res = process_CMS(request, res, '/blog') return render( request, settings.THEME_TEMPLATES['blog'], { 'page_id': 0, 'posts': posts, 'tags': tag_list, 'paginator': paginator, 'is_admin': is_admin, 'can_blog': can_blog, 'tag_id': tag_id, 'response': res, 'page': page, 'sections': sections, 'editing': editing })
def register(request): res = SiteResponse(request) next_url = '/' new_user = True if 'next' in request.GET: next_url = request.GET['next'] user = { 'id': 'new', 'username': '', 'email': '', 'first_name': '', 'last_name': '', 'is_superuser': False, 'profile': { 'website': '', 'can_blog': False, 'can_cast': False, 'subscribe_newsletter': True, } } is_admin = request.user.is_superuser if is_admin and 'impersonate-username' in request.GET: user = User.objects.get(username=_clean(request.GET, 'impersonate-username')) new_user = False elif is_admin and 'create' in request.GET: pass elif request.user.is_authenticated: new_user = False user = User.objects.get(id=request.user.id) if request.method == 'GET' and 'logout' in request.GET: logout(request) return redirect('/') if request.method == 'POST' and _contains(request.POST, [ 'user', 'password', 'email', 'fname', 'lname', 'website' ]): username = _clean(request.POST, 'user') password = _clean(request.POST, 'password') email = _clean(request.POST, 'email') fname = _clean(request.POST, 'fname') lname = _clean(request.POST, 'lname') website = _clean(request.POST, 'website') if 'password2' in request.POST: if not password == _clean(request.POST, 'password2'): user['username'] = username user['email'] = email user['first_name'] = fname user['last_name'] = lname user['profile']['website'] = website res.errors.append('Your passwords do not match.') if not res.errors: if hasattr(user, 'id'): user.username = username if password: user.set_password(password) user.email = email else: user = User.objects.create_user( username, email, password ) user.first_name = fname user.last_name = lname user.is_superuser = '******' in request.POST user.profile.website = website user.profile.can_blog = 'can-blog' in request.POST user.profile.can_cast = 'can-cast' in request.POST if user.is_superuser or user.profile.can_blog or user.profile.can_cast: user.is_staff = True user.profile.subscribe_newsletter = 'subscribe' in request.POST user.save() if 'create' in request.GET: return redirect('/accounts/register/?impersonate=' + str(user.id)) else: if next_url: auth_login(request, user) return redirect(next_url) elif new_user: auth_login(request, user) res.success = 'Registration complete!' else: res.success = 'Profile updated.' return render( request, settings.THEME_TEMPLATES['create_user'], { 'response': res, 'page_id': 0, 'user': user, 'next': next_url, 'is_admin': is_admin } )