def program_to_dict(program): """transform a program object into dict """ judge_time = program.judge_time if judge_time == getdate_none(): judge_time = None upload_time = program.upload_time if upload_time == getdate_none(): upload_time = None ProgramHelper.refresh(program.id) return { 'id': program.id, 'name': program.name, 'author': program.author, 'code': program.code, 'doc': program.doc, 'submit_time': date_to_string(program.submit_time), 'status': program.status, 'judge': program.judge, 'judge_time': date_to_string(judge_time), 'upload_time': date_to_string(upload_time), 'downloads': program.downloads, 'likes': program.likes, 'schoolid': program.schoolid, "subjectid": program.subjectid }
def fetch_stat_data(request): if not request.is_ajax() or request.method != 'POST': #return method not allowed responsedata = { 'success': False, 'message': 'Only ajax with POST is allowed.' } return HttpResponse(json.dumps(responsedata), status=405) params = json.loads(request.body) try: widget_data = Data.objects.get(user=request.user, id=params['data_id']) except Exception as e: responsedata = {'success': False, 'message': e.message} return HttpResponse(json.dumps(responsedata), status=500) responsedata = { 'success': True, 'message': '', 'name': widget_data.name, 'data': widget_data.function, 'sensor': widget_data.sensor, 'extract': widget_data.extract, 'chart': widget_data.chart.widget_id if widget_data.chart is not None else None, 'from': date_to_string(widget_data.date_from), 'to': date_to_string(widget_data.date_to), } return HttpResponse(json.dumps(responsedata), status=200)
def parse_date_range(self, limit=None, date_from_str=None, date_to_str=None, timedelta=0): """ This function gets articles based on the time of creation from the database, and calls self.parse_articles() on the fetched articles. :param limit: Maximum number of articles. :param date_from_str: Minimum date string. :param date_to_str: Maximum date string. :param timedelta: Integer : number of days to add. :return: None """ logger.info('') self.out_articles = [] date_from = utils.parse_date_str(date_from_str, timedelta) # timedelta date_to = utils.parse_date_str(date_to_str, 1) # next midnight print("Parsing date range %s to %s..." % (utils.date_to_string(date_from), utils.date_to_string(date_to))) articles = utils.get_article_daterange(date_from, date_to) articles = articles[:limit] self.parse_articles(articles)
def stream(): while True: time_now = utils.date_to_string(datetime.now()) time_last_15min = utils.date_to_string(datetime.now() - timedelta(minutes=15)) articles = request_news(from_date=time_last_15min, to_date=time_now, language='pt') extend_news(articles) print(f"{articles['totalResults']} acquired") with open(file, "w") as write_file: json.dump(articles, write_file) time.sleep(utils.minutes_15)
def get_events(start_date, end_date, classification): query = "SELECT id, name, date, classification FROM events WHERE date >= %s AND date <= %s AND classification=%s" cursor = db.cursor() cursor.execute( query, (date_to_string(start_date), date_to_string(end_date), classification)) results = cursor.fetchall() data = [] for result in results: dt = result[2] event = Event(result[0], result[1], dt, result[3]) data.append(event.serialize()) return json.dumps(data)
def get_subject_with_schoolid(subject_id): """get a subject """ qs = Subject.objects.filter(id=subject_id) if qs.exists(): subject = qs.last() return { 'id': subject.id, 'title': subject.title, 'school_id': subject.school_id, 'description': subject.description, 'create_time': date_to_string(subject.create_time), 'deadline': date_to_string(subject.deadline) } return None
def rotate(dataset, new): cur_date = datetime.datetime.now() cur_date_string = utils.date_to_string(cur_date) cur_month = cur_date.month cur_weekday = cur_date.isoweekday() cur_week = cur_date.isocalendar()[1] cur_day = cur_date.day yst_day = (cur_date - datetime.timedelta(days=1)).day cur_hour = cur_date.hour cur_year = cur_date.year utils.create_snapshot(dataset, cur_date_string) snap_list = utils.get_snapshot_list(dataset) for snap in snap_list: snap_tag = snap.split("@")[-1] snap_date = utils.string_to_date(snap_tag) if \ snap_date.year != cur_year or \ (snap_date.month != cur_month and snap_date.day != 1) or \ (snap_date.isoweekday() < cur_weekday and snap_date.isocalendar()[1] != cur_week and snap_date.isoweekday() != 1 and snap_date.day != 1) or \ (snap_date.day != cur_day and snap_date.day != yst_day and snap_date.hour != 0) or \ (snap_date.day == yst_day and snap_date.hour < cur_hour and snap_date.hour != 0): print("Deleting snapshot {}".format(snap)) utils.delete_snapshot(snap)
def get_chats(user_id): """get the chats by user_id return the chaters' info and numbers of unread messages """ qs = Chat.objects.filter(models.Q(user_1=user_id) | models.Q(user_2=user_id)) qs = qs.order_by("-importance", "-latest_time") blocked = BlockHelper.get_block_list(user_id) chats = [] for chat in qs: others = UserHelper.get_user(chat.user_1 + chat.user_2 - user_id) others = UserHelper.user_filter(others) if others is None: continue othername = others['username'] if othername in blocked: continue unread = chat.unread_count if chat.latest_sender == user_id: unread = 0 chats.append({ 'user' : others, 'unread' : unread, 'time' : date_to_string(chat.latest_time) }) return chats
def widget(request): if request.is_ajax(): if request.method == 'POST': data = json.loads(request.body) # {widget: '<widget_id>'} widget = Widgets.objects.filter(user=request.user, widget_id=data['widget']) #widget_dict = {} if widget.exists(): date_range = parse_date( widget[0].widget['data']['range']['to']) - parse_date( widget[0].widget['data']['range']['from']) today = datetime.datetime.now() - (HOUR * 2) date_from = today - date_range new_widget = widget[0].widget new_widget['data']['range']['to'] = date_to_string(today) new_widget['data']['range']['from'] = date_to_string(date_from) update_widget = set_widget(new_widget, request.user) return HttpResponse(json.dumps({'widget': update_widget}))
def get_lunar_phase(date): query = "SELECT * FROM lunar_phases WHERE date=%s" cursor = db.cursor() cursor.execute(query, [date_to_string(date)]) result = cursor.fetchone() if not result: raise Exception("Error: no result found!") cursor.close() phase = LunarPhase(date, result[1]) return json.dumps(phase.serialize())
def get_sun_transition_time(transition, date, latitude, longitude): url = "https://api.sunrise-sunset.org/json?lat={}&lng={}&date={}".format( latitude, longitude, date_to_string(date)) res = requests.get(url) data = json.loads(res.text) if "results" not in data: raise Exception("Error: no results found!") time_string = zero_padded_time(data["results"][transition]) time = datetime.strptime(time_string, "%I:%M:%S %p") return time.strftime("%I:%M:%S %p")
def get_subjects(school_id, listtype, page): """get school's subjects """ params = SubjectHelper.get_subject_filter(school_id, listtype) if params is None: return [] qs = Subject.objects.filter(**params) qs.order_by('-id') qs = qs[(page - 1) * 20:page * 20] subjects = [] for subject in qs: subjects.append({ 'id': subject.id, 'title': subject.title, 'description': subject.description, 'create_time': date_to_string(subject.create_time), 'deadline': date_to_string(subject.deadline) }) return subjects
def update_element(self, index, url): for label_element in preset.label_dictionary: if int(label_element) > 28: break if int(label_element) == 0: self.list_ctrl.InsertItem(index, url[int(label_element)]) element = url[int(label_element)] if isinstance(element, datetime.datetime): element = utils.date_to_string(element) self.list_ctrl.SetItem(index, int(label_element), str(element))
def get_rate(self, currency, date): uri = self.url_rate.replace('{ccy_id}', currency).replace( '{date}', utils.date_to_string(date, self.DATE_FORMAT)) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) return ApiResponse(result=j_resp['Cur_OfficialRate'])
def get_subject_by_name(themename): """get a subject by name """ qs = Subject.objects.filter(title=themename) if qs.exists(): subject = qs.last() school = SchoolHelper.get_school(subject.school_id) if school is None: schoolname = '-' else: schoolname = school.get('schoolname') return { 'id': subject.id, 'title': subject.title, 'schoolname': schoolname, 'description': subject.description, 'create_time': date_to_string(subject.create_time), 'deadline': date_to_string(subject.deadline) } return None
def get_rates_dynamics(self, currency, from_date, to_date): if self.__cur_mapping is None: self.__cur_mapping = self.get_currencies() if self.__cur_mapping is None or len(self.__cur_mapping) == 0: logging.debug(u'Initialization failed') return ApiResponse( has_error=True, error_description='Connection error on initialization') if currency not in self.__cur_mapping: return ApiResponse( has_error=True, error_description='Unsupported/unknown currency') currency_id = self.__cur_mapping[currency] uri = self.url_dynamics \ .replace("{ccy_id}", str(currency_id)) \ .replace("{from}", utils.date_to_string(from_date, self.DATE_FORMAT)) \ .replace("{to}", utils.date_to_string(to_date, self.DATE_FORMAT)) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) rates = {} for item in j_resp: date = utils.string_to_date(item['Date'], self.ISO_DATE_FORMAT) str_date = utils.date_to_string(date, self.DATE_FORMAT) rate = item['Cur_OfficialRate'] rates[str_date] = rate return ApiResponse( result=collections.OrderedDict(sorted(rates.items())))
def video_to_dict(video): """video to dict """ return { 'id' : video.id, 'name' : video.title, 'description' : video.description, 'upload_time' : date_to_string(video.upload_time), 'size' : video.video_size, 'filename' : video.filename, 'filepath' : video.filepath, 'school' : video.school, 'uploader' : UserHelper.get_name_by_id(video.uploader), 'category' : video.category }
def apply_to_dict(apply): """apply to dict """ print(apply, type(apply)) if apply is None: return None data = { 'userid': apply.user_id, 'schoolid': apply.school_id, 'reason': apply.message, 'apply_time': date_to_string(apply.apply_time), 'judger': apply.judge, 'status': apply.status } return data
def create_advent_calendar(): advent_calendar = [] startdate = utils.date_from_string( os.getenv("DISCORD_ADVENT_CALENDAR_START")).astimezone() for i in range(0, 24): advent_calendar.append({ "number": i + 1, "date": utils.date_to_string(startdate + timedelta(days=i)), "assigned": False, "opened": False }) return advent_calendar
def get_applies(school_id, list_type, page): """get school's applies """ params = SchoolApplyHelper.get_applies_filter(school_id, list_type) if params is None: return 0 qs = SchoolApply.objects.filter(**params) qs.order_by('apply_time') qs = qs[(page - 1) * 20:page * 20] applies = [] for apply in qs: username = UserHelper.get_name_by_id(apply.user_id) judger = UserHelper.get_name_by_id(apply.judge) applies.append({ 'id': apply.id, 'username': username, 'reason': apply.message, 'time': date_to_string(apply.apply_time), 'judger': judger, 'status': apply.status }) return applies
def task_4(): ''' Return the data for rentals with 01/06/1999 <= lease start date <= 31/08/2007 ''' reader = utils.get_masts_as_list() lease_start_header = 'Lease Start Date' lease_start_header_index = utils.get_header_index(lease_start_header) range_start = date(1999, 6, 1) range_end = date(2007, 8, 31) res = [] for item in reader: lease_start_date_str = item[lease_start_header_index] if utils.is_date_string_in_range(lease_start_date_str, range_start, range_end): item[lease_start_header_index] = utils.date_to_string( utils.string_to_date(lease_start_date_str)) res.append(item) return res
def get_messages(chat_id, page): """get messages between user_1 and user_2 """ messages = [] qs = Message.objects.filter(chat_id=chat_id, valid=True) qs = qs.order_by('-id') qs = qs[(page - 1) * 20 : page * 20] usernames = {} for message in qs: if usernames.get(message.sender) is None: user = UserHelper.get_user(message.sender) if user is not None: usernames[message.sender] = user['username'] else: usernames[message.sender] = '-' messages.append({ 'id' : message.id, 'username' : usernames[message.sender], 'content' : message.content, 'send_time' : date_to_string(message.send_time) }) return messages
user="******", password="******") CURSOR = CONN.cursor() actual_date = None try: # Parse the last date from the file, if it exists with open('last_date.txt', 'r') as date_file: actual_date = utils.parse_date(date_file.read()) except FileNotFoundError: # If there is no file, we use today's date actual_date = datetime.datetime.now() # Parse the replays, one day at a time. The parse occurs backwards, that means, the most recent replays first while True: # Save to the file the last date parsed with open('last_date.txt', 'w') as date_file: date_file.write(utils.date_to_string(actual_date)) # Get the info from the API actual_page = 1 while True: print("Requesting from {} to {} in page {}".format( utils.date_to_string(actual_date), utils.date_to_string(actual_date + datetime.timedelta(days=1)), actual_page)) replays_info = requests.get( "https://hotsapi.net/api/v1/replays/paged", params={ "page": actual_page, "start_date": utils.date_to_string(actual_date), "end_date":
def update_fetch_date(self, user_id): datetime_str = date_to_string(now()) self._db.child("users").child(user_id).child("last_fetch").set( datetime_str)
if __name__ == '__main__': producer = KafkaProducer( bootstrap_servers=['localhost:9092'], value_serializer=lambda x: dumps(x).encode('UTF-8')) def delivery_callback(err, msg): if err: sys.stderr.write('%% Message failed delivery: %s\n' % err) else: sys.stderr.write('%% Message delivered to %s [%d] @ %d\n' % (msg.topic(), msg.partition(), msg.offset())) while True: time_now = utils.date_to_string(datetime.now()) time_last_15min = utils.date_to_string(datetime.now() - timedelta(minutes=15)) articles = request_news(from_date=time_last_15min, to_date=time_now, language='en') data = articles['articles'] print("Artigos") for artigo in data: print(artigo) producer.send('newsEn', value=preProcessData(artigo.description) + "\n") extend_news(articles) print(f"{articles['totalResults']} acquired") with open("news2.json", "w") as write_file: json.dump(articles, write_file)
def __init__(self, edit_url): super().__init__(parent=None, title=preset.message["edit_title"] + "[" + edit_url.URL_Name + "]", size=(700, 590), style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) self.url = edit_url self.scrolled_panel = ScrolledPanel(self) self.scrolled_panel.SetupScrolling() self.left_box_sizer = wx.BoxSizer(wx.VERTICAL) self.right_box_sizer = wx.BoxSizer(wx.VERTICAL) max_length = 0 for item in preset.label_dictionary: label_length = len(preset.label_dictionary[item]) if label_length > max_length: max_length = label_length self.attribute_list = [] for index, item in enumerate(edit_url.to_list()): element_value = edit_url.get_position(index) if isinstance(element_value, datetime.datetime): item_value = utils.date_to_string(element_value) else: item_value = str(element_value) self.attribute_list.append( wx.TextCtrl(self.scrolled_panel, value=item_value)) dialog_place = "left" if index > 21: dialog_place = "right" self.add_widgets(edit_url.get_label(str(index)), self.attribute_list[index], dialog_place, max_length) cancel_button = wx.Button(self, id=wx.ID_CANCEL, label=preset.message["cancel_button"]) save_button = wx.Button(self, id=wx.ID_OK, label=preset.message["edit_save"]) save_button.Bind(wx.EVT_BUTTON, self.on_save) horizontal_box_sizer = wx.BoxSizer(wx.HORIZONTAL) horizontal_box_sizer.Add(self.left_box_sizer, 1, wx.EXPAND, 1) horizontal_box_sizer.Add(self.right_box_sizer, 1, wx.EXPAND, 1) self.scrolled_panel.SetSizer(horizontal_box_sizer) self.scrolled_panel.SetAutoLayout(1) button_box_sizer = wx.BoxSizer(wx.HORIZONTAL) button_box_sizer.Add(save_button, 1) button_box_sizer.Add(cancel_button, 1) main_box_sizer = wx.BoxSizer(wx.VERTICAL) main_box_sizer.Add(self.scrolled_panel, 1, wx.EXPAND, 1) main_box_sizer.Add(button_box_sizer, 0, wx.ALL | wx.EXPAND, 0) self.SetSizer(main_box_sizer)
def test_date_to_string(): # testing with the default output format test_date = date(2019, 11, 23) tester_output = '23/11/2019' assert date_to_string(test_date) == tester_output
def alert_action(request): #return http bad request if not request.is_ajax(): return HttpResponse('None ajax request') if request.method == 'POST': post_data = json.loads(request.body) post_data.update({'user': request.user}) print 'so far good' # GET, ADD_NEW, MODIFY, REMOVE, MARK_NEW, MARK_NO_NEW, MARK_READ # print post_data['action'] if post_data['action'] == 'ADD_NEW': try: alert = Alert(post_data) uid = alert.save() except IntegrityError as e: print e return HttpResponse( json.dumps({ 'success': False, 'message': 'Failed to save alert. Similar alert exists.' })) return HttpResponse( json.dumps({ 'success': True, 'message': 'Alert saved successfully.', 'uid': uid })) elif post_data['action'] == 'MODIFY': try: alert = Alert(post_data) alert.update() except NameError as e: print e return HttpResponse( json.dumps({ 'success': False, 'message': 'failed to modify alert' })) return HttpResponse( json.dumps({ 'success': True, 'message': 'Alert modified successfully.' })) elif post_data['action'] == 'DELETE': try: alerts = Alerts.objects.filter(user=request.user, uid=post_data['uid']) alerts.delete() except: print sys.exc_info()[0] return HttpResponse( json.dumps({ 'success': False, 'message': 'Delete failed.' })) return HttpResponse( json.dumps({ 'success': True, 'message': 'Alert deleted successfully.' })) elif post_data['action'] == 'MARK_READ': try: event = AlertEvents.objects.filter(user=request.user, event_id=post_data['uid']) event.update(read=True) return HttpResponse( json.dumps({ 'success': True, 'message': 'MARK_READ successful.' })) except (KeyError, AlertEvents.DoesNotExist, Alerts.DoesNotExist) as e: print e return HttpResponse( json.dumps({ 'success': False, 'message': 'MARK_READ failed.' })) elif post_data['action'] == 'MARK_NEW': try: last_mark_off = Settings.objects.get( user=request.user).alert_mark_off new_events_count = 0 if last_mark_off is not None: new_events_count = AlertEvents.objects.filter( user=request.user, t_notify__gt=last_mark_off, notify=True, snoozed=False).count() except Settings.DoesNotExist as e: print e new_events_count = 0 unread_events_count = AlertEvents.objects.filter( user=request.user, read=False, notify=True, snoozed=False).count() new_events = AlertEvents.objects.filter( user=request.user, notify=True, snoozed=False).reverse()[0:post_data['count']] alerts = [] for event in new_events: try: widget = Widgets.objects.get(user=request.user, widget_id=event.widget) widget_title = widget.widget['title'] except Widgets.DoesNotExist as e: print e widget_title = event.widget alerts.extend([{ 'uid': event.event_id, 'name': event.alert.name, 'description': event.alert.description, 'message': event.alert.message, 'read': event.read, 't_notify': date_to_string(event.t_notify), 'value': event.value, 'widget': widget_title, 'station': event.station, 'sensor': event.sensor, 'calc': event.calc, 'type': event.alert.type }]) response_data = { 'alerts': alerts, 'new_count': new_events_count, 'unread': unread_events_count, 'success': True, 'message': 'MARK_NEW successful.' } return HttpResponse(json.dumps(response_data)) elif post_data['action'] == 'MARK_OFF_NEW': try: event = AlertEvents.objects.filter(user=request.user, notify=True).last() except AlertEvents.DoesNotExist as e: print e return HttpResponse( json.dumps({ 'success': False, 'message': 'MARK_OFF_NEW Failed.' })) user_settings = Settings.objects.filter(user=request.user) user_settings.update(alert_mark_off=event.t_notify) return HttpResponse(json.dumps({'success': True})) elif post_data['action'] == 'GET_EVENT': try: event = AlertEvents.objects.get(user=request.user, event_id=post_data['uid']) except AlertEvents.DoesNotExist as e: print e return HttpResponse( json.dumps({ 'success': False, 'message': 'Alert event could not be reteived.' })) try: widget = Widgets.objects.get(user=request.user, widget_id=event.widget) widget_title = widget.widget['title'] except Widgets.DoesNotExist as e: print e widget_title = event.widget alert_event = { 'uid': event.event_id, 'name': event.alert.name, 'description': event.alert.description, 'message': event.alert.message, 'read': event.read, 't_notify': date_to_string(event.t_notify), 'value': event.value, 'widget': widget_title, 'station': event.station, 'sensor': event.sensor, 'calc': event.calc, 'success': True, 'error': 'GET_EVENT successful.' } return HttpResponse(json.dumps(alert_event)) elif request.method == 'GET': post_data = json.loads(request.GET.get('params')) if post_data['action'] == 'GET': try: alert = Alerts.objects.get(uid=post_data['uid'], user=request.user) except (KeyError, Alerts.DoesNotExist) as e: print e return HttpResponse( json.dumps({ 'success': False, 'message': 'Error retreiving alert.' })) response_data = { 'uid': alert.uid, 'name': alert.name, 'description': alert.description, 'sensors': alert.sensors, 'extract': alert.extract, 'logic': alert.logic, 'threshold': alert.threshold, 'message': alert.message, 't_beyond': alert.t_beyond, 'calc': alert.calc, 'type': alert.type, 'snooze': alert.snooze, 'snooze_time': alert.snooze_time, 'email_alert': alert.email_alert, 'sms_alert': alert.sms_alert, 'success': True, 'error': '' } return HttpResponse(json.dumps(response_data))
def serialize(self): return {"date": date_to_string(self.date), "phase": STAGES[self.stage]}