from server.app import api, db from server.models.Requests import Requests from server.app import mdb #output form opening_request = api.model( 'Opening Request', { 'other_user_id': fields.String, 'other_user_name': fields.String, 'from_time': fields.Integer, 'to_time': fields.Integer, 'req_accepted': fields.Boolean, 'req_confirmed': fields.Boolean }) NS = api.namespace('get_opening_request', description='Get all opening requests of a user') @NS.route('') class GetOpeningRequest(Resource): # @NS.marshal_list_with(opening_request) @token_required def get(self, cur_user): # Search the request table and return all opening requests (confirmed = false, acceptd = false) open_requests = mdb.requests.find({'other_user_id': cur_user.id}) reqs = [] for r in open_requests: r['_id'] = str(r['_id']) r['date'] = str(r['date']) reqs.append(r) return reqs
#input form update_form = api.model( 'Update form', { 'user_name': fields.String, 'email': fields.String, 'pref_timezone': fields.String, 'native_language': fields.String, 'wanted_language': fields.String }) #output form status = api.model('Status', { 'message': fields.String, }) NS = api.namespace('update_user', description='Update a user\'s information') @NS.route('') class UpdateUser(Resource): #What you post @NS.expect(update_form) #What you return @NS.marshal_with(status) #Checking if the user is registered @token_required def patch(self, cur_user): # If cur_user if not authenticated, decorator return to the client without entering this function user_request = request.get_json() # ImmutableMultiDict if user_request == None: user_request = request.form
from flask import request from flask_restplus import Resource, fields from server.app import api, db from server.models.Users import Users from server.api import token_required NS = api.namespace('get_user', description='Get user info') USER_MODEL = api.model('User model', { 'id': fields.Integer, 'user_name': fields.String, 'email': fields.String, 'pref_timezone': fields.String, 'learning_id': fields.Integer, 'learning_language': fields.String, 'native_id': fields.Integer, 'native_language': fields.String }) @NS.route('') class GetUser(Resource): @NS.marshal_with(USER_MODEL) @token_required def get(self, user): resp = { 'id': user.id, 'user_name': user.user_name, 'email': user.email, 'pref_timezone': user.pref_timezone }
from flask import request from server.api import token_required from flask_restplus import Resource, fields from server.app import api, db from server.models.Availabilities import Availabilities from server.models.Timezones import Timezones from server.models.Users import Users NS = api.namespace('availabilities', description='Endpoint for adding a new availability.') success = api.model('Success', { 'message': fields.String, 'success': fields.Boolean }) availability = api.model('Availability', { 'id': fields.Integer, 'user_id': fields.Integer, 'day_of_week': fields.Integer, 'from_time': fields.Integer, 'to_time': fields.Integer }) all_availabilities = api.model('availabilities', { 'availabilities': fields.List(fields.Nested(availability)) }) form = api.model('availability form', { 'user_id': fields.Integer, 'day_of_week': fields.Integer,
from flask_restplus import Resource from server.app import api, db NS = api.namespace('sample', description="Sample route") @NS.route('') class Sample(Resource): def get(self): a = {'a': 1, 'b': 2} return a def post(self): print('hi')
from flask import request from server.api import get_token, verify_request from flask_restplus import Resource, fields from server.app import api, db from server.models.Users import Users from server.models.LanguagesKnown import LanguagesKnown from server.models.LanguagesWanted import LanguagesWanted from server.models.Languages import Languages from server.app import mdb NS = api.namespace('register', description='Endpoint for registering a new user.') token = api.model('Token', { 'token': fields.String, 'user_id': fields.Integer, 'user_name': fields.String, 'email': fields.String, 'pref_timezone': fields.String, 'learning_id': fields.Integer, 'learning_language': fields.String, 'native_id': fields.Integer, 'native_language': fields.String, 'message': fields.String, 'authenticated': fields.Boolean }) form_input = api.model('Register form', { 'user_name': fields.String, 'email': fields.String, 'password': fields.String,
from flask import request from flask_restplus import Resource, fields from server.app import api, db from server.models.Requests import Requests from server.api import get_token, token_required, verify_request from server.models.Requests import Requests from server.models.Users import Users from server.api.get_user import USER_MODEL from server.helpers.user import get_user from server.app import mdb import datetime NS = api.namespace( 'request', description="Make a request to chat with someone") request_form = api.model('Request form', { 'partner_id': fields.Integer, 'date': fields.String, 'from_time': fields.Integer, 'to_time': fields.Integer }) success_resp = api.model('Successful request', { 'id': fields.String }) GET_REQUEST_RESPONSE = api.model('Get request response', { 'request_id': fields.Integer, 'other_user': fields.Nested(USER_MODEL), 'from_time': fields.String,
from server.app import mdb from flask import request from flask_restplus import Resource, fields from server.api import token_required, verify_request from server.app import api, db from server.models.Requests import Requests from server.models.Users import Users from pprint import pprint from bson import ObjectId NS = api.namespace('notifications', description='View notifications') READ_PATCH_FORM = { 'id': fields.String # ID of notification to mark as read } READ_PATCH_MODEL = api.model('Read notification model', READ_PATCH_FORM) def mongo_encode(doc): enc_doc = {} for key in doc: if isinstance(doc[key], ObjectId): enc_doc[key] = str(doc[key]) else: enc_doc[key] = doc[key] return enc_doc @NS.route('')
''' GET language id mapped to language name ''' from flask import request from flask_restplus import Resource, fields from server.api import token_required from server.models.Timezones import Timezones from server.app import api timezone = api.model( 'Timezone', { 'id': fields.Integer, 'timezone_name': fields.String, 'timezone_offset': fields.Integer }) NS = api.namespace( 'get_timezones', description="Get a list of available timezones in Talktime.") @NS.route('') class GetTimezones(Resource): @NS.marshal_list_with(timezone) def get(self): return [t.to_dict() for t in Timezones.query.all()]
from flask_restplus import Resource, fields from server.app import api, db from server.models.Requests import Requests from server.api import get_token, token_required from server.app import mdb from bson import ObjectId request_form = api.model('Accept request model', { 'request_id': fields.String }) accept_response = api.model('Accept request response', { 'message': fields.String }) NS = api.namespace('accept_request', description='Accept a chat request') @NS.route('') class AcceptRequest(Resource): #What you post @NS.expect(request_form) #What you return @NS.marshal_with(accept_response) #Checking if the user is registered @token_required def post(self, cur_user): # If cur_user is not authenticated, decorator return to the client without entering this function accept_request = request.get_json() if accept_request == None: accept_request = request.form
'''Contains class for logging in''' from flask import request from flask_restplus import Resource, fields from server.app import app, api from server.models.Users import Users from server.api import get_token NS = api.namespace("login", description="Login an existing user") # self.user_name = user_name # self.email = email # self.password_hash = generate_password_hash(password, method='sha256') # self.pref_timezone = pref_timezone token = api.model( 'Token', { 'token': fields.String, 'user_id': fields.String, 'user_name': fields.String, 'email': fields.String, 'pref_timezone': fields.String, 'learning_id': fields.Integer, 'learning_language': fields.String, 'native_id': fields.Integer, 'native_language': fields.String, 'message': fields.String, 'authenticated': fields.Boolean }) form = api.model('Login form', { 'user_name': fields.String,
''' POST user id << do we need this? we have the user via @token_required Match with other users who have the same timezone and language. Return a list of users, and count ''' from flask import request from flask_restplus import Resource, fields from server.app import api, db from server.models.Users import Users from server.api import token_required NS = api.namespace( "search_timezone", description="Query for users with same preferred timezone") username = api.model('username', { 'username': fields.String }) response = api.model('Users', { 'usernames': fields.List(fields.Nested(username)) }) @NS.route("") class SearchTimezone(Resource): '''Contains post method for logging in''' @api.marshal_with(response) @token_required def get(self, curr_user):
Match with other users who have the same timezone and language. Return a list of users, and count ''' from flask import request from flask_restplus import Resource, fields from server.app import api, db from server.models.Availabilities import Availabilities from server.models.Users import Users from server.models.Timezones import Timezones from server.api import token_required NS = api.namespace( "search_availabilities", description="Query for availabilities that overlap with the request") availabilities = api.model( 'availability', { 'id': fields.String, 'user_id': fields.Integer, 'user_name': fields.String, 'day_of_week': fields.Integer, 'from_time': fields.Integer, 'to_time': fields.Integer }) response = api.model( 'availabilities', {'availabilities': fields.List(fields.Nested(availabilities))})
''' GET language id mapped to language name ''' from flask import request from flask_restplus import Resource, fields from server.api import token_required from server.models.Languages import Languages from server.app import api language = api.model('Language', { 'id': fields.Integer, 'language_name': fields.String }) NS = api.namespace( 'get_languages', description="Get a list of available languages to use on Talktime.") @NS.route('') class GetLanguages(Resource): @NS.marshal_list_with(language) def get(self): return [l.to_dict() for l in Languages.query.all()]