def put(self, problem_id): """Update a problem in the database { "status": "SOLVED", "severity": "3", "title": "problem_14", "problem_type_id": 3, "content": "problem_test", "proposal": "test_proposal", "region_id": 1, "latitude": 4, "longitude":4 } """ args = self.request.arguments x = args.pop('latitude') y = args.pop('longitude') args['location'] = create_location(x, y) self.sess.query(Problem).filter_by(id=int(problem_id)). \ update(args) self.sess.commit() activity = ProblemsActivity( problem_id=int(problem_id), user_id=self.get_current_user(), datetime=get_datetime(), activity_type="UPDATED" ) self.sess.add(activity) self.sess.commit()
def delete(self, problem_id): """Delete a problem from the database by given problem id.""" activity = ProblemsActivity( problem_id=int(problem_id), user_id=self.get_current_user(), datetime=get_datetime(), activity_type='REMOVED') self.sess.add(activity) self.sess.commit()
def store_photo_data_to_db(problem_id, filename, handler): photo = models.Photo( problem_id=problem_id, name=filename, datetime=utils.get_datetime(), user_id=handler.current_user, comment=handler.request.body_arguments['comments'].pop().decode( 'utf-8') ) handler.sess.add(photo) handler.sess.commit()
class ProblemActivityFactory(SQLAlchemyModelFactory): """Creates a new problem, user and record in problem_activities table tagged 'ADDED'.""" class Meta: model = ProblemsActivity sqlalchemy_session = cm.Session problem = None user = None datetime = get_datetime() activity_type = None
class CommentFactory(SQLAlchemyModelFactory): class Meta: model = models.Comment sqlalchemy_session = cm.Session exclude = ('now',) # this attribute helps to define others, it is not passed to __init__ now = datetime.datetime.strptime( utils.get_datetime(), "%Y-%m-%d %H:%M:%S") content = factory.sequence(lambda n: 'comment_%s_content' % n) created_date = now - datetime.timedelta(days=2) modified_date = now - datetime.timedelta(minutes=50) modified_user_id = factory.sequence(lambda n: n + 1) user = None problem = None
def post(self, problem_id): """Creates a vote record for the specified problem.""" if check_vote(self, problem_id): return self.send_error(400, message=( 'You can not vote twice!')) vote = ProblemsActivity( problem_id=int(problem_id), user_id=self.current_user, datetime=get_datetime(), activity_type='VOTE' ) self.sess.add(vote) self.sess.commit()
def post(self): """Store a new problem to the database. { "status": "SOLVED", "severity": "3", "title": "problem_14", "problem_type_id": 3, "content": "problem_test", "proposal": "test_proposal", "region_id": 1, "latitude": 4, "longitude":4 }""" arguments = self.request.arguments print arguments x = arguments['latitude'] y = arguments['longitude'] problem = Problem( title=arguments['title'], content=define_values(arguments,'content'), proposal=define_values(arguments,'proposal'), severity=define_values(arguments,'severity', '1'), status=define_values(arguments,'status','UNSOLVED'), location=create_location(x, y), problem_type_id=arguments['problem_type_id'], region_id=define_values(arguments,'region_id')) self.sess.add(problem) self.sess.commit() activity = ProblemsActivity( problem_id=problem.id, user_id=self.get_current_user(), datetime=get_datetime(), activity_type="ADDED") self.sess.add(activity) self.sess.commit() if self.get_status() is 200: self.write({'id': problem.id})