<s> from django . shortcuts import get_object_or_404 <EOL> from django . core . mail import send_mail <EOL> from django . contrib . auth . models import User <EOL> from rest_framework import status <EOL> from rest_framework . decorators import detail_route <EOL> from rest_framework . response import Response <EOL> from rest_framework . viewsets import ModelViewSet <EOL> from onadata . libs . filters import ( <EOL> AnonUserProjectFilter , <EOL> ProjectOwnerFilter , <EOL> TagFilter ) <EOL> from onadata . libs . mixins . labels_mixin import LabelsMixin <EOL> from onadata . libs . serializers . user_profile_serializer import UserProfileSerializer <EOL> from onadata . libs . serializers . project_serializer import ProjectSerializer <EOL> from onadata . libs . serializers . share_project_serializer import ShareProjectSerializer <EOL> from onadata . libs . serializers . xform_serializer import XFormSerializer <EOL> from onadata . apps . api . models import Project <EOL> from onadata . apps . api import tools as utils <EOL> from onadata . apps . api . permissions import ProjectPermissions <EOL> from onadata . apps . logger . models import XForm <EOL> from onadata . apps . main . models import UserProfile <EOL> from onadata . settings . common import ( <EOL> DEFAULT_FROM_EMAIL , <EOL> SHARE_PROJECT_SUBJECT ) <EOL> class ProjectViewSet ( LabelsMixin , ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = Project . objects . all ( ) <EOL> serializer_class = ProjectSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> extra_lookup_fields = None <EOL> permission_classes = [ ProjectPermissions ] <EOL> filter_backends = ( AnonUserProjectFilter , <EOL> ProjectOwnerFilter , <EOL> TagFilter ) <EOL> @ detail_route ( methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> def forms ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> project = get_object_or_404 ( Project , pk = kwargs . get ( '<STR_LIT>' ) ) <EOL> if request . method . upper ( ) == '<STR_LIT:POST>' : <EOL> survey = utils . publish_project_xform ( request , project ) <EOL> if isinstance ( survey , XForm ) : <EOL> xform = XForm . objects . get ( pk = survey . pk ) <EOL> serializer = XFormSerializer ( <EOL> xform , context = { '<STR_LIT>' : request } ) <EOL> return Response ( serializer . data , <EOL> status = status . HTTP_201_CREATED ) <EOL> return Response ( survey , status = status . HTTP_400_BAD_REQUEST ) <EOL> project_xforms = project . projectxform_set . values ( '<STR_LIT>' ) <EOL> xforms = XForm . objects . filter ( pk__in = project_xforms ) <EOL> serializer = XFormSerializer ( xforms , context = { '<STR_LIT>' : request } , <EOL> many = True ) <EOL> return Response ( serializer . data ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def share ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> data = dict ( request . data . items ( ) + [ ( '<STR_LIT>' , self . object . pk ) ] ) <EOL> serializer = ShareProjectSerializer ( data = data ) <EOL> if serializer . is_valid ( ) : <EOL> if data . get ( "<STR_LIT>" ) : <EOL> serializer . remove_user ( ) <EOL> else : <EOL> serializer . save ( ) <EOL> email_msg = data . get ( '<STR_LIT>' ) <EOL> if email_msg : <EOL> username = serializer . validated_data [ '<STR_LIT:username>' ] <EOL> user = User . objects . get ( username = username ) <EOL> send_mail ( SHARE_PROJECT_SUBJECT . format ( self . object . name ) , <EOL> email_msg , <EOL> DEFAULT_FROM_EMAIL , <EOL> ( user . email , ) ) <EOL> else : <EOL> return Response ( data = serializer . errors , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def star ( self , request , * args , ** kwargs ) : <EOL> user = request . user <EOL> project = get_object_or_404 ( Project , pk = kwargs . get ( '<STR_LIT>' ) ) <EOL> if request . method == '<STR_LIT>' : <EOL> project . user_stars . remove ( user ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> project . user_stars . add ( user ) <EOL> elif request . method == '<STR_LIT:GET>' : <EOL> users = project . user_stars . values ( '<STR_LIT>' ) <EOL> user_profiles = UserProfile . objects . filter ( user__in = users ) <EOL> serializer = UserProfileSerializer ( user_profiles , <EOL> context = { '<STR_LIT>' : request } , <EOL> many = True ) <EOL> return Response ( serializer . data ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) </s>
