如何将用户输入从AngularJS传递到非本地客户端

时间:2018-06-13 09:34:45

标签: python datetime timezone backend web-frontend

我尝试询问this问题,关于如何将用户输入从AngularJS传递到Python后端。

该问题已被标记为Getting the client's timezone in JavaScript问题的副本,并且已关闭 - 我查看了它被引用为重复的问题,虽然它是相关的,但它似乎回答了特定的情况,而不是帮助解决我的问题。我编辑了我的问题来解释为什么它没有回答这个问题,但我的帖子仍然关闭。

我的帖子被标记为重复的问题的答案提供了获取客户的本地时区的解决方案 - 我不一定想要获得客户的本地时区,而是希望允许用户选择他们想要导出数据的时区。

正如我原来的question所述,我不知道如何修改Python后端记录的UTC时间以反映用户选择的时区的相应本地时间 - 以填充导出的CSV文件,其时间值是用户所选时区的本地时间值,而不是UTC中的时间值。

将信息导出到CSV文件的Python函数是:

class CSVOutput(object):
    # Use pdb.set_trace() to pause the execution of the Python script & debug it step by     step in the command line
    # pdb.set_trace()

    # When the user asks for a download this file extension will be used
    EXTENSION = 'csv'

    # The internet media type for CSV files is specified by RFC 4180
    HEADERS = [('Content-Type', 'text/csv; charset=UTF-8')]

    # Specify a custom lexer for use with pygments (used for highlighting in
    # the browsable API)
    LEXER = CSVLexer

    # This method is called to determine the headings given the object
    def get_headings(self, obj):
        raise NotImplementedError

    # This method is called to prepare a row given the record
    def format_row(self, record):
        raise NotImplementedError

    # This method takes a response and converts it to CSV format
    def dumps(self, obj, **hints):
        # Watch out for the user asking to convert a listing
        if isinstance(obj, list):
            raise ValueError('CSV output is only supported for instances')

        # The response should usually be a ResponseWrapper instance
        if not hasattr(obj, 'extra'):
            raise ValueError('Object is missing the "extra" attribute')

        # All log query responses should have a key for the records
        if 'records' not in obj.extra:
            raise ValueError('Response is missing the "records" key')

        # In order to use the CSV writer we need a file-like object
        data = StringIO()

        # Create a CSV writer using the default dialect
        writer = get_writer(data)

        # The first line of the CSV always contains the headings
        writer.writerow(self.get_headings(obj))

        # All the other rows contain data (which we format first)
        writer.writerows(map(self.format_row, obj.extra['records']))

        #console.log("Value of $scope.timezone in output.py/dumps: ", scope.timezone);

        # Pass back the headers, converted data and file extension
        return self.HEADERS, data.getvalue(), self.EXTENSION

# This class provides conversion to CSV for data log query responses
class DataLogCSVOutput(CSVOutput):
    #pdb.set_trace()
    # This method is called to determine the headings given the object
    def get_headings(self, obj):
        # The headings for data logs vary depending on the tags defined
        #console.log("Value of self in DataLogCSVOutput().get_headings(): ", self);
        #console.log("Value of obj in DataLogCSVOutput().get_headings(): ", obj);
        return ['timestamp'] + obj.extra['tags']

    # This method is called to prepare a row given the record
    def format_row(self, record):
        # Output rows are comprised of the timestamp and values
        #console.log("Value of self in DataLogCSVOutput().format_row(): ", self);
        #console.log("Value of record in DataLogCSVOutput().format_row(): ", record);
        return [record['timestamp']] + record['values']

# This class provides conversion to CSV for event log query responses
class EventLogCSVOutput(CSVOutput):
    #pdb.set_trace()
    # This method is called to determine the headings given the object
    def get_headings(self, obj):
        # The headings for event logs are fixed
        return 'timestamp', 'action', 'message'

    # This method is called to prepare a row given the record
    def format_row(self, record):
        # In event logs the record fields map directly to a row
        # Check whether the selected time zone is UTC- if it is, return; if not, amend the time value, and then return
        if(timezone == 'UTC'):
            console.log("record: ", record);
            return record['timestamp'], record['action'], record['message']
        else:
            console.log("record in else: ", record);
            console.log("timestamp value is: ", record['timestamp'].value);

我尝试在if函数中使用format_row()语句检查用户在表单中选择的时区是否设置为&#39; UTC&#39; - 如果是,则为应该返回记录,如果没有,只显示一些调试(这是我想要实现从系统UTC时间加/减的代码,以便给用户选择的时区本地时间。< / p>

但是,当按下“导出”时,我目前看不到任何调试。表单上的按钮,无论我选择什么时区,时间仍然以UTC值导出(如果时区不是UTC,则当前不应处理return语句... )。

任何人都有任何想法如何实现从UTC时间添加/减去小时数的功能,以便在用户选择的时区中导出数据?

0 个答案:

没有答案