def index(): from .badge_blueprint import badge_description_to_json kwargs = { "circles": [circle_to_json(c) for c in Circle.query.all()], "badge_descriptions": [badge_description_to_json(bd) for bd in BadgeDescription.query.all()], "stripe_pubkey": stripe_pubkey, "gitRevision": current_app.config.get("GIT_REVISION", None) } if flask_login.current_user.is_authenticated: account = flask_login.current_user.account # type: Account circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) circles_with_admin_access = account_management.get_circles_with_admin_access( account.id) account_json = profile_to_json(account, circles, badges, full=True) circles_with_admin_access_json = [ circle_to_json(c) for c in circles_with_admin_access ] kwargs["profile"] = account_json kwargs["circles_with_admin_access"] = [ c.id for c in circles_with_admin_access ] return render_template("index.html", **kwargs)
def data_account(account_id): account = Account.find_account_by_id(account_id) if account is None: abort(404) circles = account_management.get_circles_for_account(account.id) # Get current membership membership = get_membership(account) membership_details = {} if membership is not None: membership_details['fee'] = membership.fee membership_details['first_membership'] = membership.first_membership membership_details['start_membership'] = membership.start_membership else: membership_details['fee'] = 0 # Export payments payments = [] for pay in get_membership_payments(account): payments.append({ 'id': pay.id, 'start_date': pay.start_date, 'end_date': pay.end_date, 'amount': float(pay.amount), 'payment_date': pay.payment_date }) membership_details['payments'] = payments detail = account_to_json(account, circles, None) detail['membership'] = membership_details return jsonify(detail)
def can_haz_door_access(account, doors=[]): # If called with no doors, this basically checks if you are paying member # or employed at a company. It is used to show the buttons on the front page # for now, in the future, we probably want to make those dependant on access, # so you only see buttons you have access to. # Employed people have access to all doors for now if Company.is_account_employed(account.id): return True # If you aren't a paying member, you have access to no doors # Check paying membership if not StripePayment.is_account_paying_member(account.id): return False # Find all circles the account is a member of, then iterate the doors and check memberships = { circle.name for circle in account_management.get_circles_for_account(account.id) } for door in doors: if not memberships & door.circles: # No overlap, so we lack access to this door, lets return false return False # If we get here, we had access to all doors, so return True return True
def data_account(account_id): account = Account.find_account_by_id(account_id) if account is None: abort(404) circles = account_management.get_circles_for_account(account.id) return jsonify(account_to_json(account, circles, None))
def index(): if flask_login.current_user.is_authenticated: account = flask_login.current_user.account circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) account = account_to_json(account, circles, badges) else: account = None return render_template("index.html", account=account)
def data_account_list(): accounts_plus_plus = [ (account, account_management.get_circles_for_account(account.id), badge_management.badges_for_account(account.id)) for account in Account.query.all() ] accounts = [ account_to_json(account, circles, badges) for (account, circles, badges) in accounts_plus_plus ] return jsonify(accounts)
def account_loader(account_id): # logger.info("login_manager.user_loader: Loading account_id={}".format(account_id)) account = Account.find_account_by_id(account_id) if account is None: logger.info( "login_manager.user_loader: no such account".format(account_id)) return # logger.info("login_manager.user_loader: Loaded account.id={}, account.username={}". # format(account.id, account.username)) circles = account_management.get_circles_for_account(account.id) return AuthenticatedAccount(account, circles)
def login(): if flask.request.method == 'GET': show_message = flask.request.args.get('show_message') or '' username = flask.request.args.get('username') or '' return render_template("login.html", show_message=show_message, username=username) username = flask.request.form['username'] account = Account.find_account_by_username(username) password = flask.request.form['password'] if account.valid_password(password): circles = account_management.get_circles_for_account(account.id) logger.info("User {} logged in, circles={}".format(username, circles)) authenticated_account = auth.AuthenticatedAccount(account, circles) flask_login.login_user(authenticated_account) return flask.redirect(flask.url_for('core.index')) return flask.redirect(flask.url_for('.login', show_message='bad-login', username=username))
def data_account_summary(account_id): account = Account.find_account_by_id(account_id) if account is None: abort(404) circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) open_door_event = event_management.last_door_open(account) from .badge_blueprint import badge_to_json summary = { "account": account_to_json(account, circles, None), "badges": [badge_to_json(b) for b in badges], "lastDoorOpen": open_door_event.to_dict() if open_door_event else None } return jsonify(summary)
def service_authz_login(): username = request.json["username"] account = Account.find_account_by_username(username) password = request.json["password"] if not account: logger.info("Login: Bad login attempt, no such user: {}".format(username)) raise P2k16UserException("Invalid credentials") if not account.valid_password(password): logger.info("Login: Bad login attempt, wrong password: {}".format(username)) raise P2k16UserException("Invalid credentials") circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) logger.info("Login: username={}, circles={}".format(username, circles)) authenticated_account = auth.AuthenticatedAccount(account, circles) flask_login.login_user(authenticated_account) return jsonify(account_to_json(account, circles, badges))
def _manage_membership(account_id: int, create: bool): account = Account.find_account_by_id(account_id) if account is None: abort(404) circle_id = request.json["circle_id"] a = flask_login.current_user.account if create: account_management.add_account_to_circle(account.id, circle_id, a.id) else: account_management.remove_account_from_circle(account.id, circle_id, a.id) circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) db.session.commit() return jsonify(account_to_json(account, circles, badges))
def create(): account = flask_login.current_user.account # type: Account title = request.json["title"] recipient_username = request.json.get("recipient", None) if recipient_username: recipient = Account.find_account_by_username(recipient_username) if not recipient: raise P2k16UserException( "No such username: {}".format(recipient_username)) else: recipient = account badge_management.create_badge(recipient, account, title) circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) db.session.commit() return jsonify(account_to_json(account, circles, badges))