def db_init(): db.connect() if not User.table_exists(): User.create_table() if not Logs.table_exists(): Logs.create_table() if not Shortening.table_exists(): Shortening.create_table()
def get(self, short_url): query = Shortening.select().where(Shortening.short_url == short_url) if query.exists(): shortening = query.get() l = Logs(user=shortening.user, shortening=shortening, ip=self.request.remote_ip) l.save() full_short_url = BASE_URL+"/"+shortening.short_url data = json.dumps({'short_url': full_short_url, 'long_url': shortening.long_url}) else: self.set_status(400) data = json.dumps({'error': 'Unable to found given url'}) self.set_header('Content-Type', 'application/json') self.write(data)
def add_to_log(game, field, user, id_user, field_opponent, opponent): """add note to collection Logs Arguments: - `game`: - `field`: - `user`: """ note = Logs(game=game, snapshot=field, snapshot_opponent=field_opponent, move_user=user, move_user_id = id_user, opponent=opponent, time=datetime.datetime.now()) note.save(cascade=True)
def get_list_archive_game(firstdate, seconddate): """ returh list archive games - return dictionary in format: { 'date' : date, 'players': user VS user } - `firstdate`: date from first field on archive.html - `seconddate`: date from second field on archive.html """ if firstdate == '' and seconddate == '': games = Games.objects(status=2) elif seconddate == '' or firstdate == '': try: if firstdate and len(firstdate) == 10: year = int(firstdate[-4:]) month = int(firstdate[3:5]) day = int(firstdate[:2]) dt = datetime.datetime(year, month, day) games = Games.objects(status=2, time_begin__gte=dt) elif seconddate and len(seconddate) == 10: year = int(seconddate[-4:]) month = int(seconddate[3:5]) day = int(seconddate[:2]) dt = datetime.datetime(year, month, day) games = Games.objects(status=2, time_begin__gte=dt) else: return False except: return False else: if len(firstdate) != 10 and len(seconddate) != 10: return False try: year_f = int(firstdate[-4:]) month_f = int(firstdate[3:5]) day_f = int(firstdate[:2]) dt_first = datetime.datetime(year_f, month_f, day_f) year_s = int(seconddate[-4:]) month_s = int(seconddate[3:5]) day_s = int(seconddate[:2]) dt_second = datetime.datetime(year_s, month_s, day_s) if dt_first < dt_second: dt_second = datetime.datetime(year_s, month_s, day_s, 23, 59, 59) games = Games.objects( Q(status=2) & (Q(time_begin__gte=dt_first) & Q(time_begin__lte=dt_second))) else: dt_first = datetime.datetime(year_f, month_f, day_f, 23, 59, 59) games = Games.objects( Q(status=2) & (Q(time_begin__lte=dt_first) & Q(time_begin__gte=dt_second))) except: return False if games: dict_game = {} for game in games: note = Logs.objects(game=game)[0] dict_game[str(note.game.id)] = { 'date' : note.game.time_begin.strftime('%d-%m-%Y'), 'players': note.move_user + ' VS ' + note.opponent } return dict_game return True
def get(self, short_url): token = self.get_secure_cookie("token") query = Shortening.select().where(Shortening.short_url == short_url) if query.exists(): shortening = query.get().id logs = list(Logs.select().where((Logs.user == token) & (Logs.shortening == shortening)).dicts()) data = json.dumps(logs, default=json_serial) else: self.set_status(400) data = json.dumps({'error': 'Unable to found given url'}) self.set_header('Content-Type', 'application/json') self.write(data)
def get_all_moves(id_game): """return list moves for game move Arguments: - `id_game`: id game """ game = Games.objects.get(id=id_game) notes = Logs.objects(game=game).order_by('time') moves = {} count = 1 for note in notes: moves[count] = str(note.id) count += 1 return moves
def get(self, short_url): token = self.get_secure_cookie("token") query = Shortening.select().where(Shortening.short_url == short_url) if query.exists(): shortening = query.get() logs = list(Logs.select().where((Logs.user == token) & (Logs.shortening == shortening.id)).dicts()) shortening_dict = model_to_dict(shortening) shortening_dict['short_url'] = BASE_URL+"/"+shortening.short_url shortening_dict['access_count'] = len(logs) data = json.dumps(shortening_dict, default=json_serial) else: self.set_status(400) data = json.dumps({'error': 'Unable to found given url'}) self.set_header('Content-Type', 'application/json') self.write(data)
def get_info_battle(game_id): """return dict: { 'user_field': field, 'opponent_field': field , 'username': user_name , 'opponentname': user_name , 'game_status': status , 'time_begin': time_begin } Arguments: - `game_id`: id game """ game = Games.objects.get(id=game_id) try: note = Logs.objects(game=game).order_by('-time')[0] except: return False return get_info(note)
def stalllist(): user = current_user lgdate = str(now) msg = user.username + " viewed the stalls " logmessage = Logs(details=msg, log_date=lgdate) dbase.session.add(logmessage) dbase.session.commit() x1 = [] result_stall = Stalls.query.order_by(Stalls.stall_no).all() for r in result_stall: tayp = Types.query.filter_by(typeID=r.typeID).first() x1.append(tayp.stall_type) return render_template('showstalls.html', page_title='Stalls', result_stall=result_stall, x1=x1, result=search_panel(0)[0], x=search_panel(0)[1], result2=search_panel(1)[0], y=search_panel(1)[1], notif_count=notif_count())
def tenantslist(): user = current_user lgdate = str(now) msg = user.username + " viewed the tenant list " logmessage = Logs(details=msg, log_date=lgdate) dbase.session.add(logmessage) dbase.session.commit() x1 = [] result_tenants = Tenants.query.order_by( Tenants.first_name).all() #.paginate(1,2,True) for r in result_tenants: stall = Stalls.query.filter_by(stallID=r.stallID).first() x1.append(stall.stall_no) return render_template('showtenants.html', result_tenants=result_tenants, x1=x1, result=search_panel(0)[0], x=search_panel(0)[1], result2=search_panel(1)[0], y=search_panel(1)[1])
def review_movie(movie_id): """rate a movie from 1-5 stars (add more later)""" new_review = Review() data = json.loads(request.data) if not User.check_session(data.get('session_id')): log = Logs('review_movie', dumps({'error': 'must be logged in to review'}), 400) log.create() return make_response(dumps({'error': 'must be logged in to review'}), 400) new_review.tmdb_id = movie_id new_review.user_id = data.get('user_id') new_review.user_name = data.get('user_name') new_review.rating = data.get('rating') new_review.description = data.get('description') new_review.movie_title = data.get('movie_title') results, response_code = new_review.create() log = Logs('review_movie', dumps(results), response_code) log.create() return make_response(dumps(results), response_code)
def AddTenants(): form = addtenants() availstalls_y = [] availstalls = Stalls.query.filter_by( stall_status='0').all() #.paginate(1,11,True) for r in availstalls: type = Types.query.filter_by(typeID=r.typeID).first() availstalls_y.append(type.stall_type) x = [] result = Tenants.query.order_by( Tenants.first_name).all() #.paginate(1,2,True) for r in result: stall = Stalls.query.filter_by(stallID=r.stallID).first() x.append(stall.stall_no) if request.method == "POST": print "scull" if form.validate_on_submit(): print "hello1" firstname = form.fname.data middlename = form.mname.data lastname = form.lname.data Address = form.address.data Contnum = form.contnum.data TenantphotoID = form.tenantphotoID.data stallno1 = form.stallno.data stallnum = Stalls.query.filter_by(stall_no=stallno1).first() if stallnum: print stallnum.stallID print "hello2" sid__ = stallnum.stallID if stallnum.stall_status == "1": flash("stall already Occupied") else: tenantForm = Tenants(contact_no=Contnum, first_name=firstname, mid_name=middlename, last_name=lastname, present_addr=Address, stallID=sid__) dbase.session.add(tenantForm) dbase.session.commit() stallnumm = Stalls.query.filter_by( stallID=stallnum.stallID).first() stallnumm.stall_rate = form.rate.data stallnumm.stall_status = "1" print stallnumm.stallID dbase.session.add(stallnumm) dbase.session.commit() #else: # tyy = Tenants.query.filter_by(tenantID=storer).first() # tyy.tenant_photo = loc_default #return render_template("addtenant.html", form1=form, availstalls = availstalls, availstalls_y=availstalls_y, result=search_panel(0)[0], x=search_panel(0)[1], result2=search_panel(1)[0], y=search_panel(1)[1]) profile_entry = "" te = Tenants.query.all() tee = [] for et in te: tee.append(et.tenantID) storer = tee[len(tee) - 1] print storer TenantphotoID = img_folder + str(storer) rents = Rents(date_started=now, tenantID=storer, stallID=sid__) dbase.session.add(rents) dbase.session.commit() if os.path.isdir(TenantphotoID) == False: os.makedirs(TenantphotoID) if form.tenantphotoID.data == None or form.tenantphotoID.data == "": #tenants.tenant_photo = tenants.tenant_photo tnore = Tenants.query.filter_by( tenantID=storer).first() tnore.tenant_photo = loc_default dbase.session.add(tnore) dbase.session.commit() else: if form.tenantphotoID.data and allowed_file( form.tenantphotoID.data.filename): filename = secure_filename( form.tenantphotoID.data.filename) form.tenantphotoID.data.save( os.path.join(TenantphotoID + '/', filename)) uploadFolder = TenantphotoID + '/' nameNew = str(int(time.time())) + '.' + str( os.path.splitext(filename)[1][1:]) os.rename(uploadFolder + filename, uploadFolder + nameNew) profile_entry = uploadFolder + nameNew t = Tenants.query.filter_by( tenantID=storer).first() #t.tenant_photo = profile_entry t.tenant_photo = 'static/profile/' + str( storer) + '/' + nameNew print profile_entry dbase.session.add(t) user = current_user lgdate = str(now) msg = user.username + " added a tenant " logmessage = Logs(details=msg, log_date=lgdate) dbase.session.add(logmessage) dbase.session.commit() flash('Tenant added') #return render_template("successadd1.html", result=search_panel(0)[0], x=search_panel(0)[1], result2=search_panel(1)[0], y=search_panel(1)[1]) else: flash("Stall not found") return render_template("addtenant.html", form1=form, availstalls=availstalls, availstalls_y=availstalls_y, result=search_panel(0)[0], x=search_panel(0)[1], result2=search_panel(1)[0], y=search_panel(1)[1])
def prod_users(): """ Send users prods (movie recommendations) Return data contains a dict from receiver id to result """ data = json.loads(request.data) if not User.check_session(data.get('session_id')): log = Logs('prod_users', dumps({'error': 'must be logged in to prod'}), 400) log.create() return make_response(dumps({'error': 'must be logged in to prod'}), 400) receivers = data.get('receivers') sender = data.get('sender') tmdb_id = data.get('tmdb_id') message = data.get('message') if not receivers or not sender or not tmdb_id: log = Logs('prod_users', \ dumps({'error': 'sender, receiver, and tmdb id required for prod'}), 400) log.create() return make_response(dumps({'error': 'sender, receiver, and tmdb id required for prod'}), \ 400) results = {} for recv in receivers: new_prod = Prod(sender, recv, tmdb_id, message) result, rc = new_prod.create() results[recv] = result log = Logs('prod_users', dumps(results), 200) log.create() return make_response(dumps(results), 200)
def make_admin(): """make another user an admin""" data = json.loads(request.data) user_id = data.get('user_id') session_id = data.get('session_id') if not User.check_session(data.get('session_id')): log = Logs('make_admin', dumps({'error': 'must be logged in to make a user admin'}), 400) log.create() return make_response( dumps({'error': 'must be logged in to make a user admin'}), 400) if not user_id: log = Logs('make_admin', dumps({"error": "user id is required"}), 400) log.create() return make_response(dumps({"error": "user id is required"}), 400) current_user, _status = User.get_user_data_from_session(session_id) if not current_user.get('isAdmin'): log = Logs( 'make_admin', dumps({"error": "you do not have permission to make admin"}), 401) log.create() return make_response( dumps({"error": "you do not have permission to make admin"}), 401) result, response_status = User.make_admin(user_id) log = Logs('make_admin', dumps(result), response_status) log.create() return make_response(dumps(result), response_status)
def insert_logs_to_table(self,line,session): data = self.parse_line(line) log=Logs(**data) session.add(log) session.commit()
def log(message): date = datetime.datetime.now() Logs.create(message=message, date=date, user=1) print("%s %s" % (date, message))
def get(self): token = self.get_secure_cookie("token") logs = list(Logs.select().where(Logs.user == token).dicts()) data = json.dumps(logs, default=json_serial) self.set_header('Content-Type', 'application/json') self.write(data)
def cria_user_com_tupla(tupla): return Logs(tupla[1], tupla[2], tupla[3],tupla[4], tupla[5])
def delete_user(): """delete a user""" data = json.loads(request.data) user_id = data.get('user_id') session_id = data.get('session_id') if not User.check_session(data.get('session_id')): log = Logs('delete_user', dumps({'error': 'must be logged in to delete a user'}), 400) log.create() return make_response( dumps({'error': 'must be logged in to delete a user'}), 400) if not user_id: log = Logs('delete_user', dumps({"error": "user id is required"}), 400) log.create() return make_response(dumps({"error": "user id is required"}), 400) current_user, _status = User.get_user_data_from_session(session_id) if not (str(current_user.get('_id')) == user_id or current_user.get('isAdmin')): log = Logs('delete_user', \ dumps({"error": "you cannot delete an account you do not own"}), 401) log.create() return make_response( dumps({"error": "you cannot delete an account you do not own"}), 401) delete_result, response_status = User.delete_user(user_id) log = Logs('delete_user', dumps(delete_result), response_status) log.create() return make_response(dumps(delete_result), response_status)