Exemplo n.º 1
0
def user_activity_cached(day, user, update=False, timeout=3600):
    cache_key = "USER_ACTIVITY_NOW_{}_{}".format(day, user)

    result = cache.get(cache_key)
    if update:
        result = None
    if result is not None:
        d, users = result
    else:
        active = find_user_activity_now(day, user)

        start_date = datetime(2018, 1, 1, 0, 0, 0)
        d = []
        v = []
        users = set()
        for x in active:
            v.append(x)
            users.add(x[0])

        for td in (start_date + timedelta(hours=1 * it) for it in xrange(24)):
            row = dict()
            row['y'] = td.strftime("%H:%M")
            for u in users:
                row[u] = 0
            d.append(row)

        for r in v:
            for item in range(0, len(d)):
                if d[item]['y'] == r[1]:
                    d[item][r[0]] = r[2]

        cache.set(cache_key, (d, users), timeout=timeout)
    return d, users
def distinct(A):
    n = len(A)
    A.sort()
    result = 1
    for k in xrange(1, n):
        if A[k] != A[k - 1]:
            result += 1
        return result
Exemplo n.º 3
0
class FormProva(FlaskForm):
    disciplina = SelectField('Disciplina',
                             coerce=int,
                             validators=[InputRequired])
    perg = IntegerField('Quantidade de perguntas:',
                        validators=[DataRequired("")])
    anos = SelectField('Última vez usada em: (anos)',
                       coerce=int,
                       choices=[(0, 'Nunca usada')] + [(i, i)
                                                       for i in xrange(1, 15)])
    ano_atual = SelectField('Ano atual:',
                            coerce=int,
                            choices=[(i, i) for i in xrange(2015, 2020)])
    semestre = SelectField('Semestre atual:',
                           coerce=int,
                           choices=[(1, 1), (2, 2)])
    submit = SubmitField('Próximo')
Exemplo n.º 4
0
def prefix_sums(A):
    A = [2, 8, 9, 5, 7, 8]
    sums = [0] * (len(A) + 1)
    print("sum is", sums)
    n = len(A)
    P = [0] * n
    P[0] = A[0]
    for k in xrange(1, n):
        P[k] = P[k - 1] + A[k]
    return P
Exemplo n.º 5
0
def showLogin():
    global code
    picture_url = "/static/img/notlogged.png"
    print("requested login")
    print(request.method)
    if request.method == "POST":
        print("Called login post request method")
        # If this request does not have `X-Requested-With`
        # header, this could be a CSRF
        if not request.headers.get('X-Requested-With'):
            abort(403)
        if request.headers.get('logged-out') == 'true':
            print("a log out request")
            code = ''
            login_session.pop('userId')
            return showCategories()
        print(request.data)
        auth_code = request.data
        # Exchange auth code for access token,
        # refresh token, and ID token
        credentials = \
            client.credentials_from_clientsecrets_and_code(
                CLIENT_SECRET_FILE,
                ['https://www.googleapis.com/auth/drive.appdata',
                 'profile', 'email'],
                auth_code)
        # Call Google API
        print(credentials.id_token)
        # Get profile info from ID token
        userid = credentials.id_token['sub']
        email = credentials.id_token['email']
        picture = credentials.id_token['picture']
        picture_url = picture
        name = credentials.id_token['given_name']
        # check if user already exists
        if session.query(User.id).filter_by(id=userid) \
                .scalar() is None:
            new_user = \
                User(id=userid, name=name,
                     email=email, picture=picture)
            session.add(new_user)
            session.commit()
        login_session['userId'] = userid
        print("Login session user id is : " + login_session['userId'])
    state = ''.join(
        random.choice(string.ascii_uppercase + string.digits)
        for x in xrange(32))
    login_session['state'] = state
    return render_template('main.html', STATE=state, picture_url=picture_url)
Exemplo n.º 6
0
def binomial(n, k):
    """
    A fast way to calculate binomial coefficients by Andrew Dalke.
    See http://stackoverflow.com/questions/3025162/statistics-combinations-in-python
    """
    if 0 <= k <= n:
        ntok = 1
        ktok = 1
        for t in xrange(1, min(k, n - k) + 1):
            ntok *= n
            ktok *= t
            n -= 1
        return ntok // ktok
    else:
        return 0
def showLogin():
    """ Render the login page after a random state token is created.

    Creates a random anti-forgery state token with each GET request sent to
    localhost:5000/login before rendering the login page.

    Returns:
        The login page.
    """

    # Create a variable which will be 32 characters long and a mix of uppercase
    # letters and digits.

    state = ''.join(random.choice(
        string.ascii_uppercase + string.digits) for x in xrange(32))
    # Store the state token in the login_session object.
    login_session['state'] = state
    return render_template('login.html', STATE=state)
Exemplo n.º 8
0
def showLogin():
    """ Render the login page after a random state token is created.

    Creates a random anti-forgery state token with each GET request sent to
    localhost:5000/login before rendering the login page.

    Returns:
        The login page.
    """

    # Create a variable which will be 32 characters long and a mix of uppercase
    # letters and digits.

    state = ''.join(
        random.choice(string.ascii_uppercase + string.digits)
        for x in xrange(32))
    # Store the state token in the login_session object.
    login_session['state'] = state
    return render_template('login.html', STATE=state)
Exemplo n.º 9
0
def new_state():
    state = ''.join(
        random.choice(string.ascii_uppercase + string.digits)
        for x in xrange(32))
    login_session['state'] = state
    return state
Exemplo n.º 10
0
import random
import datetime

from flask_sqlalchemy import xrange

from main import User, Tag, Post, db

user = User.query.get(1)
tag_one = Tag('Python')
tag_two = Tag('Flask')
tag_three = Tag('SQLAlechemy')
tag_four = Tag('Jinja')
tag_list = [tag_one, tag_two, tag_three, tag_four]

s = "Example text"

for i in xrange(100):
    new_post = Post("Post" + str(i))
    new_post.user = user
    new_post.publish_date = datetime.datetime.now()
    new_post.text = s
    new_post.tags = random.sample(tag_list, random.randint(1, 3))
    db.session.add(new_post)

db.session.commit()