Commit 2f8454c6 authored by Roman Shishkin's avatar Roman Shishkin

Отправка поста через API на сервер

parent 9c280705
......@@ -7,16 +7,19 @@ db = orm.Database()
class BlacklistedDomain(db.Entity):
domain = orm.Required(str)
class Post(db.Entity):
summary = orm.Required(str)
details = orm.Optional(str)
source = orm.Optional(str)
text = orm.Optional(str)
links = orm.Set('PostLink')
photos = orm.Set("Photo")
author = orm.Required("User")
user = orm.Required("User")
state = orm.Required(str, sql_default='pending')
timestamp = orm.Required(datetime, sql_default='CURRENT_TIMESTAMP')
class PostLink(db.Entity):
link = orm.Required(str)
post = orm.Required('Post')
class Photo(db.Entity):
user = orm.Required("User")
url = orm.Required(str)
......
......@@ -4,8 +4,8 @@ import json
from flask import request
from pony import orm
from ..root import check_user
from ...models import Post
from ...models import Post, PostLink, Photo
from ...utils import get_user_from_db
def init(app):
@app.route('/api/posts', methods=['GET'])
......@@ -32,28 +32,29 @@ def init(app):
request_json = request.get_json()
app_id = app.config['APP_ID']
app_secret = app.config['APP_SECRET']
viewer_id = request_json.get('viewer_id', None)
hash = request_json.get('hash', None)
if not check_user(app_id, viewer_id, app_secret, hash):
user_id = request_json.get('user_id', None)
user_key = request_json.get('user_key', None)
links = request_json.get('links', None)
print(links)
if not check_user(app_id, user_id, app_secret, user_key):
return json.dumps({
'status': 'error',
'error': {
'type': 'unauthorized_access_attempt',
'message': 'please provide VK user credentials',
}
'error': True,
'message': 'please provide VK user credentials'
})
post = Post(summary=request_json('summary', None),
details=request_json.get('details', None),
source=request_json.get('source', None),
photo=request_json.get('photo', None),
video=request_json.get('video', None),
latitude=request_json.get('latitude', None),
longitude=request_json.get('longitude', None),
author=viewer_id,
user = get_user_from_db(user_id)
post = Post(summary=request_json.get('summary', None),
text=request_json.get('text', ''),
user=user,
state='pending')
for link in links:
if link is not '':
# TODO check for the blacklist
lnk = PostLink(link=link, post=post)
orm.commit()
return json.dumps({
'status': 'ok',
'error': False,
'message': 'Ваша новость была успешно отправлена',
'data': {
'id': post.id,
},
......
class CreateController {
constructor($scope, $location, $http) {
constructor($scope, $http) {
$scope.flash_info = false;
$scope.flash_danger = false;
$scope.link = ["", "", "", "", ""];
$scope.contents = [];
$scope.files = [];
let number = 1;
let max = 4;
let value = "Дополнительное поле для ссылки";
......@@ -18,11 +22,44 @@ class CreateController {
};
$scope.send = () => {
alert();
let data = {
summary: $scope.summary,
links: $scope.link,
text: $scope.text,
files: $scope.files,
user_id: user_id,
user_key: user_key
};
$http.post('/api/posts', data).then(function successCallback(response) {
$scope.summary = '';
$scope.link[0] = '';
$scope.link[1] = '';
$scope.link[2] = '';
$scope.link[3] = '';
$scope.link[4] = '';
$scope.text = '';
if (response.data.error === false) {
$scope.flash_danger = false;
$scope.flash_text = response.data.message;
$scope.flash_info = true;
} else {
$scope.flash_info = false;
$scope.flash_text = response.data.message;
$scope.flash_danger = true;
}
}, function errorCallback(response) {
$scope.flash_info = false;
$scope.flash_text = 'Произошла ошибка при отправке запроса';
$scope.flash_danger = true;
});
}
}
}
CreateController.$inject = ['$scope', '$location'];
CreateController.$inject = ['$scope', '$http'];
export default CreateController;
\ No newline at end of file
class PhotosUploadController {
constructor($scope, FileUploader, $uibModal) {
constructor($scope, FileUploader) {
$scope.uploader = new FileUploader();
$scope.uploader.url = '/upload/'+user_id+'/'+user_key;
$scope.uploader.autoUpload = true;
$scope.files = []; // List of urls
$scope.uploader.onSuccessItem = (item, response, status, headers) => {
if(typeof response.error === "undefined") {
// Successful upload
......@@ -21,6 +20,6 @@ class PhotosUploadController {
}
}
PhotosUploadController.$inject = ['$scope', 'FileUploader', '$uibModal'];
PhotosUploadController.$inject = ['$scope', 'FileUploader'];
export default PhotosUploadController;
\ No newline at end of file
......@@ -3,8 +3,8 @@
<img class="header__image" src="/static/images/true_logo.png" alt="ЛЕНТАЧ">
<h1>Предложи нам новость!</h1>
</header>
<div class="alert alert-info">{{flash_text}}</div>
<div class="alert alert-danger">{{flash_text}}</div>
<div class="alert alert-info" ng-show="flash_info">{{flash_text}}</div>
<div class="alert alert-danger" ng-show="flash_danger">{{flash_text}}</div>
<form ng-submit="send()">
<div class="input-group">
<label for="summary">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment