pytwask package

Submodules

pytwask.config module

This module defines the configurations of the Flask application.

class pytwask.config.Config

Bases: object

The base configuration which defines the common parameters which will be inherited by the child configurations.

Note

If ‘REDIS_DB_SOCKET’ is specified and non-empty, it will override hostname and port.

DEBUG = False
REDIS_DB_HOSTNAME = '127.0.0.1'
REDIS_DB_INDEX = 0
REDIS_DB_PASSWORD = ''
REDIS_DB_PORT = 6379
REDIS_DB_SOCKET = ''
SECRET_KEY = b'c\x04\x14\x00;\xe44 \xf4\xf3-_9B\x1d\x15u\x02g\x1a\xcc\xd8\x04~'
class pytwask.config.DevelopmentConfig

Bases: pytwask.config.Config

The configuration for development.

DEBUG = True
REDIS_DB_INDEX = 1
class pytwask.config.ProductionConfig

Bases: pytwask.config.Config

The production configuration.

class pytwask.config.TestingConfig

Bases: pytwask.config.Config

The configuration for testing.

REDIS_DB_INDEX = 15
SERVER_NAME = 'localhost'
TESTING = True
WTF_CSRF_ENABLED = False

pytwask.models module

This module defines the two data models ‘Tweet’ and ‘User’ which are used by views and templates.

class pytwask.models.Tweet(username, post_unix_time, body)

Bases: object

This ‘Tweet’ class encapsulates all the information related to one tweet.

Note that it will convert the posted UNIX timestamp into a datetime.

static get_general_timeline()

Get the general timeline. Since the general timeline doesn’t belong to any user, it is defined as a static method of the ‘Tweet’ class.

Returns:
Return type:A list of ‘Tweet’ instances.
class pytwask.models.User(username, auth_secret, session_token)

Bases: flask_login.mixins.UserMixin

This ‘User’ class is mainly used by flask-login to manage the user authentication. It can also be used to retrieve and modify user-specific data, e.g., getting user timeline, changing user password.

change_password(old_password, new_password)

Change the user password.

Parameters:
  • old_password (str) – The old password
  • new_password (str) – The new password
Raises:

ValueError – If failed to change the password.

static create_new_user(password)

Create a new user with the given username and password.

Parameters:
  • username (str) –
  • password (str) –
Raises:

ValueError – If failed to create the new user.

follow(followee_username)

Follow another user by his/her username.

Parameters:followee_username (str) –
Raises:ValueError – If failed to follow the user
static get()

Get a ‘User’ instance from the session token.

Parameters:session_token (str) –
Returns:
Return type:A ‘User’ instance if session_token exists; otherwise None.
static get_by_username_and_password(password)

Get a ‘User’ instance from the username and the password.

Parameters:
  • username (str) –
  • password (str) –
Returns:

Return type:

A ‘User’ instance if username and password are correct; otherwise None.

get_followers()

Get the follower list.

Returns:
Return type:A set of the usernames of the followers.
get_followings()

Get the following list.

Returns:
Return type:A set of the usernames of the followings.
get_id()

Get the user ID which is the session token in our case.

get_user_timeline()

Get the user timeline.

Returns:
Return type:A list of ‘Tweet’ instances.
get_user_tweets(username)

Get the tweets posted by a given user.

Parameters:username (str) –
Returns:
Return type:A list of ‘Tweet’ instances.
post_tweet(body)

Post a tweet.

Parameters:body (str) – The tweet body that will be posted.
Raises:ValueError – If failed to post the tweet.
unfollow(followee_username)

Unfollow another user by his/her username.

Parameters:followee_username (str) –
Raises:ValueError – If failed to unfollow the user.

Module contents

Package pytwask

pytwask.create_app(config_name)

Create and initialize the Flask application.

Here we follow the Flask typical pattern of “Application Factories”. For details, please refer to:

http://flask.pocoo.org/docs/0.12/patterns/appfactories/