Retrieve the console output for this task. An optional "line" query param can be passed to retrieve only the output starting from a certain line number. An optional "limit" query param can be passed to limit the number of lines to be returned An op
(self, request, pk=None, project_pk=None)
| 176 | |
| 177 | @action(detail=True, methods=['get']) |
| 178 | def output(self, request, pk=None, project_pk=None): |
| 179 | """ |
| 180 | Retrieve the console output for this task. |
| 181 | |
| 182 | An optional "line" query param can be passed to retrieve |
| 183 | only the output starting from a certain line number. |
| 184 | |
| 185 | An optional "limit" query param can be passed to limit |
| 186 | the number of lines to be returned |
| 187 | |
| 188 | An optional "f" query param can be either: "text" (default), "json" or "raw" |
| 189 | """ |
| 190 | try: |
| 191 | task = self.queryset.get(pk=pk, project=project_pk) |
| 192 | check_project_perms(request, task.project) |
| 193 | except (ObjectDoesNotExist, ValidationError): |
| 194 | raise exceptions.NotFound() |
| 195 | |
| 196 | try: |
| 197 | line_num = max(0, int(request.query_params.get('line', 0))) |
| 198 | limit = int(request.query_params.get('limit', 0)) or None |
| 199 | fmt = request.query_params.get('f', 'text') |
| 200 | if fmt not in ['text', 'json', 'raw']: |
| 201 | raise ValueError("Invalid format") |
| 202 | except ValueError: |
| 203 | raise exceptions.ValidationError("Invalid parameter") |
| 204 | |
| 205 | lines = task.console.output().rstrip().split('\n') |
| 206 | count = len(lines) |
| 207 | line_start = min(line_num, count) |
| 208 | line_end = None |
| 209 | |
| 210 | if limit is not None: |
| 211 | if limit > 0: |
| 212 | line_end = line_num + limit |
| 213 | else: |
| 214 | line_start = line_start if count - line_start <= abs(limit) else count - abs(limit) |
| 215 | line_end = None |
| 216 | |
| 217 | if fmt == 'text': |
| 218 | return Response('\n'.join(lines[line_start:line_end])) |
| 219 | elif fmt == 'raw': |
| 220 | return HttpResponse('\n'.join(lines[line_start:line_end]), content_type="text/plain; charset=utf-8") |
| 221 | else: |
| 222 | return Response({ |
| 223 | 'lines': lines[line_start:line_end], |
| 224 | 'count': count |
| 225 | }) |
| 226 | |
| 227 | def list(self, request, project_pk=None): |
| 228 | get_and_check_project(request, project_pk, defer=True) |