Пример #1
0
def ical_import(request, group_slug=None,
            template_name="calendars/ical_import.html"):
    """docstring for invitation"""
    calendar = get_object_or_404(Calendar, slug=group_slug)
    is_member = calendar.user_is_member(request.user)

    if request.method == "POST":
        if request.user.is_authenticated():
            if is_member:
                ical_url = request.POST.get('ical_url', None)
                ical_file = request.FILES.get('ical_file', None)
                if ical_url or ical_file:
                    if ical_url:
                        feed = urllib2.urlopen(ical_url)
                    if ical_file:
                        feed = ical_file
                    ical = vobject.readOne(feed.read(), ignoreUnreadable=True)
                    for ical_event in ical.components():
                        if getattr(ical_event, 'summary', None) and getattr(ical_event, 'dtstart', None):
                            event = Event(
                                title = ical_event.summary.value,
                                start_time = ical_event.dtstart.value.replace(tzinfo=None),
                                end_time = getattr(ical_event, 'dtend', None) and ical_event.dtend.value.replace(tzinfo=None),
                                body = getattr(ical_event, 'description', None) and ical_event.description.value,
                                location = getattr(ical_event, 'location', None) and ical_event.location.value,
                            )
                            calendar.associate(event, commit=False)
                            event.creator = request.user
                            event.save()
                            if getattr(ical_event, 'rruleset'):
                                today_tz = datetime.datetime.now(tzutc())
                                endday_tz = today_tz + datetime.timedelta(30)
                                dates = ical_event.rruleset.between(today_tz, endday_tz)
                                for date in dates:
                                    if date != ical_event.dtstart.value:
                                        delta = date - ical_event.dtstart.value
                                        if getattr(ical_event, 'dtend', None):
                                            end_date = ical_event.dtend.value + delta
                                        else:
                                            end_date = None
                                        event = Event(
                                            title = ical_event.summary.value,
                                            start_time = date.replace(tzinfo=None),
                                            end_time = end_date.replace(tzinfo=None) if end_date else None,
                                            body = getattr(ical_event, 'description', None) and ical_event.description.value,
                                            location = getattr(ical_event, 'location', None) and ical_event.location.value,
                                        )
                                        calendar.associate(event, commit=False)
                                        event.creator = request.user
                                        event.save()
                    messages.add_message(request, messages.SUCCESS,
                        ugettext("Successfully imported events from ical")
                    )
        else:
            return HttpResponseForbidden()

    return render_to_response(template_name, {
        "calendar": calendar,
        "group": calendar, # @@@ this should be the only context var for the calendar
    }, context_instance=RequestContext(request))
Пример #2
0
    def save(self, *args, **kwargs):

        if (self.transmissionid == None):
            var = str(uuid.uuid4())
            self.transmissionid = var[26:36]

            #Log events
            event = Event()
            event.EventTypeCode = "TRA"
            event.EventSubjectId = self.transmissionid
            event.EventSubjectName = self.SenderName
            event.EventTypeReason = "Transmission added"
            event.source = "Web App"
            event.creator=self.creator
            event.save()


        if (self.bulk_upload_indicator == "Y" and self.backend_SOR_connection != "Disconnected"):
            self.bulk_upload_indicator=""

        self.response='Success'
        super().save(*args, **kwargs)

        #connect to backend
        if self.backend_SOR_connection != "Disconnected":
            #converty model object to json
            serializer = TransmissionSerializer(self)
            json_data = serializer.data
            api = ApiDomains()
            url=api.transmission
            #url='https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidatatransmissionAPI'
            #post data to the API for backend connection
            resp = requests.post(url, json=json_data)
            print("status code " + str(resp.status_code))

            if resp.status_code == 502:
                resp.status_code = 201

            obj = get_object_or_404(APICodes, http_response_code = resp.status_code)
            status_message=obj.http_response_message
            self.response=str(resp.status_code) + " - " + status_message
            if resp.status_code == 201:
                self.commit_indicator="Committed"
            else:
                self.commit_indicator="Not Committed"

            #Log events
            event = Event()
            event.EventTypeCode = "TRC"
            event.EventSubjectId = self.transmissionid
            event.EventSubjectName = self.SenderName
            event.EventTypeReason = "Transmission added to ODS"
            event.source = "Web App"
            event.creator=self.creator
            event.save()

            super().save(*args, **kwargs)
        else:
            print("not connected to backend!")
def addEvents():
    print 'Adding Events ...'
    c = Court.objects.count()
    u = User.objects.count()
    s = Sport.objects.count()


    for i in range(5000):
        e = Event()

        year = 2016 + randint(0, 50)
        month = randint(1, 12)
        day = randint(1,30) if ( month != 2 ) else randint(1, 28)

        hour = randint(1, 23) - 2
        hour = 0 if (hour < 0) else hour
        minute = randint(0, 59)

        e.dateTime = timezone.datetime(year=year, month=month, day=day,
                                       hour=hour, minute=minute)
        e.endTime = timezone.datetime(year=year, month=month, day=day,
                                       hour=hour+2, minute=minute)

        e.court = Court.objects.all()[randint(0,c-1)]
        e.sport = Sport.objects.all()[randint(0,s-1)]
        e.creator  = User.objects.all()[randint(0,u-1)]
        e.duration = 2
        e.save()
        try:
            for i in range(randint(1,50)):
                e.participants.add(User.objects.get(id=randint(2,u)))
        except(User.DoesNotExist):
            pass
        e.save()
Пример #4
0
def ListEventsHistory(request, pk):

    context = {}

    prod_obj = get_object_or_404(Event, pk=pk)

    api = ApiDomains()
    url = api.event + "/" + "history"
    #url = 'https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidataeventAPI/history'
    payload = {'ident': prod_obj.eventid}

    resp = requests.get(url, params=payload)
    print(resp.status_code)
    obj = get_object_or_404(APICodes, http_response_code=resp.status_code)
    status_message = obj.http_response_message
    mesg = str(resp.status_code) + " - " + status_message

    if resp.status_code != 200:
        # This means something went wrong.
        #raise ApiError('GET /tasks/ {}'.format(resp.status_code))
        #raise APIError(resp.status_code)
        message = {'messages': mesg}
        return render(request, "messages.html", context=message)
    else:
        json_data = []
        dict_data = []
        obj_data = []
        json_data = resp.json()

        #print(json_data[0])
        #print(json_data[1])
        for ix in range(len(json_data)):
            obj = Event()
            #dict_data.append(json.loads(json_data[ix]))
            obj.pk = int(json_data[ix]["LOCAL_ID"])
            obj.eventid = json_data[ix]["EVENT_ID"]
            obj.EventTypeCode = json_data[ix]["EVENT_TYPE_CODE"]
            obj.EventSubjectId = json_data[ix]["EVENT_SUBJECT_ID"]
            obj.EventSubjectName = json_data[ix]["EVENT_SUBJECT_NAME"]
            obj.EventTypeReason = json_data[ix]["EVENT_TYPE_REASON"]

            obj.creator = User.objects.get(pk=int(json_data[ix]["CREATOR"]))
            #obj.crerator = get_object_or_404(User, pk=obj.creatorid)
            obj.EventDate = json_data[ix]["EVENT_DATE"]
            obj.TransactionDate = json_data[ix]["TRANSACTION_DATE"]

            obj.backend_SOR_connection = json_data[ix]["CONNECTION"]
            obj.response = json_data[ix]["RESPONSE"]
            obj.commit_indicator = json_data[ix]["COMMIT_INDICATOR"]
            obj.record_status = json_data[ix]["RECORD_STATUS"]

            obj_data.append(obj)

        context = {'object_list': obj_data}

        return render(request, "events/event_list.html", context=context)
Пример #5
0
    def create(self, validated_data):
        instance = Event()  # .objects.create(**validated_data)
        for field in validated_data:
            if field != 'creator':
                instance.__setattr__(field, validated_data[field])

        user = self.context['request'].user

        instance.creator = user
        instance.save()
        return instance
Пример #6
0
def TransmissionList(request):

    if request.method == 'GET':
        contacts = Transmission.objects.all()
        serializer = TransmissionSerializer(contacts, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = TransmissionSerializer(data=request.data)
        # Raises a ValidatinException which will be sent as a 400 response.
        serializer.is_valid(raise_exception=True)
        transmission = Transmission()
        event = Event()

        if serializer.data["transmissionid"] == '':
            transmission.transmissionid = str(uuid.uuid4())[26:36]
            event.EventTypeReason = "New transmission received via API"
        else:
            transmission.transmissionid = serializer.data["transmissionid"]
            event.EventTypeReason = "Transmission added via API"
        #transmission.transmissionid = serializer.data["transmissionid"]
        transmission.SenderName = serializer.data["SenderName"]
        transmission.BenefitAdministratorPlatform = serializer.data[
            "BenefitAdministratorPlatform"]
        transmission.ReceiverName = serializer.data["ReceiverName"]
        transmission.TestProductionCode = serializer.data["TestProductionCode"]
        transmission.TransmissionTypeCode = serializer.data[
            "TransmissionTypeCode"]
        transmission.SystemVersionIdentifier = serializer.data[
            "SystemVersionIdentifier"]

        transmission.source = "Post API"

        transmission.creator = get_object_or_404(User,
                                                 pk=serializer.data["creator"])
        #transmission.create_date = serializer.data["create_date"]
        transmission.backend_SOR_connection = "Disconnected"
        transmission.response = ""
        transmission.commit_indicator = "Not Committed"
        transmission.record_status = ""
        transmission.bulk_upload_indicator = "Y"
        print(transmission)

        #Log events
        event.EventTypeCode = "TRW"
        event.EventSubjectId = transmission.transmissionid
        event.EventSubjectName = transmission.SenderName
        event.source = "Post API"
        event.creator = transmission.creator
        event.save()

        transmission.save()
        return Response(serializer.data)
Пример #7
0
def RefreshEvent(request, pk):
    # fetch the object related to passed id
    context = {}
    prod_obj = get_object_or_404(Event, pk=pk)

    api = ApiDomains()
    url = api.event + "/" + "refresh"
    #url = 'https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidataeventAPI/history'
    payload = {'ident': prod_obj.eventid}

    resp = requests.get(url, params=payload)
    print(resp.status_code)

    obj = get_object_or_404(APICodes, http_response_code=resp.status_code)
    status_message = obj.http_response_message
    mesg = str(resp.status_code) + " - " + status_message

    if resp.status_code != 200:
        # This means something went wrong.
        #raise ApiError('GET /tasks/ {}'.format(resp.status_code))
        #raise APIError(resp.status_code)
        message = {'messages': mesg}
        return render(request, "messages.html", context=message)
    else:
        json_data = []

        json_data = resp.json()
        obj1 = Event()

        #OVERRIDE THE OBJECT WITH API data
        obj1.pk = int(json_data["LOCAL_ID"])
        obj1.eventid = json_data["EVENT_ID"]
        obj1.EventTypeCode = json_data["EVENT_TYPE_CODE"]
        obj1.EventSubjectId = json_data["EVENT_SUBJECT_ID"]
        obj1.EventSubjectName = json_data["EVENT_SUBJECT_NAME"]
        obj1.EventTypeReason = json_data["EVENT_TYPE_REASON"]

        obj1.creator = User.objects.get(pk=int(json_data["CREATOR"]))
        #obj.crerator = get_object_or_404(User, pk=obj.creatorid)
        obj1.EventDate = json_data["EVENT_DATE"]
        obj1.TransactionDate = json_data["TRANSACTION_DATE"]

        obj1.backend_SOR_connection = json_data["CONNECTION"]
        obj1.response = json_data["RESPONSE"]
        obj1.commit_indicator = json_data["COMMIT_INDICATOR"]
        obj1.record_status = json_data["RECORD_STATUS"]

        obj1.save()

        context = {'event_details': obj1}

        return render(request, "events/event_detail.html", context=context)
Пример #8
0
def ProductList(request):

    if request.method == 'GET':
        contacts = Product.objects.all()
        serializer = ProductSerializer(contacts, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = ProductSerializer(data=request.data)

        serializer.is_valid(raise_exception=True)
        product = Product()
        event = Event()

        if serializer.data["productid"] == '':
            product.productid = str(uuid.uuid4())[26:36]
            event.EventTypeReason = "New product received via API"
        else:
            product.productid = serializer.data["productid"]
            event.EventTypeReason = "Product added via API"

        product.name = serializer.data["name"]
        product.slug = slugify(product.name),

        product.description = serializer.data["description"]
        product.description_html = misaka.html(product.description),
        product.coverage_limit = serializer.data["coverage_limit"]
        product.price_per_1000_units = serializer.data["price_per_1000_units"]

        product.source = "Post API"

        product.creator = get_object_or_404(User,
                                            pk=serializer.data["creator"])

        product.backend_SOR_connection = "Disconnected"
        product.response = ""
        product.commit_indicator = "Not Committed"
        product.record_status = ""
        product.bulk_upload_indicator = "Y"

        #Log events
        event.EventTypeCode = "PRW"
        event.EventSubjectId = product.productid
        event.EventSubjectName = product.name
        event.source = "Post API"
        event.creator = product.creator
        event.save()

        product.save()
        return Response(serializer.data)
Пример #9
0
    def form_valid(self, form):
        print("hello")
        if not self.request.user.has_perm('products.delete_product'):
            raise HttpResponseForbidden()
        else:
            form.instance.creator = self.request.user

            #Log events
            event = Event()
            event.EventTypeCode = "PRD"
            event.EventSubjectId = form.instance.productid
            event.EventSubjectName = form.instance.name
            event.EventTypeReason = "Product deleted"
            event.source = "Online Transaction"
            event.creator = self.request.user
            event.save()

            return super().form_valid(form)
Пример #10
0
    def form_valid(self, form):

        if not self.request.user.has_perm('products.change_product'):
            raise HttpResponseForbidden()
        else:
            form.instance.creator = self.request.user
            form.instance.record_status = "Updated"

            #Log events
            event = Event()
            event.EventTypeCode = "PRU"
            event.EventSubjectId = form.instance.productid
            event.EventSubjectName = form.instance.name
            event.EventTypeReason = "Product updated"
            event.source = "Online Transaction"
            event.creator = self.request.user
            event.save()

            return super().form_valid(form)
Пример #11
0
    def form_valid(self, form):
        print("hello")
        if not self.request.user.has_perm('transmissions.delete_transmission'):
            raise HttpResponseForbidden()
        else:
            form.instance.creator = self.request.user

            #Log events
            event = Event()
            event.EventTypeCode = "TRD"
            event.EventSubjectId = form.instance.transmissionid
            event.EventSubjectName = form.instance.SenderName
            event.EventTypeReason = "Transmission deleted"
            event.source = "Online Transaction"
            event.creator = self.request.user
            event.save()

            #save data
            return super().form_valid(form)
Пример #12
0
def VersionTransmission(request, pk):
    # dictionary for initial data with
    # field names as keys
    context = {}

    # fetch the object related to passed id
    obj = get_object_or_404(Transmission, pk=pk)
    #obj.photo.delete()
    #obj.photo.open(mode='rb')

    # pass the object as instance in form
    form = TransmissionForm(request.POST or None, instance=obj)

    # save the data from the form and
    # redirect to detail_view
    if form.is_valid():
        obj.pk = int(round(time.time() * 1000))
        #form.photo = request.POST.get('photo', False)
        #form.photo = request.FILES['photo']
        form.instance.creator = request.user
        form.instance.record_status = "Created"

        #Log events
        event = Event()
        event.EventTypeCode = "TRV"
        event.EventSubjectId = form.instance.transmissionid
        event.EventSubjectName = form.instance.SenderName
        event.EventTypeReason = "Transmission versioned"
        event.source = "Online Transaction"
        event.creator = request.user
        event.save()

        #save data
        form.save()
        return HttpResponseRedirect(reverse("transmissions:all"))

    else:

        # add form dictionary to context
        context["form"] = form

        return render(request, "transmissions/transmission_form.html", context)
Пример #13
0
    def form_valid(self, form):

        if not self.request.user.has_perm('transmissions.change_transmission'):
            raise HttpResponseForbidden()
        else:
            form.instance.creator = self.request.user
            form.instance.record_status = "Updated"

            #Log events
            event = Event()
            event.EventTypeCode = "TRU"
            event.EventSubjectId = form.instance.transmissionid
            event.EventSubjectName = form.instance.SenderName
            event.EventTypeReason = "Transmission updated"
            event.source = "Online Transaction"
            event.creator = self.request.user
            event.save()

            #save data
            return super().form_valid(form)
Пример #14
0
def create_event(request, course_id):
    if request.method == 'POST':
        variables = _course_context(request, course_id)
        c = Course.objects.get(id=course_id)
        e = Event()
        e.creator = request.user
        form = EventForm(request.POST, request.FILES, instance=e)
        if form.is_valid():
            e = form.save()
            c.add_event(e)
            e.join_event(request.user.id)
            c.save()
            wallPost = ConversationPost(creator=request.user, wall=c.wall, message="", post_type='E', event=e)
            wallPost.save()
            notifyCreateEvent(course=c, event=e)
            return redirect(course_events, course_id)
        else:
            variables['form'] = form
            variables.update(csrf(request))
            return course(request, course_id, "Invalid event creation fields!!")
    return redirect(index)
Пример #15
0
def BulkUploadSOR(request):

    array = Product.objects.filter(bulk_upload_indicator='Y')
    serializer = ProductSerializer(array, many=True)
    json_array = serializer.data

    api = ApiDomains()
    url = api.product + "/" + "upload"
    #url='https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidataProductAPI'
    #post data to the API for backend connection
    resp = requests.post(url, json=json_array)
    print("status code " + str(resp.status_code))

    if resp.status_code == 502:
        resp.status_code = 201

    obj = get_object_or_404(APICodes, http_response_code=resp.status_code)
    status_message = obj.http_response_message
    mesg = str(resp.status_code) + " - " + status_message

    if resp.status_code != 201:
        # This means something went wrong.
        message = {'messages': mesg}
        return render(request, "messages.html", context=message)
    else:
        Product.objects.filter(bulk_upload_indicator='Y').update(
            bulk_upload_indicator=" ")

        #Log events
        event = Event()
        event.EventTypeCode = "PRO"
        event.EventSubjectId = "productodsupload"
        event.EventSubjectName = "Bulk upload to ODS"
        event.EventTypeReason = "Products uploaded to ODS in bulk"
        event.source = "Online Transaction"
        event.creator = request.user
        event.save()

        return HttpResponseRedirect(reverse("products:all"))
Пример #16
0
 def add(self, ical_event, start_time=None, end_time=None, main=True):
     """docstring for import"""
     if not start_time:
         if isinstance(ical_event.dtstart.value, datetime.datetime):
             start_time = ical_event.dtstart.value
         else:
             start_time = datetime.datetime.combine(ical_event.dtstart.value, datetime.time(0,0, tzinfo=local_timezone))
     if not end_time:
         if isinstance(ical_event.dtend.value, datetime.datetime):
             end_time = ical_event.dtend.value
         else:
             end_time = datetime.datetime.combine(ical_event.dtend.value, datetime.time(0,0, tzinfo=local_timezone))
     event = Event(
         title = ical_event.summary.value,
         start_time = start_time.replace(tzinfo=None),
         end_time = end_time.replace(tzinfo=None),
         body = getattr(ical_event, 'description', None) and ical_event.description.value,
         location = getattr(ical_event, 'location', None) and ical_event.location.value,
     )
     self.calendar.associate(event, commit=False)
     event.creator = self.calendar.creator
     event.save()
     if main:
         if getattr(ical_event, 'rruleset'):
             recursive = True
         else:
             recursive = False
     else:
         recursive = True
     item = Item.objects.create(
         uid = ical_event.uid.value,
         event = event,
         link = self,
         main = main,
         recursive = recursive,
     )
     if recursive and main:
         self.add_recursive(ical_event)
Пример #17
0
def RefreshProduct(request, pk):
    # fetch the object related to passed id
    context = {}
    prod_obj = get_object_or_404(Product, pk=pk)

    api = ApiDomains()
    url = api.product + "/" + "refresh"
    #url = 'https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidataProductAPI/history'
    payload = {'ident': prod_obj.productid}

    resp = requests.get(url, params=payload)
    print(resp.status_code)

    obj = get_object_or_404(APICodes, http_response_code=resp.status_code)
    status_message = obj.http_response_message
    mesg = str(resp.status_code) + " - " + status_message

    if resp.status_code != 200:
        # This means something went wrong.
        #raise ApiError('GET /tasks/ {}'.format(resp.status_code))
        #raise APIError(resp.status_code)
        message = {'messages': mesg}
        return render(request, "messages.html", context=message)
    else:
        json_data = []

        json_data = resp.json()
        obj1 = Product()

        #OVERRIDE THE OBJECT WITH API data
        obj1.pk = int(json_data["LOCAL_ID"])
        obj1.productid = json_data["PRODUCT_ID"]
        obj1.name = json_data["NAME"]
        obj1.type = json_data["TYPE"]
        obj1.coverage_limit = json_data["COVERAGE_LIMIT"]
        obj1.price_per_1000_units = json_data["RATE"]
        obj1.product_date = json_data["CREATE_DATE"]
        obj1.description = json_data["DESCRIPTION"]
        obj1.description_html = misaka.html(obj1.description)
        #obj1.photo = json_data["PHOTO"]
        obj1.creator = User.objects.get(pk=int(json_data["CREATOR"]))
        #obj.crerator = get_object_or_404(User, pk=obj.creatorid)
        obj1.create_date = json_data["CREATE_DATE"]
        obj1.backend_SOR_connection = "Disconnected"
        obj1.response = json_data["RESPONSE"]
        obj1.commit_indicator = json_data["COMMIT_INDICATOR"]
        obj1.record_status = json_data["RECORD_STATUS"]

        #Log events
        event = Event()
        event.EventTypeCode = "PRR"
        event.EventSubjectId = obj1.productid
        event.EventSubjectName = obj1.name
        event.EventTypeReason = "Product refreshed from ODS"
        event.source = "Online Transaction"
        event.creator = obj1.creator
        event.save()

        obj1.save()

        context = {'product_details': obj1}

        return render(request, "products/product_detail.html", context=context)
Пример #18
0
    def save(self, *args, **kwargs):

        if (self.employeeid == None):
            var = str(uuid.uuid4())
            self.employeeid = var[26:36]

            #Log events
            event = Event()
            event.EventTypeCode = "EEA"
            event.EventSubjectId = self.employeeid
            event.EventSubjectName = self.name
            event.EventTypeReason = "Employee added"
            event.source = "Web App"
            event.creator=self.creator
            event.save()

        self.slug = slugify(self.name)
        self.response='Success'

        if (self.bulk_upload_indicator == "Y" and self.backend_SOR_connection != "Disconnected"):
            self.bulk_upload_indicator=""

    #    self.description_html = misaka.html(self.description)
        super().save(*args, **kwargs)

        #connect to backend
        if self.backend_SOR_connection != "Disconnected":
            #converty model object to json
            serializer = EmployeeSerializer(self)
            json_data = serializer.data
            api = ApiDomains()
            url=api.employee
            #post data to the API for backend connection
            resp = requests.post(url, json=json_data)
            print("status code " + str(resp.status_code))

            if resp.status_code == 502:
                resp.status_code = 201

            obj = get_object_or_404(APICodes, http_response_code = resp.status_code)
            status_message=obj.http_response_message
            self.response=str(resp.status_code) + " - " + status_message
            if resp.status_code == 201:
                self.commit_indicator="Committed"
            else:
                self.commit_indicator="Not Committed"


            #Log events
            event = Event()
            event.EventTypeCode = "EEC"
            event.EventSubjectId = self.employeeid
            event.EventSubjectName = self.name
            event.EventTypeReason = "Employee added to ODS"
            event.source = "Web App"
            event.creator=self.creator
            event.save()

            super().save(*args, **kwargs)
        else:
            print("not connected to backend!")
Пример #19
0
def csv_import(request, group_slug=None,
            template_name="calendars/csv_import.html"):
    """docstring for invitation"""
    calendar = get_object_or_404(Calendar, slug=group_slug)
    is_member = calendar.user_is_member(request.user)

    if request.method == "POST":
        if request.user.is_authenticated():
            if is_member:
                    import csv
                    import re
                    from django.utils import formats
                    import time
                    fields_mapping = {
                        'title': {'names':['title', 'subject'], 'value':None},
                        'body': {'names':['description', 'body'], 'value':None},
                        'start_date': {'names':['start_date'], 'value':None},
                        'start_time': {'names':['start_time'], 'value':None},
                        'end_date': {'names':['end_date'], 'value':None},
                        'end_time': {'names':['end_time'], 'value':None},
                        'location': {'names':['location'], 'value':None},
                    }
                    csv_file = request.FILES.get('csv_file', None)
                    lines = csv_file.readlines()
                    lines[0] = re.sub('\s+,', ',', lines[0])
                    reader = csv.DictReader(lines)
                    reader.next()
                    for field_name in reader.fieldnames:
                        for key, value in fields_mapping.items():
                            if field_name.replace(' ','_').lower() in value['names']:
                                value['value'] = field_name
                    for row in reader:
                        data = {}
                        for key, value in fields_mapping.items():
                            if value['value']:
                                data[key] = row[value['value']]

                        if 'start_time' in data and 'start_date' in data:
                            data['start_time'] = '%s %s' % (data['start_date'], data['start_time'])
                        for format in formats.get_format('DATETIME_INPUT_FORMATS'):
                            try:
                                data['start_time'] = datetime.datetime(*time.strptime(data['start_time'], format)[:6])
                                break
                            except ValueError:
                                continue

                        if 'end_time' in data and 'end_date' in data:
                            data['end_time'] = '%s %s' % (data['end_date'], data['end_time'])
                        for format in formats.get_format('DATETIME_INPUT_FORMATS'):
                            try:
                                data['end_time'] = datetime.datetime(*time.strptime(data['end_time'], format)[:6])
                                break
                            except ValueError:
                                continue

                        event = Event()
                        for key, value in data.items():
                            setattr(event, key, value)
                        calendar.associate(event, commit=False)
                        event.creator = request.user
                        try:
                            event.save()
                        except Exception:
                            pass
                    messages.add_message(request, messages.SUCCESS,
                        ugettext("Successfully imported events from ical")
                    )
        else:
            return HttpResponseForbidden()

    return render_to_response(template_name, {
        "calendar": calendar,
        "group": calendar, # @@@ this should be the only context var for the calendar
    }, context_instance=RequestContext(request))
Пример #20
0
def BulkUploadProduct(request):

    context = {}

    form = BulkUploadForm(request.POST, request.FILES)

    if form.is_valid():
        form.instance.creator = request.user
        form.save()

        s3 = boto3.client('s3')
        s3.download_file('intellidatastatic1', 'media/products.csv',
                         'products.csv')

        with open('products.csv', 'rt') as csv_file:
            array_good = []
            array_bad = []
            #array_bad =[]
            next(csv_file)  # skip header line
            for row in csv.reader(csv_file):
                bad_ind = 0
                array1 = []
                array2 = []

                #populate serial number
                serial = row[0]
                array2.append(serial)

                #pass product:
                productid = row[1]
                array2.append(productid)
                #validate name
                name = row[2]
                if name == "":
                    bad_ind = 1
                    description = "Name is mandatory"
                    array1.append(serial)
                    array1.append(productid)
                    array1.append(name)
                    array1.append(name)
                    array1.append(description)
                    array_bad.append(array1)

                else:
                    array2.append(name)

                slug = slugify(name)
                #array2.append(slug)

                type = row[3]
                array2.append(type)

                description = row[4]
                array2.append(description)

                description_html = misaka.html(description)

                coverage_limit = row[5]
                array2.append(coverage_limit)

                price_per_1000_units = row[6]
                array2.append(price_per_1000_units)

                if bad_ind == 0:
                    array_good.append(array2)

            # create good file
        #with open('products1.csv', 'w', newline='') as clean_file:
        ##    writer = csv.writer(clean_file)
        #    writer.writerows(array_good)

        buff1 = io.StringIO()

        writer = csv.writer(buff1, dialect='excel', delimiter=',')
        writer.writerows(array_good)

        buff2 = io.BytesIO(buff1.getvalue().encode())

        # check if a version of the good file already exists
        #    try:
        #        s3.Object('my-bucket', 'dootdoot.jpg').load()
        #    except botocore.exceptions.ClientError as e:
        #        if e.response['Error']['Code'] == "404":
        #            # The object does not exist.
        #            ...
        #        else:
        #            # Something else has gone wrong.
        #            raise
        #    else:
        #        # do something

        # create good file
        try:
            response = s3.delete_object(Bucket='intellidatastatic1',
                                        Key='media/products1.csv')
            s3.upload_fileobj(buff2, 'intellidatastatic1',
                              'media/products1.csv')
            print("Good File Upload Successful")

        except FileNotFoundError:
            print("The good file was not found")

        except NoCredentialsError:
            print("Credentials not available")

            # create bad file
        #with open('product_error.csv', 'w', newline='') as error_file:
        #       writer = csv.writer(error_file)
        #       writer.writerows(array1)

        buff3 = io.StringIO()

        writer = csv.writer(buff3, dialect='excel', delimiter=',')
        writer.writerows(array_bad)

        buff4 = io.BytesIO(buff3.getvalue().encode())

        # save bad file to S3
        try:
            response = s3.delete_object(Bucket='intellidatastatic1',
                                        Key='media/products_error.csv')
            s3.upload_fileobj(buff4, 'intellidatastatic1',
                              'media/products_error.csv')
            print("Bad File Upload Successful")

        except FileNotFoundError:
            print("The bad file was not found")

        except NoCredentialsError:
            print("Credentials not available")

        # load the product table
        s3.download_file('intellidatastatic1', 'media/products1.csv',
                         'products1.csv')

        with open('products1.csv', 'rt') as csv_file:
            bulk_mgr = BulkCreateManager(chunk_size=20)

            for row in csv.reader(csv_file):
                if row[1] == "":
                    bulk_mgr.add(
                        models.Product(productid=str(uuid.uuid4())[26:36],
                                       name=row[2],
                                       slug=slugify(row[2]),
                                       type=row[3],
                                       description=row[4],
                                       description_html=misaka.html(row[4]),
                                       coverage_limit=row[5],
                                       price_per_1000_units=row[6],
                                       creator=request.user,
                                       source="Standard Feed Bulk Upload",
                                       record_status="Created",
                                       bulk_upload_indicator="Y"))
                else:
                    bulk_mgr.add(
                        models.Product(productid=row[1],
                                       name=row[2],
                                       slug=slugify(row[2]),
                                       type=row[3],
                                       description=row[4],
                                       description_html=misaka.html(row[4]),
                                       coverage_limit=row[5],
                                       price_per_1000_units=row[6],
                                       creator=request.user,
                                       source="Standard Feed Bulk Upload",
                                       record_status="Created",
                                       bulk_upload_indicator="Y"))

            bulk_mgr.done()

            # load the product error table
            s3.download_file('intellidatastatic1', 'media/products_error.csv',
                             'products_error.csv')

            #Refresh Error table for concerned employer
            ProductError.objects.all().delete()

            with open('products_error.csv', 'rt') as csv_file:
                bulk_mgr = BulkCreateManager(chunk_size=20)
                for row1 in csv.reader(csv_file):
                    bulk_mgr.add(
                        models.ProductError(
                            serial=row1[0],
                            productid=row1[1],
                            name=row1[2],
                            errorfield=row1[3],
                            error_description=row1[4],
                            creator=request.user,
                            source="Standard Feed Bulk Upload"))
                bulk_mgr.done()

        error_report = ProductErrorAggregate()

        error_report.clean = Product.objects.count()
        error_report.error = ProductError.objects.count()

        error_report.total = (error_report.clean + error_report.error)

        #Refresh Error aggregate table for concerned employer
        #ProductErrorAggregate.objects.all().delete()

        error_report.save()

        #Log events
        event = Event()
        event.EventTypeCode = "PRB"
        event.EventSubjectId = "bulkproducts"
        event.EventSubjectName = "Bulk processing"
        event.EventTypeReason = "Products uploaded in bulk"
        event.source = "Standard Feed Bulk Upload"
        event.creator = request.user
        event.save()

        return HttpResponseRedirect(reverse("products:all"))

        #return HttpResponseRedirect(reverse("products:all"))

    else:
        # add form dictionary to context
        context["form"] = form

        return render(request, "bulkuploads/bulkupload_form.html", context)
Пример #21
0
    def get_queryset(self, **kwargs):  # new
        query = self.request.GET.get('q', None)
        object_list = Event.objects.filter(
            Q(eventid__icontains=query) | Q(EventTypeCode__icontains=query)
            | Q(EventTypeReason__icontains=query))

        #change start for remote SearcheventsForm
        if not object_list:
            api = ApiDomains()
            url = api.event + "/" + "refresh"
            #url = 'https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidataeventAPI/history'
            payload = {'ident': query}

            resp = requests.get(url, params=payload)
            print(resp.status_code)

            obj = get_object_or_404(APICodes,
                                    http_response_code=resp.status_code)
            status_message = obj.http_response_message
            mesg = str(resp.status_code) + " - " + status_message

            if resp.status_code != 200:
                # This means something went wrong.
                #raise ApiError('GET /tasks/ {}'.format(resp.status_code))
                #raise APIError(resp.status_code)
                #message={'messages':mesg}
                #return render(self.request, "messages.html", context=message)
                print("Status Code: " + str(resp.status_code))
            else:
                json_data = []

                json_data = resp.json()
                obj_data = []
                obj1 = Event()

                #OVERRIDE THE OBJECT WITH API data
                obj1.pk = int(json_data["LOCAL_ID"])
                obj1.eventid = json_data["EVENT_ID"]
                obj1.EventTypeCode = json_data["EVENT_TYPE_CODE"]
                obj1.EventSubjectId = json_data["EVENT_SUBJECT_ID"]
                obj1.EventSubjectName = json_data["EVENT_SUBJECT_NAME"]
                obj1.EventTypeReason = json_data["EVENT_TYPE_REASON"]

                obj1.creator = User.objects.get(pk=int(json_data["CREATOR"]))
                #obj.crerator = get_object_or_404(User, pk=obj.creatorid)
                obj1.EventDate = json_data["EVENT_DATE"]
                obj1.TransactionDate = json_data["TRANSACTION_DATE"]

                obj1.backend_SOR_connection = json_data["CONNECTION"]
                obj1.response = json_data["RESPONSE"]
                obj1.commit_indicator = json_data["COMMIT_INDICATOR"]
                obj1.record_status = json_data["RECORD_STATUS"]

                obj1.save()

                #obj_data.append(obj1)
                #print(obj_data)

                #context = {'object_list':obj_data}

                #return render(self.request, "eventevents/event_search_list.html", context=context)
                object_remote_list = Event.objects.filter(eventid=query)
                print(object_remote_list)
                return object_remote_list

        else:
            #change end for remote SearcheventsForm

            return object_list
Пример #22
0
    def handle(self, *args, **options):

        self.create_categories()
        self.create_locations()

        old_calendars = UNLCalendar.objects.all()
        for old_calendar in old_calendars:

            # Check if the old calendar creator exists in our DB
            calendar_creator = self.get_create_user(str(old_calendar.uidcreated))
            if calendar_creator is not None:
                new_calendar = Calendar(title=old_calendar.name, owner=calendar_creator)
                new_calendar.pk = old_calendar.id
                try:
                    new_calendar.save()
                except Exception, e:
                    logging.error('Unable to save calendar `%s`: %s' % (old_calendar.name, str(e)))
                else:

                    # Editors
                    # Assume if they had any permissions at all, they are an editor
                    # (unless they are the calendar owner or a superuser)
                    for uid in UNLUserHasPermission.objects.filter(calendar_id=old_calendar.id).values_list('user_uid').distinct():
                        uid = uid[0]
                        editor = self.get_create_user(str(uid))
                        if editor is not None and editor != calendar_creator and not editor.is_superuser:
                            new_calendar.editors.add(editor)

                    # Events
                    for old_calendar_event in UNLCalendarHasEvent.objects.filter(calendar_id = old_calendar.id):
                        try:
                            old_event = UNLEvent.objects.get(id=old_calendar_event.event_id)
                        except UNLEvent.DoesNotExist:
                            logging.error('Has event missing %d' % old_calendar_event.event_id)

                        old_title = old_event.title
                        if len(old_title) > 255:
                            old_title = old_title[0:254]
                        elif old_event.subtitle is not None and len(old_event.title + ' - ' + old_event.subtitle) < 255:
                            old_title += ' - ' + old_event.subtitle

                        old_contact_name = old_event.listingcontactname
                        if not old_contact_name:
                            old_contact_name = calendar_creator.first_name
                        old_contact_name = remove_html(old_contact_name) # Clean before checking length

                        # check to see if the contact name is too long
                        if len(old_contact_name) > 64:
                            old_contact_name = old_contact_name[0:63]

                        old_contact_email = remove_html(old_event.listingcontactemail)
                        old_contact_phone = remove_html(old_event.listingcontactphone)

                        if not old_event.description:
                            old_event.description = settings.FALLBACK_EVENT_DESCRIPTION

                        new_event = Event(title=old_title,
                                          description=old_event.description,
                                          calendar=new_calendar,
                                          contact_name=old_contact_name,
                                          contact_email=old_contact_email,
                                          contact_phone=old_contact_phone)

                        # Statuses: pending, posted, archived
                        state = None
                        if old_calendar_event.status == 'pending':
                            new_event.state = State.pending
                        elif old_calendar_event.status == 'posted' or old_calendar_event.status == 'archived':
                            new_event.state = State.posted
                        else:
                            logging.error('Unknown old event status `%s`' % old_calendar_event.status)
                            continue

                        event_creator = self.get_create_user(str(old_event.uidcreated))
                        if event_creator is not None:
                            new_event.creator = event_creator

                            # Event Type -> Category
                            category = self.get_event_category(old_event)
                            if category is not None:
                                new_event.category = category

                            try:
                                new_event.save()
                            except Exception, e:
                                logging.error('Unable to save new event `%s`: %s' % (new_event.title,str(e)))
                                continue
                            else:
                                # Instances
                                for old_instance in UNLEventdatetime.objects.filter(event_id=old_event.id):
                                    new_instance = EventInstance(event=new_event,
                                                                 unl_eventdatetime_id=old_instance.id)

                                    old_start_time = old_instance.starttime
                                    old_end_time = old_instance.endtime

                                    # Validate start/end datetimes (UNL event times can be invalid and/or empty)
                                    if not old_end_time:
                                        new_instance.start = old_start_time
                                        new_instance.end = old_start_time + timedelta(hours=1)
                                        logging.error('Invalid event instance datetimes: No end datetime available. Setting new end datetime to 1 hour after the given start datetime value `%s`. (Event: `%s`)' % (old_start_time, old_title))
                                    elif old_start_time > old_end_time:
                                        new_instance.start = old_start_time
                                        new_instance.end = old_start_time + timedelta(hours=1)
                                        logging.error('Invalid event instance datetimes: End datetime `%s` occurs before start datetime `%s`. Setting new end datetime value. (Event: `%s`)' % (old_end_time, old_start_time, old_title))
                                    else:
                                        new_instance.start = old_start_time
                                        new_instance.end = old_end_time

                                    # Location
                                    old_location_id = old_instance.location_id
                                    if not old_location_id:
                                        old_location_id = 1

                                    try:
                                        old_location = UNLLocation.objects.get(id=old_location_id)
                                    except UNLLocation.DoesNotExist:
                                        logging.info('UNL event instance location not in UNL Location table: %d' % old_location_id)
                                    else:
                                        if old_location.name:
                                            # check to see if the location name is too long
                                            old_location_name = old_location.name
                                            if len(old_location_name) > 256:
                                                old_location_name = old_location_name[0:256]

                                            try:
                                                new_instance.location = Location.objects.get(title__iexact=old_location_name)
                                            except Location.DoesNotExist:
                                                logging.error('No Location for UNL Location %s' % old_location_name)

                                    try:
                                        new_instance.save()
                                    except Exception, e:
                                        logging.error('Unable to save event instance for event `%s`: %s' % (new_event.title,str(e)))
Пример #23
0
def RefreshTransmission(request, pk):
    # fetch the object related to passed id
    context = {}
    prod_obj = get_object_or_404(Transmission, pk=pk)

    api = ApiDomains()
    url = api.transmission + "/" + "refresh"
    #url = 'https://94q78vev60.execute-api.us-east-1.amazonaws.com/Prod/intellidatatransmissionAPI/history'
    payload = {'ident': prod_obj.transmissionid}

    resp = requests.get(url, params=payload)
    print(resp.status_code)

    obj = get_object_or_404(APICodes, http_response_code=resp.status_code)
    status_message = obj.http_response_message
    mesg = str(resp.status_code) + " - " + status_message

    if resp.status_code != 200:
        # This means something went wrong.
        #raise ApiError('GET /tasks/ {}'.format(resp.status_code))
        #raise APIError(resp.status_code)
        message = {'messages': mesg}
        return render(request, "messages.html", context=message)
    else:
        json_data = []

        json_data = resp.json()
        obj1 = Transmission()

        #OVERRIDE THE OBJECT WITH API data
        obj1.pk = int(json_data["LOCAL_ID"])
        obj1.transmissionid = json_data["TRANSMISSION_ID"]
        obj1.SenderName = json_data["SENDER_NAME"]
        obj1.BenefitAdministratorPlatform = json_data[
            "BENEFIT_ADMINISTRATOR_PLATFORM"]
        obj1.ReceiverName = json_data["RECEIVER_NAME"]

        obj1.TestProductionCode = json_data["TEST_PRODUCTION_CODE"]
        obj1.TransmissionTypeCode = json_data["TRANSMISSION_TYPE_CODE"]
        obj1.SystemVersionIdentifier = json_data["SYSTEM_VERSION_IDENTIFIER"]
        obj1.planadmin_email = json_data["PLANADMIN_EMAIL"]

        obj1.creator = User.objects.get(pk=int(json_data["CREATOR"]))
        #obj.crerator = get_object_or_404(User, pk=obj.creatorid)
        obj1.create_date = json_data["CREATE_DATE"]
        obj1.backend_SOR_connection = "Disconnected"
        obj1.response = json_data["RESPONSE"]
        obj1.commit_indicator = json_data["COMMIT_INDICATOR"]
        obj1.record_status = json_data["RECORD_STATUS"]

        #Log events
        event = Event()
        event.EventTypeCode = "TRR"
        event.EventSubjectId = obj1.transmissionid
        event.EventSubjectName = obj1.SenderName
        event.EventTypeReason = "Transmission refreshed from ODS"
        event.source = "Online Transaction"
        event.creator = obj1.creator
        event.save()

        #save data

        obj1.save()

        context = {'transmission_details': obj1}

        return render(request,
                      "transmissions/transmission_detail.html",
                      context=context)