<s> import pytz <EOL> from datetime import datetime <EOL> from django . conf import settings <EOL> from django . http import Http404 <EOL> from django . shortcuts import get_object_or_404 <EOL> from rest_framework import viewsets <EOL> from rest_framework import permissions <EOL> from rest_framework . response import Response <EOL> from rest_framework . decorators import detail_route <EOL> from onadata . apps . api . tools import get_media_file_response <EOL> from onadata . apps . logger . models . xform import XForm <EOL> from onadata . apps . main . models . meta_data import MetaData <EOL> from onadata . apps . main . models . user_profile import UserProfile <EOL> from onadata . libs import filters <EOL> from onadata . libs . authentication import DigestAuthentication <EOL> from onadata . libs . renderers . renderers import MediaFileContentNegotiation <EOL> from onadata . libs . renderers . renderers import XFormListRenderer <EOL> from onadata . libs . renderers . renderers import XFormManifestRenderer <EOL> from onadata . libs . serializers . xform_serializer import XFormListSerializer <EOL> from onadata . libs . serializers . xform_serializer import XFormManifestSerializer <EOL> DEFAULT_CONTENT_LENGTH = getattr ( settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> class XFormListApi ( viewsets . ReadOnlyModelViewSet ) : <EOL> content_negotiation_class = MediaFileContentNegotiation <EOL> filter_backends = ( filters . XFormListObjectPermissionFilter , ) <EOL> queryset = XForm . objects . filter ( downloadable = True ) <EOL> permission_classes = ( permissions . AllowAny , ) <EOL> renderer_classes = ( XFormListRenderer , ) <EOL> serializer_class = XFormListSerializer <EOL> template_name = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( XFormListApi , self ) . __init__ ( * args , ** kwargs ) <EOL> authentication_classes = [ <EOL> DigestAuthentication , <EOL> ] <EOL> self . authentication_classes = authentication_classes + [ <EOL> auth_class for auth_class in self . authentication_classes <EOL> if not auth_class in authentication_classes <EOL> ] <EOL> def get_openrosa_headers ( self ) : <EOL> tz = pytz . timezone ( settings . TIME_ZONE ) <EOL> dt = datetime . now ( tz ) . strftime ( '<STR_LIT>' ) <EOL> return { <EOL> '<STR_LIT>' : dt , <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT>' : DEFAULT_CONTENT_LENGTH <EOL> } <EOL> def get_renderers ( self ) : <EOL> if self . action and self . action == '<STR_LIT>' : <EOL> return [ XFormManifestRenderer ( ) ] <EOL> return super ( XFormListApi , self ) . get_renderers ( ) <EOL> def filter_queryset ( self , queryset ) : <EOL> username = self . kwargs . get ( '<STR_LIT:username>' ) <EOL> if username is None and self . request . user . is_anonymous ( ) : <EOL> self . permission_denied ( self . request ) <EOL> if username is not None : <EOL> profile = get_object_or_404 ( <EOL> UserProfile , user__username = username . lower ( ) ) <EOL> if profile . require_auth and self . request . user . is_anonymous ( ) : <EOL> self . permission_denied ( self . request ) <EOL> else : <EOL> queryset = queryset . filter ( user = profile . user ) <EOL> if not self . request . user . is_anonymous ( ) : <EOL> queryset = super ( XFormListApi , self ) . filter_queryset ( queryset ) <EOL> return queryset <EOL> def list ( self , request , * args , ** kwargs ) : <EOL> self . object_list = self . filter_queryset ( self . get_queryset ( ) ) <EOL> serializer = self . get_serializer ( self . object_list , many = True ) <EOL> return Response ( serializer . data , headers = self . get_openrosa_headers ( ) ) <EOL> def retrieve ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> return Response ( self . object . xml , headers = self . get_openrosa_headers ( ) ) <EOL> @ detail_route ( methods = [ '<STR_LIT:GET>' ] ) <EOL> def manifest ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> object_list = MetaData . objects . filter ( data_type = '<STR_LIT>' , <EOL> xform = self . object ) <EOL> context = self . get_serializer_context ( ) <EOL> serializer = XFormManifestSerializer ( object_list , many = True , <EOL> context = context ) <EOL> return Response ( serializer . data , headers = self . get_openrosa_headers ( ) ) <EOL> @ detail_route ( methods = [ '<STR_LIT:GET>' ] ) <EOL> def media ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> pk = kwargs . get ( '<STR_LIT>' ) <EOL> if not pk : <EOL> raise Http404 ( ) <EOL> meta_obj = get_object_or_404 ( <EOL> MetaData , data_type = '<STR_LIT>' , xform = self . object , pk = pk ) <EOL> return get_media_file_response ( meta_obj ) </s>
