def index(request): convert_session_to_user(request) opts = { 'header': { 'is_loggedin': False, 'is_empty': False }, 'cat': get_catagories(request), 'active_cat': 'all' } opts['blogs'] = [] if request.user.is_authenticated: opts['header']['is_loggedin'] = True opts['blogs'] = User.get_feed(request.user) else: opts['blogs'] = User.get_feed() try: featured = Featured.pickOne().blog except Exception as e: print(e) opts['featured_blog'] = None else: opts['featured_blog'] = featured.get_obj_min() opts['featured_blog']['intro'] = html.escape(featured.content[:300]) res = render(request, 'index.html', opts) return res
def get_catagories(request): user = None session = None if request.user.is_authenticated: user = request.user else: session = get_session(request) return User.get_catagories(user, session)
def followers(request, username): try: user = User.get_by_username(username) except: return page404(request) else: data = {'header': {'is_loggedin': True}, 'user': user.get_profile_min(), 'chaselist': []} chaselist = user.get_followers() for follower in chaselist: data['chaselist'].append(follower.user.get_profile_min()) return render(request, 'followers.html', data)
def unfollow_user(request): if request.user.is_authenticated: if 'user_id' in request.POST: try: target = User.get_by_id(request.POST['user_id']) except: return apiRespond(400, msg='Target user not found') else: result = request.user.unfollow(target) return apiRespond(201, result=result) else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def follow_user(request): if request.user.is_authenticated: if 'user_id' in request.POST: try: target = User.get_by_id(request.POST['user_id']) except: return apiRespond(400, msg='Target user not found') else: result, obj = request.user.follow(target) Alert.create_alert(user=target, ref_user=request.user, type=Alert.FOLLOW, follow=obj) return apiRespond(201, result=result) else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def profile(request, username): opts = {'header': { 'is_loggedin': False, 'is_empty': False}, 'is_owner': request.user.is_authenticated and request.user.username == username} if request.user.is_authenticated: opts['header']['is_loggedin'] = True try: user = User.get_by_username(username) except: return page404(request) else: opts['user'] = user.get_profile( request.user if request.user.is_authenticated else None) res = render(request, 'profile.html', opts) return res