SHARE
TWEET

Untitled

a guest Jun 4th, 2017 514 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, session, render_template
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_admin import Admin
  4. from flask_admin.contrib.sqla import ModelView
  5. from flask_script import Manager
  6. from flask_restless import APIManager
  7. from flask_restful import Resource, fields, marshal_with, Api
  8. from flask_migrate import Migrate, MigrateCommand
  9. from flask_mail import Mail, Message
  10. from sqlalchemy import create_engine
  11. from sqlalchemy.orm import sessionmaker
  12. from random import randint
  13. import radar
  14.  
  15. # from celery import Celery
  16.  
  17. from udblab import site, api
  18.  
  19. app = Flask(__name__)
  20. # mail = Mail(app)
  21. app.config['MAIL_SERVER'] = 'smtp.gmail.com'
  22. app.config['MAIL_PORT'] = 465
  23. app.config['MAIL_USERNAME'] = 'nizom7812@gmail.com'
  24. app.config['MAIL_PASSWORD'] = '$$$$$$$'
  25. app.config['MAIL_USE_TLS'] = False
  26. app.config['MAIL_USE_SSL'] = True
  27. mail = Mail(app)
  28.  
  29. api_app = Api(app)
  30. app.secret_key = 's3cr3t'
  31. app.config.from_pyfile('config.py')
  32. # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////var/www/FlaskApp/FlaskApp/udblab/app.sqlite'
  33. # <<<<<<< HEAD
  34. # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///C:\\Users\\Ahatajon\\PycharmProjects\\weather_station\\udblab\\app.sqlite'
  35. # =======
  36. # # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///E:\\Pycharm Projects\\untitled\\udblab\\app.sqlite'
  37. # >>>>>>> d089acf4f8a08088aadae8cc53e51df6fec31bec
  38. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  39.  
  40. engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
  41. Session = sessionmaker(bind=engine, autoflush=False)
  42. session_db = Session()
  43. # app.config['CELERY_BROKER_URL'] = 'redis://127.0.0.1:6379'
  44. # app.config['CELERY_RESULT_BACKEND'] = 'redis://127.0.0.1:6379'
  45.  
  46. # celery = Celery("test", backend='amqp', broker='amqp:/127.0.0.1/')
  47.  
  48.  
  49. # app.config.from_pyfile('config.py')
  50. db = SQLAlchemy(app)
  51. migrate = Migrate(app, db)
  52. manager_app = Manager(app)
  53. manager_app.add_command('db', MigrateCommand)
  54. admin = Admin(app, template_mode='bootstrap3')
  55. from udblab.models import *
  56.  
  57. admin.add_view(ModelView(User, db.session))
  58. admin.add_view(ModelView(Role, db.session))
  59. admin.add_view(ModelView(Location, db.session))
  60. admin.add_view(ModelView(Sensor, db.session))
  61. admin.add_view(ModelView(SensorNode, db.session))
  62. admin.add_view(ModelView(History, db.session))
  63.  
  64. db.create_all()
  65.  
  66. manager = APIManager(app, flask_sqlalchemy_db=db)
  67.  
  68. user_blueprint = manager.create_api(User, include_columns=['username', 'email', 'isAdmin', 'locations'],
  69.                                     methods=['GET'])
  70. location_blueprint = manager.create_api(Location, include_columns=['address'], methods=['GET'])
  71. sensor_blueprint = manager.create_api(Sensor, include_columns=['latitude'])
  72.  
  73. api_app.add_resource(UserApi, '/api/v1/users/<user_id>')
  74. api_app.add_resource(UserListApi, '/api/v1/users')
  75.  
  76. from udblab.api.routers import mod
  77. from udblab.site.routers import mod
  78.  
  79. app.register_blueprint(site.routers.mod)
  80. app.register_blueprint(api.routers.mod, url_prefix='/api/v1')
  81.  
  82. sensor_type_list = ['temperature', 'humidity', 'pressure', 'wind_speed']
  83.  
  84. # sensor_node_count = SensorNode.query.all()
  85. # count = len(sensor_node_count)
  86. #
  87.  
  88. from celery import Celery
  89.  
  90.  
  91. def make_celery(app):
  92.     celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
  93.                     broker=app.config['CELERY_BROKER_URL'])
  94.     celery.conf.update(app.config)
  95.     TaskBase = celery.Task
  96.  
  97.     class ContextTask(TaskBase):
  98.         abstract = True
  99.  
  100.         def __call__(self, *args, **kwargs):
  101.             with app.app_context():
  102.                 return TaskBase.__call__(self, *args, **kwargs)
  103.  
  104.     celery.Task = ContextTask
  105.     return celery
  106.  
  107.  
  108. app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379'
  109. app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379'
  110.  
  111. celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
  112. celery.conf.update(app.config)
  113.  
  114.  
  115. @celery.task()
  116. def add_together(a, b):
  117.     print(a + b)
  118.  
  119.  
  120. def test_user():
  121.     for i in range(5):
  122.         username = 'User_' + str(i + 7)
  123.         password = '12345'
  124.         email = 'user_' + str(i + 7) + '@gmail.com'
  125.         isAdmin = False
  126.         user = User(username=username, password=password, email=email, isAdmin=isAdmin)
  127.         db.session.add(user)
  128.         db.session.commit()
  129.  
  130.  
  131. def test_location():
  132.     for i in range(5):
  133.         address = 'Chungju_street_' + str(i + 1)
  134.         camera_url = 'camera_url_' + str(i + 1)
  135.         owner_id = i + 7
  136.         location = Location(address=address, camera_url=camera_url, owner_id=owner_id)
  137.         db.session.add(location)
  138.         db.session.commit()
  139.  
  140.  
  141. def test_sensor_node():
  142.     for i in range(6):
  143.         for j in range(randint(1, 4)):
  144.             latitude = 36.96504 + i / 10 + j / 10
  145.             longitude = 127.9712 + i / 10 + j / 10
  146.             area_id = i + 6
  147.             sensor_node = SensorNode(latitude=latitude, longitude=longitude, area_id=area_id)
  148.             db.session.add(sensor_node)
  149.     db.session.commit()
  150.  
  151.  
  152. def test_sensor():
  153.     for i in range(5):
  154.         for j in range(4):
  155.             sensor_type = sensor_type_list[j]
  156.             sensor_type_id = j + 1
  157.             sensor_node_id = i + 6
  158.             sensor = Sensor(sensor_type=SensorTypeChecker.sensor_type_checker(sensor_type),
  159.                             sensor_type_id=sensor_type_id,
  160.                             sensor_node_id=sensor_node_id)
  161.             db.session.add(sensor)
  162.     db.session.commit()
  163.  
  164.  
  165. def test_sensor_value():
  166.     for i in range(4):
  167.         for j in range(randint(10, 20)):
  168.             value = randint(1, 60)
  169.             sensor_type = sensor_type_list[i]
  170.             sensor_value = SensorValue(value=value, type=sensor_type, sensor_id=i + 35)
  171.             db.session.add(sensor_value)
  172.     db.session.commit()
  173.  
  174.  
  175. def test_history_data():
  176.     for i in range(10):
  177.         start_time_start = '2017-05-28T17:05:00'
  178.         stop_time_start = '2017-05-28T17:10:00'
  179.         start_time_end = '2017-05-28T17:15:00'
  180.         stop_time_end = '2017-05-28T17:40:00'
  181.         db_start_time = radar.random_date(start_time_start, stop_time_start)
  182.         db_end_time = radar.random_date(start_time_end, stop_time_end)
  183.         calculated_time = (db_end_time - db_start_time).total_seconds()
  184.         history = History(start_time=db_start_time, end_time=db_end_time, calculated_time=str(calculated_time),
  185.                           sensor_node_id=1)
  186.         db.session.add(history)
  187.         db.session.commit()
  188.  
  189.  
  190.         #
  191.  
  192.  
  193. @celery.task()
  194. def send_template():
  195.     msg = Message("Snow Melting", sender='nizom7812@gmail.com', recipients=['nizom7812@gmail.com'])
  196.     msg.body = "Snow Melting"
  197.     msg.html = 'dsjk'
  198.     mail.send(msg)
  199.     print("sent")
  200.  
  201.  
  202. with app.app_context():
  203.     print(datetime.now())
  204.     add_together.delay(2, 3)
  205.     result = add_together.delay(2, 3)
  206.     send_template.delay()
  207.     print(result)
  208.     print(datetime.now())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top