Ejemplo n.º 1
0
    def put(self, request, **kwargs):
        """modifiey a group"""
        def getGroup(groupType):
            if (groupType == "user"):
                return db.UserGroup.objects.get(id=kwargs['title'])
            elif (groupType == "group"):
                return db.Group.objects.get(id=kwargs['title'])
            elif (groupType == "feed"):
                return db.feedGroup.objects.get(title=kwargs['title'])

        groupType = kwargs["type"]
        group = getGroup(groupType)
        if (groupType == "user"):
            if group.user != request.user:
                raise ApiException(
                    "Permission Denied, you do not have permission to edit",
                    401)
        elif (groupType == "group"):
            pass
        elif (groupType == "feed"):
            pass

        json_post = json.loads(request.raw_post_data)
        #if can edit

        form = forms.UserGroupForm(json_post, instance=group.abstractgroup_ptr)
        if form.is_valid():
            group = form.save()
            return utils.json_response({
                'success': True,
            })
        else:
            raise ApiException(utils.form_errors_to_json(form), 401)
Ejemplo n.º 2
0
 def post(self, request):
     """create an event"""
     if not request.user.is_authenticated():
         raise ApiException("User Is not Logged In", 401)
     json_post = json.loads(request.raw_post_data)
     form = forms.EventForm(request.user, json_post)
     if form.is_valid():
         event = form.save()
         return utils.json_response(event.__json__())
     else:
         raise ApiException(utils.form_errors_to_json(form), 401)
Ejemplo n.º 3
0
 def delete(self, request, **kwargs):
     """Deletes An Event"""
     try:
         event = db.Event.objects.get(slug=kwargs['slug'])
         if event.author.user == request.user:
             event.delete()
             return utils.json_response({
                 'success': True,
             })
         else:
             raise ApiException(
                 "You don not have permission to delete this event", 401)
     except ObjectDoesNotExist:
         raise ApiException("Event Not Found", 404)
Ejemplo n.º 4
0
 def put(self, request, **kwargs):
     """Modify an Event via a post based on it id"""
     json_post = json.loads(request.raw_post_data)
     event = self.get_event(kwargs)
     if event.author.user == request.user:
         form = forms.EventForm(request.user, json_post, instance=event)
         if form.is_valid():
             event = form.save()
             return utils.json_response({
                 'success': True,
             })
         else:
             raise ApiException(utils.form_errors_to_json(form), 401)
     else:
         raise ApiException("Permission Denied", 401)
Ejemplo n.º 5
0
 def get_event(self, kwargs):
     try:
         if 'id' in kwargs:
             return db.Event.objects.get(id=kwargs['id'])
         else:
             return db.Event.objects.get(slug=kwargs['slug'])
     except ObjectDoesNotExist:
         raise ApiException("Event Not Found", 404)
Ejemplo n.º 6
0
 def post(self, request):
     """
     Login - create a new session
     """
     json_post = json.loads(request.raw_post_data)
     username = json_post['username']
     password = json_post['password']
     #login
     user = authenticate(username=username, password=password)
     if user is not None:
         if user.is_active:
             #log user in
             auth_login(request, user)
             return self.get(request)
         else:
             # Return a 'disabled account' error message
             raise ApiException("You have been banned. FOADUMF!", 401)
     else:
         # Return an 'invalid login' error message.
         raise ApiException("Invalid Username Or Password", 401)
Ejemplo n.º 7
0
def fetch_feed(feed_url):
    url = urlparse(feed_url)
    if url.netloc == Site.objects.get_current().domain:
        raise ApiException('feed is from current domain', 415)
    #add https of https
    if url.scheme == '':
        try:
            result = fetch_feed('https://' + feed_url)
        except:
            result = fetch_feed('http://' + feed_url)
        return result

    r = requests.get(feed_url)
    if r.status_code == 200:
        head_type = r.headers['content-type'].split(';')[0]
        if head_type != 'text/calendar':
            raise ApiException("invalid content type", 415)
    else:
        raise ApiException("No Response From URL", 415)

    return r.content
Ejemplo n.º 8
0
 def post(self, request, **kwargs):
     """create an a group"""
     json_post = json.loads(request.raw_post_data)
     form = forms.GroupForm(request.user, json_post)
     if form.is_valid():
         group = form.save()
         assign_perm("group_admin", request.user, group.abstractgroup_ptr)
         response = {
             "id": group.id,
             "title": group.title,
             "description": group.description
         }
         return utils.json_response(response)
     else:
         raise ApiException(utils.form_errors_to_json(form), 401)
Ejemplo n.º 9
0
 def post(self, request):
     """Create a new user"""
     json_post = json.loads(request.raw_post_data)
     form = forms.SignUpForm(json_post)
     if form.is_valid():
         #key = 'signup_' + request.META['REMOTE_ADDR']
         #if cache.get(key):
         #    return HttpResponse('please wait before signing up again')
         #cache.set(key, True, settings.OWS_LIMIT_SIGNUP)
         form.save()
         user = authenticate(username=form.cleaned_data.get('username'),
                             password=form.cleaned_data.get('password'))
         auth_login(request, user)
         return utils.json_response({
             'authenticated': True,
             'username': user.username,
             'id': user.id
         })
     else:
         raise ApiException(utils.form_errors_to_json(form), 401)
Ejemplo n.º 10
0
def import_ical(request, source):
    if request.method == 'POST':
        if source == 'url':
            content = importers.fetch_feed(request.POST['feed_url'])
        else:
            content = [rf.read() for rf in request.FILES.values()]
        if not isinstance(content, list):
            content = [content]
        all_events = list()
        for ical in content:
            created_events, old_events = importers.import_ical(
                ical, request.user.usergroup)
            if created_events:
                request.user.usergroup.bfs_propagation(created_events,
                                                       created=True)
            if old_events:
                request.user.usergroup.bfs_propagation(old_events)
            #raise ApiException("invalid file type", 415)
            all_events = created_events + old_events + all_events
        return utils.json_response([event.__json__() for event in all_events])
    else:
        raise ApiException("invalid request type. only POST allowed", 405)
Ejemplo n.º 11
0
 def put(self, request):
     """Mark read Notifications"""
     raise ApiException("NOT Implemented", 401)
Ejemplo n.º 12
0
 def delete(self, request):
     """delete a subscription"""
     raise ApiException("NOT Implemented", 401)
Ejemplo n.º 13
0
 def post(self, request):
     """create a new subscription"""
     raise ApiException("NOT Implemented", 401)
Ejemplo n.º 14
0
 def get(self, request):
     """get a list of subscriptions"""
     raise ApiException("NOT Implemented", 401)
Ejemplo n.º 15
0
 def delete(self, request):
     """delete a group"""
     raise ApiException("NOT Implemented", 401)
Ejemplo n.º 16
0
    def get(self, request):
        """
        Gets a list of events.
        GET[n] specifies the number of events
        GET[start] Where to start getting the events from
        GET[offset] How many events to skip
        GET[author] selects events by the an authors username
        GET[group] gets all the events in a particular group
        GET[me] get all of the events in the usergroup of the current user
        """
        sge = db.SubGroupEvent.objects.select_related('event')

        if request.GET.get('complete') and request.GET.get(
                'complete').lower() == 'false':
            sge = sge.filter(event__complete=False)
        else:
            sge = sge.filter(event__complete=True)

        if request.GET.get('start'):
            #change to use actully date
            try:
                begin = dateutil.parser.parse(
                    request.GET.get('start').replace("Z", ""))
            except (ValueError, OverflowError):
                raise ApiException("Invalid ISO date", 400, "start")
        else:
            begin = datetime.utcnow().replace(tzinfo=tz.utc)

        if request.GET.get('modified'):
            try:
                mod_date = dateutil.parser.parse(
                    request.GET.get('modified').replace("Z", ""))
            except (ValueError, OverflowError):
                raise ApiException("Invalid ISO date", 400, "start")
            sge = sge.filter(event__date__modified__gte=mod_date)

        if request.GET.get('author'):
            #gets events by the author's username
            sge = sge.filter(
                event__author__user__username=request.GET.get('author'))
        #get your personal user group
        elif request.GET.get('me'):
            sge = sge.filter(group_id=request.user)
        #get group
        elif request.GET.get('group'):
            sge = sge.filter(group_id=request.GET.get('group'))
        else:
            #if aggergating accross multiple groups then select distint events
            sge = sge.filter(subscription__isnull=True)

        if request.GET.get('offset'):
            offset = int(request.GET.get('offset'))
        else:
            offset = 0

        if request.GET.get('n'):
            end = int(request.GET.get('n'))
        else:
            end = 20
        #example end=1
        if offset >= 0:
            #3, 4 works
            if end >= 0:
                sge = sge.\
                    filter(event__start_date__gte=begin).\
                    order_by('event__start_date', 'event__start_date_index')[offset: offset + end]
            #3 -2 works
            elif offset + end >= 0:
                sge = sge.\
                    filter(event__start_date__gte=begin).\
                    order_by('event__start_date', 'event__start_date_index')[offset + end: offset]
                sge = list(sge)
                sge.reverse()
            #3 - 5 works
            else:
                before_sge = sge.\
                    filter(event__start_date__lte=begin).\
                    order_by('-event__start_date', '-event__start_date_index')[:abs(offset + end)]
                sge = sge.\
                    filter(event__start_date__gte=begin).\
                    order_by('event__start_date', 'event__start_date_index')[:offset]
                sge = list(sge)
                sge.reverse()
                sge.extend(before_sge)
        #-1,
        else:
            #-1 -5
            if end <= 0:
                sge = sge.\
                    filter(event__start_date__lte=begin).\
                    order_by('-event__start_date', '-event__start_date_index')[abs(offset): abs(offset + end)]
            #-1, 4
            elif end + offset >= 0:
                after_sge = sge.\
                    filter(event__start_date__gte=begin).\
                    order_by('event__start_date', 'event__start_date_index')[:abs(end + offset)]
                sge = sge.\
                    filter(event__start_date__lte=begin).\
                    order_by('-event__start_date', '-event__start_date_index')[:abs(offset)]
                sge = list(sge)
                sge.reverse()
                sge.extend(after_sge)
            #-10 6
            else:
                sge = sge.\
                    filter(event__start_date__lte=begin).\
                    order_by('-event__start_date', '-event__start_date_index')[abs(offset + end): abs(offset)]
                sge = list(sge)
                sge.reverse()
        return utils.json_response([_sge.__json__() for _sge in sge])