def process(comment): user_fields = [ 'id', 'username', 'email', 'name', 'surname', 'gender', 'phonenumber', 'avatar', 'birthday', 'role' ] issue_fields = [ 'id', 'title', 'description', 'state', 'date', 'pollution_category', 'pollution_rating', 'budget' ] d = to_dict(comment, [i for i in fields if i in comment_fields]) if 'datetime' in d: d['datetime'] = str(d['datetime']) if 'author' in fields: d['author'] = to_dict(comment.author, user_fields) d['author']['birthday'] = str(d['author']['birthday']) d['author']['role'] = d['author']['role'].role if 'origin' in fields: d['origin'] = to_dict(comment.issue, issue_fields) d['origin']['state'] = d['origin']['state'].state d['origin']['date'] = str(d['origin']['date']) d['origin']['pollution_category'] = d['origin'][ 'pollution_category'].category d['origin']['comments'] = len(comment.issue.comments) if d['origin']['budget'] is not None: d['origin']['budget'] = float(d['origin']['budget']) if 'location' in fields: point = wkb.loads(bytes(comment.issue.location.data)) d['location'] = {'x': point.x, 'y': point.y} return d
def info(self, id, fields): session = self.session_maker() issue_fields = [ 'id', 'title', 'description', 'date', 'pollution_rating', 'budget' ] user_fields = [ 'id', 'username', 'email', 'name', 'surname', 'gender', 'phonenumber', 'avatar', 'birthday', 'role' ] issue = session.query(Issue) \ .filter(Issue.id == id) \ .one_or_none() if issue is None: return {} issue_dict = to_dict(issue, [i for i in fields if i in issue_fields]) if 'budget' in issue_dict and issue_dict['budget'] is not None: issue_dict['budget'] = float(issue_dict['budget']) if 'comments' in fields: issue_dict['comments'] = session.query(Comment) \ .filter(Issue.id == id) \ .count() if 'state' in fields: issue_dict['state'] = issue.state.state if 'creator' in fields: issue_dict['creator'] = to_dict(issue.creator, user_fields) issue_dict['creator']['birthday'] = str( issue_dict['creator']['birthday']) issue_dict['creator']['role'] = issue_dict['creator']['role'].role if 'approver' in fields and issue.approver: issue_dict['approver'] = to_dict(issue.approver, user_fields) issue_dict['approver']['birthday'] = str( issue_dict['approver']['birthday']) issue_dict['approver']['role'] = issue_dict['approver'][ 'role'].role if 'pollution_category' in fields: issue_dict[ 'pollution_category'] = issue.pollution_category.category if 'location' in fields: point = wkb.loads(bytes(issue.location.data)) issue_dict['location'] = {'x': point.x, 'y': point.y} if 'date' in fields: issue_dict['date'] = str(issue_dict['date']) if 'photos' in fields: photos = session.query(Photo) \ .filter(Photo.issue_id == id) \ .all() issue_dict['photos'] = [i.url for i in photos] return issue_dict
def process(issue): issue_dict = to_dict(issue, [i for i in fields if i in issue_fields]) if 'budget' in issue_dict and issue_dict['budget'] is not None: issue_dict['budget'] = float(issue_dict['budget']) if 'comments' in fields: issue_dict['comments'] = session.query(Comment) \ .filter(Issue.id == id) \ .count() if 'state' in fields: issue_dict['state'] = issue.state.state if 'pollution_category' in fields: issue_dict[ 'pollution_category'] = issue.pollution_category.category if 'location' in fields: point = wkb.loads(bytes(issue.location.data)) issue_dict['location'] = {'x': point.x, 'y': point.y} if 'date' in fields: issue_dict['date'] = str(issue_dict['date']) if 'photos' in fields: photos = session.query(Photo) \ .filter(Photo.issue_id == id) \ .all() issue_dict['photos'] = [i.url for i in photos]
def create_issue(self, data): session = self.session_maker() try: pollution_category_id = session.query(PollutionCategory).filter( PollutionCategory.category == data.get( 'pollution_category')).one_or_none().id data['pollution_category'] = pollution_category_id unapproved_issue_id = session.query(IssueState).filter( IssueState.state == 'new').one_or_none().id data['state_id'] = unapproved_issue_id new_issue = Issue(data) session.add(new_issue) session.commit() issue_fields = [ 'id', 'title', 'description', 'location', 'date', 'pollution_rating', 'pollution_category', 'creator' ] result = to_dict(new_issue, [i for i in issue_fields]) point = wkb.loads(bytes(new_issue.location.data)) result['location'] = {'x': point.x, 'y': point.y} result[ 'pollution_category'] = new_issue.pollution_category.category result['creator'] = new_issue.creator.username return (result, True) except Exception as e: return (e.args, False)
def unapproved_issues(self): session = self.session_maker() issue_fields = [ 'id', 'title', 'description', 'location', 'date', 'pollution_rating', 'pollution_category', 'creator' ] unapproved_issue_id = session.query(IssueState) \ .filter(IssueState.state=='new').one_or_none().id issues = session.query(Issue) \ .filter(Issue.state_id == unapproved_issue_id).all() issues_dict = [] for issue in issues: item = to_dict(issue, [i for i in issue_fields]) point = wkb.loads(bytes(issue.location.data)) item['location'] = {'x': point.x, 'y': point.y} item['pollution_category'] = issue.pollution_category.category item['creator'] = issue.creator.username issues_dict.append(item) if issues is None: return else: return issues_dict
def info(self, id, fields): session = self.session_maker() event_fields = ['id', 'datetime'] issue_fields = [ 'id', 'title', 'description', 'state', 'date', 'pollution_category', 'pollution_rating', 'budget', 'comments' ] user_fields = [ 'id', 'username', 'email', 'name', 'surname', 'gender', 'phonenumber', 'avatar', 'birthday', 'role' ] event = session.query(Event) \ .filter(Event.id == id) \ .one_or_none() if event is None: return {} event_dict = to_dict(event, [i for i in fields if i in event_fields]) if 'datetime' in event_dict: event_dict['datetime'] = str(event_dict['datetime']) if 'comments' in fields: event_dict['comments'] = len(event.comments) if 'participants' in fields: event_dict['participants'] = len(event.participants) if 'issue' in fields: event_dict['issue'] = to_dict(event.issue, issue_fields) event_dict['issue']['state'] = event_dict['issue']['state'].state event_dict['issue']['date'] = str(event_dict['issue']['date']) event_dict['issue']['pollution_category'] = event_dict['issue']['pollution_category'].category event_dict['issue']['comments'] = len(event_dict['issue']['comments']) if event_dict['issue']['budget'] is not None: event_dict['issue']['budget'] = float(event_dict['issue']['budget']) if 'creator' in fields: event_dict['creator'] = to_dict(event.creator, user_fields) event_dict['creator']['birthday'] = str(event_dict['creator']['birthday']) event_dict['creator']['role'] = event_dict['creator']['role'].role if 'photos' in fields: photos = session.query(Photo) \ .filter(Photo.event_id == id) \ .all() event_dict['photos'] = [i.url for i in photos] return event_dict
def process(comment): user_fields = [ 'id', 'username', 'email', 'name', 'surname', 'gender', 'phonenumber', 'avatar', 'birthday', 'role' ] event_fields = ['id', 'datetime', 'comments', 'participants'] d = to_dict(comment, [i for i in fields if i in comment_fields]) if 'datetime' in d: d['datetime'] = str(d['datetime']) if 'author' in fields: d['author'] = to_dict(comment.author, user_fields) d['author']['birthday'] = str(d['author']['birthday']) d['author']['role'] = d['author']['role'].role if 'origin' in fields: d['origin'] = to_dict(comment.event, event_fields) d['origin']['datetime'] = str(d['origin']['datetime']) d['origin']['comments'] = len(d['origin']['comments']) d['origin']['participants'] = len(d['origin']['participants']) return d
def process(event): event_fields = ['id', 'datetime'] event_dict = to_dict(event, [i for i in fields if i in event_fields]) if 'datetime' in event_dict: event_dict['datetime'] = str(event_dict['datetime']) if 'comments' in fields: event_dict['comments'] = len(event.comments) if 'participants' in fields: event_dict['participants'] = len(event.participants) if 'issue' in fields: event_dict['issue'] = to_dict(event.issue, issue_fields) event_dict['issue']['state'] = event_dict['issue']['state'].state event_dict['issue']['date'] = str(event_dict['issue']['date']) event_dict['issue']['pollution_category'] = event_dict['issue']['pollution_category'].category event_dict['issue']['comments'] = len(event_dict['issue']['comments']) if event_dict['issue']['budget'] is not None: event_dict['issue']['budget'] = float(event_dict['issue']['budget']) if 'photos' in fields: photos = session.query(Photo) \ .filter(Photo.event_id == id) \ .all() event_dict['photos'] = [i.url for i in photos]
def create_event(self,data): session = self.session_maker() try: user = session.query(User).filter(User.id == data['user_id']) if user.role.role != 'admin': return {'Error': 'User is not admin'}, False if datetime.datetime.strptime(data['datetime'],"%Y-%m-%d %H:%M") < datetime.datetime.now(): return {'Error': 'Invalid date'}, False new_event = Event(data) session.add(new_event) session.commit() event_fields = ['id','title','description','issue_id','creator_id','datetime'] result = to_dict(new_event, [i for i in event_fields]) return result, True except Exception as e: return e.args, False
def create_event(self, data): session = self.session_maker() try: if datetime.datetime.strptime( data['datetime'], "%Y-%m-%d %H:%M") < datetime.datetime.now(): return 'Invalid date' new_event = Event(data) session.add(new_event) session.commit() event_fields = [ 'id', 'title', 'description', 'issue_id', 'creator_id', 'datetime' ] result = to_dict(new_event, [i for i in event_fields]) return (result, True) except Exception as e: return (e.args, False)