<s> """<STR_LIT>""" <EOL> from django import forms <EOL> from django . contrib . auth import authenticate <EOL> try : <EOL> from django . contrib . auth import get_user_model <EOL> User = get_user_model ( ) <EOL> except ImportError : <EOL> from django . contrib . auth . models import User <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import render_to_response <EOL> from django . template import loader , Context <EOL> import simplejson <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> try : <EOL> from django . utils import timezone <EOL> except ImportError : <EOL> from datetime import datetime as timezone <EOL> from helpdesk . forms import TicketForm <EOL> from helpdesk . lib import send_templated_mail , safe_template_context <EOL> from helpdesk . models import Ticket , Queue , FollowUp <EOL> import warnings <EOL> STATUS_OK = <NUM_LIT:200> <EOL> STATUS_ERROR = <NUM_LIT> <EOL> STATUS_ERROR_NOT_FOUND = <NUM_LIT> <EOL> STATUS_ERROR_PERMISSIONS = <NUM_LIT> <EOL> STATUS_ERROR_BADMETHOD = <NUM_LIT> <EOL> @ csrf_exempt <EOL> def api ( request , method ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" , category = DeprecationWarning ) <EOL> if method == '<STR_LIT>' : <EOL> return render_to_response ( '<STR_LIT>' ) <EOL> if request . method != '<STR_LIT:POST>' : <EOL> return api_return ( STATUS_ERROR_BADMETHOD ) <EOL> request . user = authenticate ( <EOL> username = request . POST . get ( '<STR_LIT:user>' , False ) , <EOL> password = request . POST . get ( '<STR_LIT:password>' ) , <EOL> ) <EOL> if request . user is None : <EOL> return api_return ( STATUS_ERROR_PERMISSIONS ) <EOL> api = API ( request ) <EOL> if hasattr ( api , '<STR_LIT>' % method ) : <EOL> return getattr ( api , '<STR_LIT>' % method ) ( ) <EOL> return api_return ( STATUS_ERROR ) <EOL> def api_return ( status , text = '<STR_LIT>' , json = False ) : <EOL> content_type = '<STR_LIT>' <EOL> if status == STATUS_OK and json : <EOL> content_type = '<STR_LIT>' <EOL> if text is None : <EOL> if status == STATUS_ERROR : <EOL> text = '<STR_LIT>' <EOL> elif status == STATUS_ERROR_NOT_FOUND : <EOL> text = '<STR_LIT>' <EOL> elif status == STATUS_ERROR_PERMISSIONS : <EOL> text = '<STR_LIT>' <EOL> elif status == STATUS_ERROR_BADMETHOD : <EOL> text = '<STR_LIT>' <EOL> elif status == STATUS_OK : <EOL> text = '<STR_LIT:OK>' <EOL> r = HttpResponse ( status = status , content = text , content_type = content_type ) <EOL> if status == STATUS_ERROR_BADMETHOD : <EOL> r . Allow = '<STR_LIT:POST>' <EOL> return r <EOL> class API : <EOL> def __init__ ( self , request ) : <EOL> self . request = request <EOL> def api_public_create_ticket ( self ) : <EOL> form = TicketForm ( self . request . POST ) <EOL> form . fields [ '<STR_LIT>' ] . choices = [ [ q . id , q . title ] for q in Queue . objects . all ( ) ] <EOL> form . fields [ '<STR_LIT>' ] . choices = [ [ u . id , u . get_username ( ) ] for u in User . objects . filter ( is_active = True ) ] <EOL> if form . is_valid ( ) : <EOL> ticket = form . save ( user = self . request . user ) <EOL> return api_return ( STATUS_OK , "<STR_LIT:%s>" % ticket . id ) <EOL> else : <EOL> return api_return ( STATUS_ERROR , text = form . errors . as_text ( ) ) <EOL> def api_public_list_queues ( self ) : <EOL> return api_return ( STATUS_OK , simplejson . dumps ( [ { "<STR_LIT:id>" : "<STR_LIT:%s>" % q . id , "<STR_LIT:title>" : "<STR_LIT:%s>" % q . title } for q in Queue . objects . all ( ) ] ) , json = True ) <EOL> def api_public_find_user ( self ) : <EOL> username = self . request . POST . get ( '<STR_LIT:username>' , False ) <EOL> try : <EOL> u = User . objects . get ( username = username ) <EOL> return api_return ( STATUS_OK , "<STR_LIT:%s>" % u . id ) <EOL> except User . DoesNotExist : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> def api_public_delete_ticket ( self ) : <EOL> if not self . request . POST . get ( '<STR_LIT>' , False ) : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> try : <EOL> ticket = Ticket . objects . get ( id = self . request . POST . get ( '<STR_LIT>' , False ) ) <EOL> except Ticket . DoesNotExist : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> ticket . delete ( ) <EOL> return api_return ( STATUS_OK ) <EOL> def api_public_hold_ticket ( self ) : <EOL> try : <EOL> ticket = Ticket . objects . get ( id = self . request . POST . get ( '<STR_LIT>' , False ) ) <EOL> except Ticket . DoesNotExist : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> ticket . on_hold = True <EOL> ticket . save ( ) <EOL> return api_return ( STATUS_OK ) <EOL> def api_public_unhold_ticket ( self ) : <EOL> try : <EOL> ticket = Ticket . objects . get ( id = self . request . POST . get ( '<STR_LIT>' , False ) ) <EOL> except Ticket . DoesNotExist : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> ticket . on_hold = False <EOL> ticket . save ( ) <EOL> return api_return ( STATUS_OK ) <EOL> def api_public_add_followup ( self ) : <EOL> try : <EOL> ticket = Ticket . objects . get ( id = self . request . POST . get ( '<STR_LIT>' , False ) ) <EOL> except Ticket . DoesNotExist : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> message = self . request . POST . get ( '<STR_LIT:message>' , None ) <EOL> public = self . request . POST . get ( '<STR_LIT>' , '<STR_LIT:n>' ) <EOL> if public not in [ '<STR_LIT:y>' , '<STR_LIT:n>' ] : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> if not message : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> f = FollowUp ( <EOL> ticket = ticket , <EOL> date = timezone . now ( ) , <EOL> comment = message , <EOL> user = self . request . user , <EOL> title = '<STR_LIT>' , <EOL> ) <EOL> if public : <EOL> f . public = True <EOL> f . save ( ) <EOL> context = safe_template_context ( ticket ) <EOL> context [ '<STR_LIT>' ] = f . comment <EOL> messages_sent_to = [ ] <EOL> if public and ticket . submitter_email : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = ticket . submitter_email , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> messages_sent_to . append ( ticket . submitter_email ) <EOL> if public : <EOL> for cc in ticket . ticketcc_set . all ( ) : <EOL> if cc . email_address not in messages_sent_to : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = cc . email_address , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> messages_sent_to . append ( cc . email_address ) <EOL> if ticket . queue . updated_ticket_cc and ticket . queue . updated_ticket_cc not in messages_sent_to : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = ticket . queue . updated_ticket_cc , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> messages_sent_to . append ( ticket . queue . updated_ticket_cc ) <EOL> if ( <EOL> ticket . assigned_to and <EOL> self . request . user != ticket . assigned_to and <EOL> ticket . assigned_to . usersettings . settings . get ( '<STR_LIT>' , False ) and <EOL> ticket . assigned_to . email and <EOL> ticket . assigned_to . email not in messages_sent_to <EOL> ) : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = ticket . assigned_to . email , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> ticket . save ( ) <EOL> return api_return ( STATUS_OK ) <EOL> def api_public_resolve ( self ) : <EOL> try : <EOL> ticket = Ticket . objects . get ( id = self . request . POST . get ( '<STR_LIT>' , False ) ) <EOL> except Ticket . DoesNotExist : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> resolution = self . request . POST . get ( '<STR_LIT>' , None ) <EOL> if not resolution : <EOL> return api_return ( STATUS_ERROR , "<STR_LIT>" ) <EOL> f = FollowUp ( <EOL> ticket = ticket , <EOL> date = timezone . now ( ) , <EOL> comment = resolution , <EOL> user = self . request . user , <EOL> title = '<STR_LIT>' , <EOL> public = True , <EOL> ) <EOL> f . save ( ) <EOL> context = safe_template_context ( ticket ) <EOL> context [ '<STR_LIT>' ] = f . comment <EOL> subject = '<STR_LIT>' % ( ticket . ticket , ticket . title ) <EOL> messages_sent_to = [ ] <EOL> if ticket . submitter_email : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = ticket . submitter_email , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> messages_sent_to . append ( ticket . submitter_email ) <EOL> for cc in ticket . ticketcc_set . all ( ) : <EOL> if cc . email_address not in messages_sent_to : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = cc . email_address , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> messages_sent_to . append ( cc . email_address ) <EOL> if ticket . queue . updated_ticket_cc and ticket . queue . updated_ticket_cc not in messages_sent_to : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = ticket . queue . updated_ticket_cc , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> messages_sent_to . append ( ticket . queue . updated_ticket_cc ) <EOL> if ticket . assigned_to and self . request . user != ticket . assigned_to and getattr ( ticket . assigned_to . usersettings . settings , '<STR_LIT>' , False ) and ticket . assigned_to . email and ticket . assigned_to . email not in messages_sent_to : <EOL> send_templated_mail ( <EOL> '<STR_LIT>' , <EOL> context , <EOL> recipients = ticket . assigned_to . email , <EOL> sender = ticket . queue . from_address , <EOL> fail_silently = True , <EOL> ) <EOL> ticket . resoltuion = f . comment <EOL> ticket . status = Ticket . RESOLVED_STATUS <EOL> ticket . save ( ) <EOL> return api_return ( STATUS_OK ) </s>
