Skip to content

Commit

Permalink
Merge pull request #100 from codethechange/guest-mode
Browse files Browse the repository at this point in the history
Let users who aren't logged-in still browse public information like network posts. This aligns the app functionality with the website.
  • Loading branch information
U8NWXD authored Jan 11, 2019
2 parents 16dc39f + 896c0ab commit b7c469c
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 39 deletions.
Binary file added .DS_Store
Binary file not shown.
27 changes: 16 additions & 11 deletions culturemesh/blueprints/events/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from culturemesh.utils import get_network_title
from culturemesh.utils import get_event_location
from culturemesh.utils import safe_get_query_arg
from culturemesh.utils import is_logged_in

from culturemesh.blueprints.events.forms.event_forms import *
from culturemesh.blueprints.networks.utils import gather_network_info
Expand All @@ -24,7 +25,6 @@ def ping():
return c.ping_event()

@events.route("/")
@login_required
def render_event():
current_event_id = request.args.get('id')
c = Client(mock=False)
Expand All @@ -40,27 +40,32 @@ def render_event():

role = None

if event['id_host'] == current_user.id:
if is_logged_in(current_user):
user_id = current_user.id
if event['id_host'] == user_id:

# The current user is hosting this event.
role = 'hosting'
host['username'] = 'you'

# The current user is hosting this event.
role = 'hosting'
host['username'] = 'you'
elif user_is_attending_event(c, user_id, event):

elif user_is_attending_event(c, current_user.id, event):
# The current user is already signed up for this event.
role = 'attending'
else:

# The current user is already signed up for this event.
role = 'attending'
# The current user is not signed up for this event.
pass
else:
user_id = None

# The current user is not signed up for this event.
pass

return render_template(
'event.html',
event=event,
host=host,
role=role,
curr_user_id=current_user.id,
curr_user_id=user_id,
join_form=EventJoinForm(),
leave_form=EventLeaveForm(),
cancel_form=EventCancelForm()
Expand Down
34 changes: 16 additions & 18 deletions culturemesh/blueprints/networks/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from culturemesh.utils import get_network_title
from culturemesh.utils import get_upcoming_events_by_network
from culturemesh.utils import get_time_ago
from culturemesh.utils import is_logged_in
from utils import parse_date

from culturemesh.blueprints.networks.forms.network_forms import NetworkJoinForm
Expand All @@ -22,13 +23,12 @@
utc=pytz.UTC

@networks.route("/")
@flask_login.login_required
def network():
id_network = request.args.get('id')
c = Client(mock=False)
id_user = current_user.id
network_info = gather_network_info(id_network, id_user, c)

id_user = current_user.get_id()
network_info = gather_network_info(id_network, id_user, c)
upcoming_events = get_upcoming_events_by_network(c, id_network, 3)

return render_template(
Expand All @@ -54,7 +54,6 @@ def join_network():
)

@networks.route("/events/")
@flask_login.login_required
def network_events() :
# TODO: A lot of this code is repeated from network(), with just minor variations.
# This should be factored out.
Expand Down Expand Up @@ -93,13 +92,14 @@ def network_events() :
utils.enhance_event_date_info(event)
event['num_registered'] = c.get_event_reg_count(event['id'])['reg_count']

id_user = current_user.id
user_networks = c.get_user_networks(id_user, count=100)
user_is_member = False
for network_ in user_networks:
if int(id_network) == int(network_['id']):
user_is_member = True
break

if is_logged_in(current_user):
user_networks = c.get_user_networks(current_user.get_id(), count=100)
for network_ in user_networks:
if int(id_network) == int(network_['id']):
user_is_member = True
break

network_info = {}
network_info['id'] = id_network
Expand All @@ -121,7 +121,6 @@ def network_events() :
)

@networks.route("/posts/")
@flask_login.login_required
def network_posts() :
# TODO: A lot of this code is repeated from network(), with just minor variations.
# This should be factored out.
Expand Down Expand Up @@ -165,13 +164,13 @@ def network_posts() :
else :
post_index = posts[-1]['id']

id_user = current_user.id
user_networks = c.get_user_networks(id_user, count=100)
user_is_member = False
for network_ in user_networks:
if int(id_network) == int(network_['id']):
user_is_member = True
break
if is_logged_in(current_user):
user_networks = c.get_user_networks(current_user.get_id(), count=100)
for network_ in user_networks:
if int(id_network) == int(network_['id']):
user_is_member = True
break

network_info = {}
network_info['id'] = id_network
Expand Down Expand Up @@ -366,4 +365,3 @@ def leave():
def ping():
c = Client(mock=False)
return c.ping_network()

11 changes: 6 additions & 5 deletions culturemesh/blueprints/networks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ def gather_network_info(id_network, id_user, client, scenario="normal"):
post['reply_count'] = client.get_post_reply_count(post['id'])['reply_count']
post['time_ago'] = get_time_ago(post['post_date'])

user_networks = client.get_user_networks(id_user, count=100)
user_is_member = False
for network_ in user_networks:
if int(id_network) == int(network_['id']):
user_is_member = True
break
if not id_user == None:
user_networks = client.get_user_networks(id_user, count=100)
for network_ in user_networks:
if int(id_network) == int(network_['id']):
user_is_member = True
break

network_info = {}
network_info['id'] = id_network
Expand Down
8 changes: 6 additions & 2 deletions culturemesh/blueprints/posts/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from culturemesh.utils import get_time_ago
from culturemesh.utils import get_network_title
from culturemesh.utils import safe_get_query_arg
from culturemesh.utils import is_logged_in

from culturemesh.blueprints.posts.forms.post_forms import *

Expand All @@ -16,12 +17,11 @@
posts = Blueprint('posts', __name__, template_folder='templates')

@posts.route("/", methods=['GET', 'POST'])
@flask_login.login_required
def render_post():

current_post_id = safe_get_query_arg(request, 'id')

user_id = current_user.id
user_id = current_user.get_id()
c = Client(mock=False)
post = c.get_post(current_post_id)

Expand All @@ -41,6 +41,10 @@ def render_post():

if request.method == 'GET':
pass
elif not is_logged_in(current_user):
return redirect(
url_for('render_login_page')
)
else:
data = request.form
form_submitted = CreatePostReplyForm(request.form)
Expand Down
2 changes: 1 addition & 1 deletion culturemesh/blueprints/user_home/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def update_profile_and_render_home():
'id': user_id, 'first_name': first_name,
'last_name': last_name, 'about_me': about_me
}

if form.validate():
c.update_user(current_user, user)

Expand Down
1 change: 0 additions & 1 deletion culturemesh/blueprints/users/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
users = Blueprint('users', __name__, template_folder='templates')

@users.route('/<int:user_id>/', methods=['GET'])
@flask_login.login_required
def user_profile(user_id):

c = Client(mock=False)
Expand Down
3 changes: 3 additions & 0 deletions culturemesh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,6 @@ def user_is_attending_event(client, user_id, event):
if user_id == reg['id_guest']:
return True
return False

def is_logged_in(current_user):
return current_user.get_id() != None
1 change: 0 additions & 1 deletion culturemesh/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,3 @@ def page_not_found(e):
@app.errorhandler(httplib.METHOD_NOT_ALLOWED)
def internal_server_error(e):
return render_template('error.html')

0 comments on commit b7c469c

Please sign in to comment.