We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
"Since Time" filter in MARS query
Using the MARS query, the results do not depend on the value chosen for the "Since Time" filter.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `tom_alerts/brokers/mars.py`
Content:
```
1 import requests
2 import json
3 from requests.exceptions import HTTPError
4 from urllib.parse import urlencode
5 from dateutil.parser import parse
6 from django import forms
7 from crispy_forms.layout import Layout, Div, Fieldset, HTML
8 from astropy.time import Time, TimezoneInfo
9 
10 from tom_alerts.alerts import GenericQueryForm, GenericAlert
11 from tom_targets.models import Target, TargetExtra
12 from tom_dataproducts.models import ReducedDatum
13 
14 MARS_URL = 'https://mars.lco.global'
15 filters = {0: 'g', 1: 'r', 2: 'i'}
16 
17 
18 class MARSQueryForm(GenericQueryForm):
19     time__gt = forms.CharField(
20         required=False,
21         label='Time Lower',
22         widget=forms.TextInput(attrs={'type': 'date'})
23     )
24     time__lt = forms.CharField(
25         required=False,
26         label='Time Upper',
27         widget=forms.TextInput(attrs={'type': 'date'})
28     )
29     since__time = forms.IntegerField(
30         required=False,
31         label='Since Time',
32         help_text='Alerts younger than this number of seconds'
33     )
34     jd__gt = forms.FloatField(required=False, label='JD Lower')
35     jd__lt = forms.FloatField(required=False, label='JD Upper')
36     filter = forms.CharField(required=False)
37     cone = forms.CharField(
38         required=False,
39         label='Cone Search',
40         help_text='RA,Dec,radius in degrees'
41     )
42     objectcone = forms.CharField(
43         required=False,
44         label='Object Cone Search',
45         help_text='Object name,radius in degrees'
46     )
47     objectidps = forms.CharField(
48         required=False,
49         label='Nearby Objects',
50         help_text='Id from PS1 catalog'
51     )
52     ra__gt = forms.FloatField(required=False, label='RA Lower')
53     ra__lt = forms.FloatField(required=False, label='RA Upper')
54     dec__gt = forms.FloatField(required=False, label='Dec Lower')
55     dec__lt = forms.FloatField(required=False, label='Dec Upper')
56     l__gt = forms.FloatField(required=False, label='l Lower')
57     l__lt = forms.FloatField(required=False, label='l Upper')
58     b__gt = forms.FloatField(required=False, label='b Lower')
59     b__lt = forms.FloatField(required=False, label='b Upper')
60     magpsf__gte = forms.FloatField(required=False, label='Magpsf Lower')
61     magpsf__lte = forms.FloatField(required=False, label='Magpsf Upper')
62     sigmapsf__lte = forms.FloatField(required=False, label='Sigmapsf Upper')
63     magap__gte = forms.FloatField(required=False, label='Magap Lower')
64     magap__lte = forms.FloatField(required=False, label='Magap Upper')
65     distnr__gte = forms.FloatField(required=False, label='Distnr Lower')
66     distnr__lte = forms.FloatField(required=False, label='Distnr Upper')
67     deltamaglatest__gte = forms.FloatField(
68         required=False,
69         label='Delta Mag Lower'
70     )
71     deltamaglatest__lte = forms.FloatField(
72         required=False,
73         label='Delta Mag Upper'
74     )
75     deltamagref__gte = forms.FloatField(
76         required=False,
77         label='Delta Mag Ref Lower'
78     )
79     deltamagref__lte = forms.FloatField(
80         required=False,
81         label='Delta Mag Ref Upper'
82     )
83     rb__gte = forms.FloatField(required=False, label='Real/Bogus Lower')
84     classtar__gte = forms.FloatField(required=False, label='Classtar Lower')
85     classtar__lte = forms.FloatField(required=False, label='Classtar Upper')
86     fwhm__lte = forms.FloatField(required=False, label='FWHM Upper')
87 
88     def __init__(self, *args, **kwargs):
89         super().__init__(*args, **kwargs)
90         self.helper.layout = Layout(
91             HTML('''
92                 <p>
93                 Please see <a href="https://mars.lco.global/help">MARS help</a>
94                 for a detailed description of available filters.
95                 </p>
96             '''),
97             self.common_layout,
98             Fieldset(
99                 'Time based filters',
100                 'since__time',
101                 Div(
102                     Div(
103                         'time__gt',
104                         'jd__gt',
105                         css_class='col',
106                     ),
107                     Div(
108                         'time__lt',
109                         'jd__lt',
110                         css_class='col',
111                     ),
112                     css_class="form-row",
113                 )
114             ),
115             Fieldset(
116                 'Location based filters',
117                 'cone',
118                 'objectcone',
119                 'objectidps',
120                 Div(
121                     Div(
122                         'ra__gt',
123                         'dec__gt',
124                         'l__gt',
125                         'b__gt',
126                         css_class='col',
127                     ),
128                     Div(
129                         'ra__lt',
130                         'dec__lt',
131                         'l__lt',
132                         'b__lt',
133                         css_class='col',
134                     ),
135                     css_class="form-row",
136                 )
137             ),
138             Fieldset(
139                 'Other Filters',
140                 Div(
141                     Div(
142                         'magpsf__gte',
143                         'magap__gte',
144                         'distnr__gte',
145                         'deltamaglatest__gte',
146                         'deltamagref__gte',
147                         'classtar__gte',
148                         css_class='col'
149                     ),
150                     Div(
151                         'magpsf__lte',
152                         'magap__lte',
153                         'distnr__lte',
154                         'deltamaglatest__lte',
155                         'deltamagref__lte',
156                         'classtar__lte',
157                         css_class='col'
158                     ),
159                     css_class='form-row',
160                 )
161             ),
162             'filter',
163             'sigmapsf__lte',
164             'rb__gte',
165             'fwhm__lte'
166         )
167 
168 
169 class MARSBroker(object):
170     name = 'MARS'
171     form = MARSQueryForm
172 
173     def _clean_parameters(self, parameters):
174         return {k: v for k, v in parameters.items() if v and k != 'page'}
175 
176     def fetch_alerts(self, parameters):
177         if not parameters.get('page'):
178             parameters['page'] = 1
179         args = urlencode(self._clean_parameters(parameters))
180         url = '{0}/?page={1}&format=json&{2}'.format(
181             MARS_URL,
182             parameters['page'],
183             args
184         )
185         alerts = []
186         response = requests.get(url)
187         response.raise_for_status()
188         parsed = response.json()
189         alerts = parsed['results']
190         if parsed['has_next'] and parameters['page'] < 10:
191             parameters['page'] += 1
192             alerts += self.fetch_alerts(parameters)
193         return alerts
194 
195     def fetch_alert(self, id):
196         url = f'{MARS_URL}/{id}/?format=json'
197         response = requests.get(url)
198         response.raise_for_status()
199         parsed = response.json()
200         return parsed
201 
202     def process_reduced_data(self, target, alert=None):
203         if not alert:
204             try:
205                 target_datum = ReducedDatum.objects.filter(
206                     target=target,
207                     data_type='PHOTOMETRY',
208                     source_name=self.name).first()
209                 if not target_datum:
210                     return
211                 alert = self.fetch_alert(target_datum.source_location)
212             except HTTPError:
213                 raise Exception('Unable to retrieve alert information from broker')
214         for prv_candidate in alert.get('prv_candidate'):
215             if all([key in prv_candidate['candidate'] for key in ['jd', 'magpsf', 'fid']]):
216                 jd = Time(prv_candidate['candidate']['jd'], format='jd', scale='utc')
217                 jd.to_datetime(timezone=TimezoneInfo())
218                 value = {
219                     'magnitude': prv_candidate['candidate']['magpsf'],
220                     'filter': filters[prv_candidate['candidate']['fid']]
221                 }
222                 rd, created = ReducedDatum.objects.get_or_create(
223                     timestamp=jd.to_datetime(timezone=TimezoneInfo()),
224                     value=json.dumps(value),
225                     source_name=self.name,
226                     source_location=alert['lco_id'],
227                     data_type='photometry',
228                     target=target)
229                 rd.save()
230 
231     def to_target(self, alert):
232         alert_copy = alert.copy()
233         target = Target.objects.create(
234             identifier=alert_copy['objectId'],
235             name=alert_copy['objectId'],
236             type='SIDEREAL',
237             ra=alert_copy['candidate'].pop('ra'),
238             dec=alert_copy['candidate'].pop('dec'),
239             galactic_lng=alert_copy['candidate'].pop('l'),
240             galactic_lat=alert_copy['candidate'].pop('b'),
241         )
242         for k, v in alert_copy['candidate'].items():
243             if v is not None:
244                 TargetExtra.objects.create(target=target, key=k, value=v)
245 
246         return target
247 
248     def to_generic_alert(self, alert):
249         timestamp = parse(alert['candidate']['wall_time'])
250         url = '{0}/{1}/'.format(MARS_URL, alert['lco_id'])
251 
252         return GenericAlert(
253             timestamp=timestamp,
254             url=url,
255             id=alert['lco_id'],
256             name=alert['objectId'],
257             ra=alert['candidate']['ra'],
258             dec=alert['candidate']['dec'],
259             mag=alert['candidate']['magpsf'],
260             score=alert['candidate']['rb']
261         )
262
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/tom_alerts/brokers/mars.py b/tom_alerts/brokers/mars.py
--- a/tom_alerts/brokers/mars.py
+++ b/tom_alerts/brokers/mars.py
@@ -26,9 +26,9 @@
         label='Time Upper',
         widget=forms.TextInput(attrs={'type': 'date'})
     )
-    since__time = forms.IntegerField(
+    time__since = forms.IntegerField(
         required=False,
-        label='Since Time',
+        label='Time Since',
         help_text='Alerts younger than this number of seconds'
     )
     jd__gt = forms.FloatField(required=False, label='JD Lower')
@@ -97,7 +97,7 @@
             self.common_layout,
             Fieldset(
                 'Time based filters',
-                'since__time',
+                'time__since',
                 Div(
                     Div(
                         'time__gt',

{"golden_diff": "diff --git a/tom_alerts/brokers/mars.py b/tom_alerts/brokers/mars.py\n--- a/tom_alerts/brokers/mars.py\n+++ b/tom_alerts/brokers/mars.py\n@@ -26,9 +26,9 @@\n         label='Time Upper',\n         widget=forms.TextInput(attrs={'type': 'date'})\n     )\n-    since__time = forms.IntegerField(\n+    time__since = forms.IntegerField(\n         required=False,\n-        label='Since Time',\n+        label='Time Since',\n         help_text='Alerts younger than this number of seconds'\n     )\n     jd__gt = forms.FloatField(required=False, label='JD Lower')\n@@ -97,7 +97,7 @@\n             self.common_layout,\n             Fieldset(\n                 'Time based filters',\n-                'since__time',\n+                'time__since',\n                 Div(\n                     Div(\n                         'time__gt',\n", "issue": "\"Since Time\" filter in MARS query\nUsing the MARS query, the results do not depend on the value chosen for the \"Since Time\" filter.\n", "before_files": [{"content": "import requests\nimport json\nfrom requests.exceptions import HTTPError\nfrom urllib.parse import urlencode\nfrom dateutil.parser import parse\nfrom django import forms\nfrom crispy_forms.layout import Layout, Div, Fieldset, HTML\nfrom astropy.time import Time, TimezoneInfo\n\nfrom tom_alerts.alerts import GenericQueryForm, GenericAlert\nfrom tom_targets.models import Target, TargetExtra\nfrom tom_dataproducts.models import ReducedDatum\n\nMARS_URL = 'https://mars.lco.global'\nfilters = {0: 'g', 1: 'r', 2: 'i'}\n\n\nclass MARSQueryForm(GenericQueryForm):\n    time__gt = forms.CharField(\n        required=False,\n        label='Time Lower',\n        widget=forms.TextInput(attrs={'type': 'date'})\n    )\n    time__lt = forms.CharField(\n        required=False,\n        label='Time Upper',\n        widget=forms.TextInput(attrs={'type': 'date'})\n    )\n    since__time = forms.IntegerField(\n        required=False,\n        label='Since Time',\n        help_text='Alerts younger than this number of seconds'\n    )\n    jd__gt = forms.FloatField(required=False, label='JD Lower')\n    jd__lt = forms.FloatField(required=False, label='JD Upper')\n    filter = forms.CharField(required=False)\n    cone = forms.CharField(\n        required=False,\n        label='Cone Search',\n        help_text='RA,Dec,radius in degrees'\n    )\n    objectcone = forms.CharField(\n        required=False,\n        label='Object Cone Search',\n        help_text='Object name,radius in degrees'\n    )\n    objectidps = forms.CharField(\n        required=False,\n        label='Nearby Objects',\n        help_text='Id from PS1 catalog'\n    )\n    ra__gt = forms.FloatField(required=False, label='RA Lower')\n    ra__lt = forms.FloatField(required=False, label='RA Upper')\n    dec__gt = forms.FloatField(required=False, label='Dec Lower')\n    dec__lt = forms.FloatField(required=False, label='Dec Upper')\n    l__gt = forms.FloatField(required=False, label='l Lower')\n    l__lt = forms.FloatField(required=False, label='l Upper')\n    b__gt = forms.FloatField(required=False, label='b Lower')\n    b__lt = forms.FloatField(required=False, label='b Upper')\n    magpsf__gte = forms.FloatField(required=False, label='Magpsf Lower')\n    magpsf__lte = forms.FloatField(required=False, label='Magpsf Upper')\n    sigmapsf__lte = forms.FloatField(required=False, label='Sigmapsf Upper')\n    magap__gte = forms.FloatField(required=False, label='Magap Lower')\n    magap__lte = forms.FloatField(required=False, label='Magap Upper')\n    distnr__gte = forms.FloatField(required=False, label='Distnr Lower')\n    distnr__lte = forms.FloatField(required=False, label='Distnr Upper')\n    deltamaglatest__gte = forms.FloatField(\n        required=False,\n        label='Delta Mag Lower'\n    )\n    deltamaglatest__lte = forms.FloatField(\n        required=False,\n        label='Delta Mag Upper'\n    )\n    deltamagref__gte = forms.FloatField(\n        required=False,\n        label='Delta Mag Ref Lower'\n    )\n    deltamagref__lte = forms.FloatField(\n        required=False,\n        label='Delta Mag Ref Upper'\n    )\n    rb__gte = forms.FloatField(required=False, label='Real/Bogus Lower')\n    classtar__gte = forms.FloatField(required=False, label='Classtar Lower')\n    classtar__lte = forms.FloatField(required=False, label='Classtar Upper')\n    fwhm__lte = forms.FloatField(required=False, label='FWHM Upper')\n\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.helper.layout = Layout(\n            HTML('''\n                <p>\n                Please see <a href=\"https://mars.lco.global/help\">MARS help</a>\n                for a detailed description of available filters.\n                </p>\n            '''),\n            self.common_layout,\n            Fieldset(\n                'Time based filters',\n                'since__time',\n                Div(\n                    Div(\n                        'time__gt',\n                        'jd__gt',\n                        css_class='col',\n                    ),\n                    Div(\n                        'time__lt',\n                        'jd__lt',\n                        css_class='col',\n                    ),\n                    css_class=\"form-row\",\n                )\n            ),\n            Fieldset(\n                'Location based filters',\n                'cone',\n                'objectcone',\n                'objectidps',\n                Div(\n                    Div(\n                        'ra__gt',\n                        'dec__gt',\n                        'l__gt',\n                        'b__gt',\n                        css_class='col',\n                    ),\n                    Div(\n                        'ra__lt',\n                        'dec__lt',\n                        'l__lt',\n                        'b__lt',\n                        css_class='col',\n                    ),\n                    css_class=\"form-row\",\n                )\n            ),\n            Fieldset(\n                'Other Filters',\n                Div(\n                    Div(\n                        'magpsf__gte',\n                        'magap__gte',\n                        'distnr__gte',\n                        'deltamaglatest__gte',\n                        'deltamagref__gte',\n                        'classtar__gte',\n                        css_class='col'\n                    ),\n                    Div(\n                        'magpsf__lte',\n                        'magap__lte',\n                        'distnr__lte',\n                        'deltamaglatest__lte',\n                        'deltamagref__lte',\n                        'classtar__lte',\n                        css_class='col'\n                    ),\n                    css_class='form-row',\n                )\n            ),\n            'filter',\n            'sigmapsf__lte',\n            'rb__gte',\n            'fwhm__lte'\n        )\n\n\nclass MARSBroker(object):\n    name = 'MARS'\n    form = MARSQueryForm\n\n    def _clean_parameters(self, parameters):\n        return {k: v for k, v in parameters.items() if v and k != 'page'}\n\n    def fetch_alerts(self, parameters):\n        if not parameters.get('page'):\n            parameters['page'] = 1\n        args = urlencode(self._clean_parameters(parameters))\n        url = '{0}/?page={1}&format=json&{2}'.format(\n            MARS_URL,\n            parameters['page'],\n            args\n        )\n        alerts = []\n        response = requests.get(url)\n        response.raise_for_status()\n        parsed = response.json()\n        alerts = parsed['results']\n        if parsed['has_next'] and parameters['page'] < 10:\n            parameters['page'] += 1\n            alerts += self.fetch_alerts(parameters)\n        return alerts\n\n    def fetch_alert(self, id):\n        url = f'{MARS_URL}/{id}/?format=json'\n        response = requests.get(url)\n        response.raise_for_status()\n        parsed = response.json()\n        return parsed\n\n    def process_reduced_data(self, target, alert=None):\n        if not alert:\n            try:\n                target_datum = ReducedDatum.objects.filter(\n                    target=target,\n                    data_type='PHOTOMETRY',\n                    source_name=self.name).first()\n                if not target_datum:\n                    return\n                alert = self.fetch_alert(target_datum.source_location)\n            except HTTPError:\n                raise Exception('Unable to retrieve alert information from broker')\n        for prv_candidate in alert.get('prv_candidate'):\n            if all([key in prv_candidate['candidate'] for key in ['jd', 'magpsf', 'fid']]):\n                jd = Time(prv_candidate['candidate']['jd'], format='jd', scale='utc')\n                jd.to_datetime(timezone=TimezoneInfo())\n                value = {\n                    'magnitude': prv_candidate['candidate']['magpsf'],\n                    'filter': filters[prv_candidate['candidate']['fid']]\n                }\n                rd, created = ReducedDatum.objects.get_or_create(\n                    timestamp=jd.to_datetime(timezone=TimezoneInfo()),\n                    value=json.dumps(value),\n                    source_name=self.name,\n                    source_location=alert['lco_id'],\n                    data_type='photometry',\n                    target=target)\n                rd.save()\n\n    def to_target(self, alert):\n        alert_copy = alert.copy()\n        target = Target.objects.create(\n            identifier=alert_copy['objectId'],\n            name=alert_copy['objectId'],\n            type='SIDEREAL',\n            ra=alert_copy['candidate'].pop('ra'),\n            dec=alert_copy['candidate'].pop('dec'),\n            galactic_lng=alert_copy['candidate'].pop('l'),\n            galactic_lat=alert_copy['candidate'].pop('b'),\n        )\n        for k, v in alert_copy['candidate'].items():\n            if v is not None:\n                TargetExtra.objects.create(target=target, key=k, value=v)\n\n        return target\n\n    def to_generic_alert(self, alert):\n        timestamp = parse(alert['candidate']['wall_time'])\n        url = '{0}/{1}/'.format(MARS_URL, alert['lco_id'])\n\n        return GenericAlert(\n            timestamp=timestamp,\n            url=url,\n            id=alert['lco_id'],\n            name=alert['objectId'],\n            ra=alert['candidate']['ra'],\n            dec=alert['candidate']['dec'],\n            mag=alert['candidate']['magpsf'],\n            score=alert['candidate']['rb']\n        )\n", "path": "tom_alerts/brokers/mars.py"}], "after_files": [{"content": "import requests\nimport json\nfrom requests.exceptions import HTTPError\nfrom urllib.parse import urlencode\nfrom dateutil.parser import parse\nfrom django import forms\nfrom crispy_forms.layout import Layout, Div, Fieldset, HTML\nfrom astropy.time import Time, TimezoneInfo\n\nfrom tom_alerts.alerts import GenericQueryForm, GenericAlert\nfrom tom_targets.models import Target, TargetExtra\nfrom tom_dataproducts.models import ReducedDatum\n\nMARS_URL = 'https://mars.lco.global'\nfilters = {0: 'g', 1: 'r', 2: 'i'}\n\n\nclass MARSQueryForm(GenericQueryForm):\n    time__gt = forms.CharField(\n        required=False,\n        label='Time Lower',\n        widget=forms.TextInput(attrs={'type': 'date'})\n    )\n    time__lt = forms.CharField(\n        required=False,\n        label='Time Upper',\n        widget=forms.TextInput(attrs={'type': 'date'})\n    )\n    time__since = forms.IntegerField(\n        required=False,\n        label='Time Since',\n        help_text='Alerts younger than this number of seconds'\n    )\n    jd__gt = forms.FloatField(required=False, label='JD Lower')\n    jd__lt = forms.FloatField(required=False, label='JD Upper')\n    filter = forms.CharField(required=False)\n    cone = forms.CharField(\n        required=False,\n        label='Cone Search',\n        help_text='RA,Dec,radius in degrees'\n    )\n    objectcone = forms.CharField(\n        required=False,\n        label='Object Cone Search',\n        help_text='Object name,radius in degrees'\n    )\n    objectidps = forms.CharField(\n        required=False,\n        label='Nearby Objects',\n        help_text='Id from PS1 catalog'\n    )\n    ra__gt = forms.FloatField(required=False, label='RA Lower')\n    ra__lt = forms.FloatField(required=False, label='RA Upper')\n    dec__gt = forms.FloatField(required=False, label='Dec Lower')\n    dec__lt = forms.FloatField(required=False, label='Dec Upper')\n    l__gt = forms.FloatField(required=False, label='l Lower')\n    l__lt = forms.FloatField(required=False, label='l Upper')\n    b__gt = forms.FloatField(required=False, label='b Lower')\n    b__lt = forms.FloatField(required=False, label='b Upper')\n    magpsf__gte = forms.FloatField(required=False, label='Magpsf Lower')\n    magpsf__lte = forms.FloatField(required=False, label='Magpsf Upper')\n    sigmapsf__lte = forms.FloatField(required=False, label='Sigmapsf Upper')\n    magap__gte = forms.FloatField(required=False, label='Magap Lower')\n    magap__lte = forms.FloatField(required=False, label='Magap Upper')\n    distnr__gte = forms.FloatField(required=False, label='Distnr Lower')\n    distnr__lte = forms.FloatField(required=False, label='Distnr Upper')\n    deltamaglatest__gte = forms.FloatField(\n        required=False,\n        label='Delta Mag Lower'\n    )\n    deltamaglatest__lte = forms.FloatField(\n        required=False,\n        label='Delta Mag Upper'\n    )\n    deltamagref__gte = forms.FloatField(\n        required=False,\n        label='Delta Mag Ref Lower'\n    )\n    deltamagref__lte = forms.FloatField(\n        required=False,\n        label='Delta Mag Ref Upper'\n    )\n    rb__gte = forms.FloatField(required=False, label='Real/Bogus Lower')\n    classtar__gte = forms.FloatField(required=False, label='Classtar Lower')\n    classtar__lte = forms.FloatField(required=False, label='Classtar Upper')\n    fwhm__lte = forms.FloatField(required=False, label='FWHM Upper')\n\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.helper.layout = Layout(\n            HTML('''\n                <p>\n                Please see <a href=\"https://mars.lco.global/help\">MARS help</a>\n                for a detailed description of available filters.\n                </p>\n            '''),\n            self.common_layout,\n            Fieldset(\n                'Time based filters',\n                'time__since',\n                Div(\n                    Div(\n                        'time__gt',\n                        'jd__gt',\n                        css_class='col',\n                    ),\n                    Div(\n                        'time__lt',\n                        'jd__lt',\n                        css_class='col',\n                    ),\n                    css_class=\"form-row\",\n                )\n            ),\n            Fieldset(\n                'Location based filters',\n                'cone',\n                'objectcone',\n                'objectidps',\n                Div(\n                    Div(\n                        'ra__gt',\n                        'dec__gt',\n                        'l__gt',\n                        'b__gt',\n                        css_class='col',\n                    ),\n                    Div(\n                        'ra__lt',\n                        'dec__lt',\n                        'l__lt',\n                        'b__lt',\n                        css_class='col',\n                    ),\n                    css_class=\"form-row\",\n                )\n            ),\n            Fieldset(\n                'Other Filters',\n                Div(\n                    Div(\n                        'magpsf__gte',\n                        'magap__gte',\n                        'distnr__gte',\n                        'deltamaglatest__gte',\n                        'deltamagref__gte',\n                        'classtar__gte',\n                        css_class='col'\n                    ),\n                    Div(\n                        'magpsf__lte',\n                        'magap__lte',\n                        'distnr__lte',\n                        'deltamaglatest__lte',\n                        'deltamagref__lte',\n                        'classtar__lte',\n                        css_class='col'\n                    ),\n                    css_class='form-row',\n                )\n            ),\n            'filter',\n            'sigmapsf__lte',\n            'rb__gte',\n            'fwhm__lte'\n        )\n\n\nclass MARSBroker(object):\n    name = 'MARS'\n    form = MARSQueryForm\n\n    def _clean_parameters(self, parameters):\n        return {k: v for k, v in parameters.items() if v and k != 'page'}\n\n    def fetch_alerts(self, parameters):\n        if not parameters.get('page'):\n            parameters['page'] = 1\n        args = urlencode(self._clean_parameters(parameters))\n        url = '{0}/?page={1}&format=json&{2}'.format(\n            MARS_URL,\n            parameters['page'],\n            args\n        )\n        alerts = []\n        response = requests.get(url)\n        response.raise_for_status()\n        parsed = response.json()\n        alerts = parsed['results']\n        if parsed['has_next'] and parameters['page'] < 10:\n            parameters['page'] += 1\n            alerts += self.fetch_alerts(parameters)\n        return alerts\n\n    def fetch_alert(self, id):\n        url = f'{MARS_URL}/{id}/?format=json'\n        response = requests.get(url)\n        response.raise_for_status()\n        parsed = response.json()\n        return parsed\n\n    def process_reduced_data(self, target, alert=None):\n        if not alert:\n            try:\n                target_datum = ReducedDatum.objects.filter(\n                    target=target,\n                    data_type='PHOTOMETRY',\n                    source_name=self.name).first()\n                if not target_datum:\n                    return\n                alert = self.fetch_alert(target_datum.source_location)\n            except HTTPError:\n                raise Exception('Unable to retrieve alert information from broker')\n        for prv_candidate in alert.get('prv_candidate'):\n            if all([key in prv_candidate['candidate'] for key in ['jd', 'magpsf', 'fid']]):\n                jd = Time(prv_candidate['candidate']['jd'], format='jd', scale='utc')\n                jd.to_datetime(timezone=TimezoneInfo())\n                value = {\n                    'magnitude': prv_candidate['candidate']['magpsf'],\n                    'filter': filters[prv_candidate['candidate']['fid']]\n                }\n                rd, created = ReducedDatum.objects.get_or_create(\n                    timestamp=jd.to_datetime(timezone=TimezoneInfo()),\n                    value=json.dumps(value),\n                    source_name=self.name,\n                    source_location=alert['lco_id'],\n                    data_type='photometry',\n                    target=target)\n                rd.save()\n\n    def to_target(self, alert):\n        alert_copy = alert.copy()\n        target = Target.objects.create(\n            identifier=alert_copy['objectId'],\n            name=alert_copy['objectId'],\n            type='SIDEREAL',\n            ra=alert_copy['candidate'].pop('ra'),\n            dec=alert_copy['candidate'].pop('dec'),\n            galactic_lng=alert_copy['candidate'].pop('l'),\n            galactic_lat=alert_copy['candidate'].pop('b'),\n        )\n        for k, v in alert_copy['candidate'].items():\n            if v is not None:\n                TargetExtra.objects.create(target=target, key=k, value=v)\n\n        return target\n\n    def to_generic_alert(self, alert):\n        timestamp = parse(alert['candidate']['wall_time'])\n        url = '{0}/{1}/'.format(MARS_URL, alert['lco_id'])\n\n        return GenericAlert(\n            timestamp=timestamp,\n            url=url,\n            id=alert['lco_id'],\n            name=alert['objectId'],\n            ra=alert['candidate']['ra'],\n            dec=alert['candidate']['dec'],\n            mag=alert['candidate']['magpsf'],\n            score=alert['candidate']['rb']\n        )\n", "path": "tom_alerts/brokers/mars.py"}]}