def insert_serv(self, name_service, doc_serv, date_get, period, cost_unit, unit, peny_data, peny): from database import User, Service, select_obj import datetime self.name_services = name_service self.doc_servs = doc_serv self.date_gets = date_get self.periods = period self.cost_units = cost_unit self.units = unit self.peny_dates = peny_data self.penys = peny self.ids = select_obj(Service.id, Service.name_service, self.name_services, None, None) if len(self.ids) != 0: self.ids1 = self.ids[len(self.ids) - 1] else: self.ids1 = -1 self.b = Service(id=self.ids1 + 1, date=datetime.datetime.now(), name_service=self.name_services, doc_serv=self.doc_servs, date_get=self.__date(self.date_gets), period=int(self.periods), cost_unit=float(self.cost_units.replace(",", ".")), unit=self.units, peny_data=self.__date(self.peny_dates), peny=int(self.penys)) return session.add(self.b)
def admin_api(function): def listofshame(): return [{ 'name': u.name, 'balance': u.balance, 'score': u.score, 'id': str(u.id), 'switch_url': url_for('coffee.administrate_switch_user', username=u.username), 'vip': u.vip, 'last_service': getattr(u.last_service, 'date', None), } for u in User.objects(active=True)] def next_service_periods(): latestDate = ( Service.objects.order_by('-date').first().date.timestamp() if Service.objects else pendulum.now().timestamp()) latest_service = pendulum.from_timestamp(latestDate) periods = [] for _ in range(8): nmo = latest_service.next(pendulum.MONDAY) nfr = nmo.next(pendulum.FRIDAY) periods.append('%s:%s' % (nmo.to_date_string(), nfr.to_date_string())) latest_service = nfr return periods if function == 'listofshame': return jsonify(list=listofshame(), nextServicePeriods=next_service_periods()) if function == 'add_service': data = request.get_json() user = User.objects.get(id=data.get('uid')) start, end = [ pendulum.parse(d) for d in data.get('interval').split(':') ] for day in pendulum.period(start, end): Service(user=user, date=day).save() return jsonify(list=listofshame(), nextServicePeriods=next_service_periods())
def admin_api(function): def listofshame(): return [{ 'name': u.name, 'balance': u.balance, 'score': u.score, 'id': str(u.id), 'switch_url': url_for('coffee.administrate_switch_user', username=u.username), 'vip': u.vip, 'last_service': getattr(u.last_service, 'date', None), } for u in User.objects(active=True)] def next_service_periods(): """ Return a list of date perios where no master service is defined """ # get all upcoming services upcomingServices = list( Service.objects.aggregate( { '$match': { 'date': { '$gte': pendulum.today() }, 'master': True, } }, { '$group': { '_id': { '$dateToString': { # group by Year-Week 'format': '%Y%U', 'date': '$date' } } } })) # also get upcoming 8 weeks if no service is set for this week upcoming_weeks = [] nextMonday = pendulum.today().next(pendulum.MONDAY) for weekDelta in range(8): nextStartDate = nextMonday.add(weeks=weekDelta) if nextStartDate.format('%Y%U') not in [ s['_id'] for s in upcomingServices ]: nextEndDate = nextStartDate.next(pendulum.FRIDAY) upcoming_weeks.append( f'{nextStartDate.to_date_string()}:{nextEndDate.to_date_string()}' ) return upcoming_weeks if function == 'listofshame': return jsonify(list=listofshame(), nextServicePeriods=next_service_periods()) if function == 'add_service': data = request.get_json() user = User.objects.get(id=data.get('uid')) start, end = [ pendulum.parse(d) for d in data.get('interval').split(':') ] for day in pendulum.period(start, end): Service(user=user, date=day).save() return jsonify(list=listofshame(), nextServicePeriods=next_service_periods()) return abort(404)