# Path: spacescout_web/forms/share.py
# class ShareForm(forms.Form):
#         back = forms.CharField(widget=forms.HiddenInput())
#         spot_id = forms.IntegerField(widget=forms.HiddenInput())
#         sender = forms.EmailField(
#             max_length=128,
#             label="From",
#             required=True,
#             error_messages={'required': 'Required field'})
#         recipient = EmailListField(
#             max_length=1028,
#             label="To",
#             required=True,
#             error_messages={'required': 'Required field'})
#         subject = forms.CharField(max_length=128,
#                                   label="Subject",
#                                   required=True)
#         message = forms.CharField(
#             widget=forms.Textarea(attrs={'rows': 5}),
#             label="Your Message (optional)",
#             required=False)
#         email_confirmation = forms.CharField(required=False)
# 
# Path: spacescout_web/spot.py
# class Spot(object):
#     def __init__(self, spot_id):
#         self.spot_id = spot_id
# 
#     def get(self):
#         # Required settings for the client
#         if not hasattr(settings, 'SS_WEB_SERVER_HOST'):
#             raise(Exception("Required setting missing: SS_WEB_SERVER_HOST"))
#         if not hasattr(settings, 'SS_WEB_OAUTH_KEY'):
#             raise(Exception("Required setting missing: SS_WEB_OAUTH_KEY"))
#         if not hasattr(settings, 'SS_WEB_OAUTH_SECRET'):
#             raise(Exception("Required setting missing: SS_WEB_OAUTH_SECRET"))
# 
#         consumer = oauth2.Consumer(
#                                 key=settings.SS_WEB_OAUTH_KEY,
#                                 secret=settings.SS_WEB_OAUTH_SECRET
#                                 )
#         client = oauth2.Client(consumer)
# 
#         url = "{0}/api/v1/spot/{1}".format(
#                                     settings.SS_WEB_SERVER_HOST,
#                                     self.spot_id
#                                     )
# 
#         resp, content = client.request(url, 'GET')
# 
#         if resp.status != 200:
#             raise SpotException(resp.status, self.spot_id)
# 
#         spot = json.loads(content)
# 
#         types = []
#         for t in spot["type"]:
#             types.append(_(t))
# 
#         spot["type"] = ','.join(types)
#         modified_date = (
#                         spot["last_modified"][5:10] +
#                         '-' +
#                         spot["last_modified"][:4]
#                         )
#         spot["last_modified"] = re.sub('-', '/', modified_date)
# 
#         return spot
# 
# class SpotException(Exception):
#     """ Exception for invalid Spot.
#     """
#     def __init__(self, resp_status, spot_id):
#         self.resp_status = resp_status
#         self.spot_id = spot_id
# 
# Path: spacescout_web/views/contact.py
# def validate_back_link(back):
#     if not re.match(r'^(/|%2F)', back):
#         raise(Exception('Invalid Back Link'))


from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, Http404
from spacescout_web.forms.share import ShareForm
from django.conf import settings
from django.utils.http import urlquote
from spacescout_web.spot import Spot, SpotException
from spacescout_web.views.contact import validate_back_link
import oauth2
import socket
import simplejson as json
import logging
""" Copyright 2012, 2013 UW Information Technology, University of Washington

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""


logger = logging.getLogger(__name__)


@login_required(login_url='/login')
def share(request, spot_id=None):
    if request.method == 'POST':
        form = ShareForm(request.POST)

        try:
            back = request.POST['back']

# Path: spacescout_web/forms/share.py
# class ShareForm(forms.Form):
#         back = forms.CharField(widget=forms.HiddenInput())
#         spot_id = forms.IntegerField(widget=forms.HiddenInput())
#         sender = forms.EmailField(
#             max_length=128,
#             label="From",
#             required=True,
#             error_messages={'required': 'Required field'})
#         recipient = EmailListField(
#             max_length=1028,
#             label="To",
#             required=True,
#             error_messages={'required': 'Required field'})
#         subject = forms.CharField(max_length=128,
#                                   label="Subject",
#                                   required=True)
#         message = forms.CharField(
#             widget=forms.Textarea(attrs={'rows': 5}),
#             label="Your Message (optional)",
#             required=False)
#         email_confirmation = forms.CharField(required=False)
# 
# Path: spacescout_web/spot.py
# class Spot(object):
#     def __init__(self, spot_id):
#         self.spot_id = spot_id
# 
#     def get(self):
#         # Required settings for the client
#         if not hasattr(settings, 'SS_WEB_SERVER_HOST'):
#             raise(Exception("Required setting missing: SS_WEB_SERVER_HOST"))
#         if not hasattr(settings, 'SS_WEB_OAUTH_KEY'):
#             raise(Exception("Required setting missing: SS_WEB_OAUTH_KEY"))
#         if not hasattr(settings, 'SS_WEB_OAUTH_SECRET'):
#             raise(Exception("Required setting missing: SS_WEB_OAUTH_SECRET"))
# 
#         consumer = oauth2.Consumer(
#                                 key=settings.SS_WEB_OAUTH_KEY,
#                                 secret=settings.SS_WEB_OAUTH_SECRET
#                                 )
#         client = oauth2.Client(consumer)
# 
#         url = "{0}/api/v1/spot/{1}".format(
#                                     settings.SS_WEB_SERVER_HOST,
#                                     self.spot_id
#                                     )
# 
#         resp, content = client.request(url, 'GET')
# 
#         if resp.status != 200:
#             raise SpotException(resp.status, self.spot_id)
# 
#         spot = json.loads(content)
# 
#         types = []
#         for t in spot["type"]:
#             types.append(_(t))
# 
#         spot["type"] = ','.join(types)
#         modified_date = (
#                         spot["last_modified"][5:10] +
#                         '-' +
#                         spot["last_modified"][:4]
#                         )
#         spot["last_modified"] = re.sub('-', '/', modified_date)
# 
#         return spot
# 
# class SpotException(Exception):
#     """ Exception for invalid Spot.
#     """
#     def __init__(self, resp_status, spot_id):
#         self.resp_status = resp_status
#         self.spot_id = spot_id
# 
# Path: spacescout_web/views/contact.py
# def validate_back_link(back):
#     if not re.match(r'^(/|%2F)', back):
#         raise(Exception('Invalid Back Link'))


# Path: spacescout_web/views/share.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, Http404
from spacescout_web.forms.share import ShareForm
from django.conf import settings
from django.utils.http import urlquote
from spacescout_web.spot import Spot, SpotException
from spacescout_web.views.contact import validate_back_link
import oauth2
import socket
import simplejson as json
import logging

""" Copyright 2012, 2013 UW Information Technology, University of Washington

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""


logger = logging.getLogger(__name__)


@login_required(login_url='/login')
def share(request, spot_id=None):
    if request.method == 'POST':
        form = ShareForm(request.POST)

        try:
            back = request.POST['back']

            validate_back_link(back)