def removeSelectedItems(request):
    """ Removes the selecteds ItemHarvesters from the user's harvesting list
    """
    user = request.user
    selection = getUserSelection(request)
    queryset = selection.getSavedQueryset(None, request.GET['tableId'])
    count = queryset.count()
    try:
        for item in queryset:
            user.userProfile.remove_item_from_harvest_list(item)
    except Exception:
        logError(
            'An error occured while removing selection from harvesting list')
        return jResponse({
            'error': {
                'code': 500,
                'message': 'Une erreur est survenue. Veuillez réessayer.'
            }
        })
    resetUserSelection(request)
    return jsonMessages([
        "{} élément{} {} été retiré{} de votre liste de "
        "collecte.".format(count, 's' if count > 1 else '',
                           'ont' if count > 1 else 'a',
                           's' if count > 1 else '')
    ])
def ajaxBase(request):
    try:
        if not request.user.is_authenticated: return jsonUnauthorizedError()
        if 'tableId' not in request.GET: return missingParam('tableId')
        if 'modelName' not in request.GET: return missingParam('modelName')
        if 'srcs' not in request.GET: return missingParam('srcs')
        tableId = request.GET['tableId']
        modelName = request.GET['modelName']
        if modelName not in MODEL_WHITELIST: return jsonForbiddenError()
        queryset = getQueryset(request)
        userSelection = getUserSelection(request)
        selecteds = userSelection.getSavedQueryset(modelName,
                                                   tableId).distinct()
        if 'download' in request.GET and request.GET['download'] == 'true':
            if request.GET['fileType'] == 'csv':
                return generateCSVDownload(request, selecteds, userSelection)
            elif request.GET['fileType'] == 'json':
                return generateJSONDownload(request, selecteds, userSelection)
        else:
            response = generateAjaxTableResponse(queryset, request, selecteds)
            return HttpResponse(json.dumps(response),
                                content_type='application/json')
    except EmojiiSearchException:
        return jResponse({
            "status": "exception",
            "error": {
                "code": 400,
                "reason": "La recherche par emojii n'est pas supportée."
            }
        })
    except Exception as e:
        logError("Exception occured in tool/views/ajaxTables:ajaxBase")
        return jsonBadRequest("Bad arguments")
def addFbPages(request, pageUrls):
    aspiraProfile = request.user.userProfile
    if not hasattr(aspiraProfile, "fbAccessToken"): raise FacebookAccessTokenException()
    if aspiraProfile.fbAccessToken.is_expired(): raise FacebookAccessTokenException()
    graph = facebook.GraphAPI(aspiraProfile.fbAccessToken._token)
    invalids = []
    response = None
    try:
        response = graph.get_objects(ids=pageUrls)
        pretty(response)
    except Exception as e:
        logError("An error occured")
        return pageUrls
    for url in response.keys():
        if 'name' in response[url] and 'id' in response[url]:
            jUser = graph.get_object(response[url]['id'], fields='name,id')
            fbPage, new = FBPage.objects.get_or_create(_ident=response[url]['id'])
            if new:
                fbProfile, new = FBProfile.objects.get_or_create(_ident=fbPage._ident)
                if new:
                    fbProfile.type = 'P'
                    fbProfile.fbPage = fbPage
                    fbProfile.save()
            fbPage.name = response[url]['name']
            fbPage.save()
            aspiraProfile.facebookPagesToHarvest.add(fbPage)
            aspiraProfile.save()
        else:
            invalids.append(response[url]['id'])
    return invalids
def generateCSVDownload(request, queryset, userSelection):
    tableId = request.GET['tableId']
    userSelection.setQueryOption(tableId, 'downloadProgress', 0)
    userSelection.setQueryOption(tableId, 'linesTransfered', 0)

    def dataStream():
        sent = 0
        lastPercent = 0
        count = queryset.count()
        csvfile = io.StringIO()
        csvfile.write('\uFEFF')
        csvwriter = csv.writer(csvfile)
        fields = request.GET['fields'].split(',')
        temp = queryset.first()
        if not temp:
            csvfile.seek(0)
            yield 'Aucune données selectionnées'
        csvwriter.writerow(getColumnsDescriptions(temp, fields, 'name'))
        csvwriter.writerow(getColumnsDescriptions(temp, fields, 'description'))
        if count <= 0:
            csvfile.seek(0)
            yield csvfile.read()
        itterCount = 0
        for obj in queryset.iterator():
            percent = (int)(sent * 100 / count)
            itterCount += 1
            if percent > lastPercent or itterCount > 100:
                itterCount = 0
                lastPercent = percent
                userSelection.setQueryOption(tableId, 'downloadProgress',
                                             percent)
                userSelection.setQueryOption(tableId, 'linesTransfered', sent)
                # log("sent: %s lines. (%i %%)"%(sent, percent))
            csvwriter.writerow(getValuesAsList(obj, fields))
            csvfile.seek(0)
            data = csvfile.read()
            csvfile.seek(0)
            csvfile.truncate()
            sent += 1
            try:
                yield data
            except:
                logError("Error occured in generateCSVDownload")
        # log('completed download')
        userSelection.setQueryOption(tableId, 'downloadProgress', 100)
        userSelection.setQueryOption(tableId, 'linesTransfered', 1)

    try:
        response = StreamingHttpResponse(dataStream(), content_type="text/csv")
        response[
            "Content-Disposition"] = "attachment; filename=%s" % request.GET[
                'filename'] + '.csv'
        return response
    except:
        logError("Error occured in generateCSVDownload")
def formBase(request, formName):
    if not request.user.is_authenticated: return jsonUnauthorizedError()
    if not formName in validFormNames: return jsonBadRequest('Specified form does not exists')
    try:
        return globals()[formName](request)
    except FacebookAccessTokenException:
        logError('Error while adding FBPages to harvest:')
        return jResponse({
            'status': 'exception',
            'errors': ["Une erreur est survenue avec votre connection Facebook. Visitez votre page de <a class='classic' "
                       "href='/user/settings'>paramètres</a> pour en savoir plus."],
        })
    except:
        logError("ERROR OCCURED IN %s AJAX WITH FORM NAME '%s':" % (__name__, formName))
        return jsonUnknownError()
def form_base(request, form_name):
    try:
        return {
            'create': create_collection,
            'unsubscribe': unsubscribe_from_collection,
            'subscribe': subscribe_to_collections
        }[form_name](request)
    except KeyError:
        return HttpResponseBadRequest('Invalid form name')
    except Exception as e:
        logError('An unknown exception as occured in Collection forms')
        add_error_message(
            request,
            'Une erreur inconnue est survenue{}. Veuillez réessayer.'.format(
                ' ({})'.format(e) if DEBUG else ''))
        return redirect(request.META.get('HTTP_REFERER'))
def get_youtube_api(user_profile):
    try:
        api = build("youtube",
                    "v3",
                    developerKey=user_profile.youtubeApp_dev_key)
        api.i18nLanguages().list(part='snippet').execute()  # testing
        return api
    except:
        user_profile.youtubeApp_parameters_error = True
        user_profile.save()
        logError('Error in Youtube forms: get_youtube_api')
        raise AddYoutubeItemHarvesterException(
            "Un problème est survenu avec votre application Youtube! Veuillez "
            "visiter votre page de <a href='/user/settings' "
            "class='TableToolLink'>paramètres</a> et assurez-vous que les "
            "informations inscrites dans la section \"Youtube\" sont correctes."
        )
def fetch_new_youtube_playlist(api, playlist_ident):
    try:
        response = api.playlists().list(id=playlist_ident,
                                        part='snippet,id,status').execute()
    except:
        logError("Unknown error while fetching new youtube playlist")
        raise AddYoutubeItemHarvesterException(
            'Une erreur est survenue en tentant de trouver '
            'l\'URL spécifiée. Veuillez réessayer.')

    if 'items' not in response or not response['items']:
        raise AddYoutubeItemHarvesterException(
            'L\'url contenant {} ne semble pas correspondre à une playlist '
            'Youtube. Veuillez re-vérifier celle-ci.'.format(playlist_ident))
    item = response['items'][0]
    new_playlist = YTPlaylist.objects.create(_ident=item['id'])
    new_playlist.update(item)
    return new_playlist
Exemple #9
0
def ajax_base(request, endpoint_name):
    try:
        return {
            'search': search,
            'addItems': addItems,
            'remove_items': removeCollectionItem,
        }[endpoint_name](request)
    except KeyError:
        logError("KeyError occured in collection ajax endpoint")
        return jsonNotFound()
    except InvalidFieldException:
        return jsonBadRequest('Invalid field received.')
    except Exception as e:
        logError('An unknown exception as occured in Collection forms')
        return jResponse({
            'status':
            'error',
            'errors':
            ['Une erreur inconnue est survenue. Veuillez réessayer.']
        })
def ajax_lineChart(request):
    reqId = None
    if 'tqx' in request.GET:
        reqId = request.GET['tqx'].split(':')[1]
    try:
        response = {
            "status": 'ok',
            'reqId': reqId,
            "table": generateLineChartTable(request),
        }
    except Exception as e:
        logError('An error occured while creating a Linechart')
        response = {
            'status': 'error',
            'message': e.args[0],
            'reqId': reqId,
        }
    return HttpResponse("google.visualization.Query.setResponse(%s)" %
                        json.dumps(response),
                        content_type='application/json')
 def dataStream():
     sent = 0
     lastPercent = 0
     count = queryset.count()
     csvfile = io.StringIO()
     csvfile.write('\uFEFF')
     csvwriter = csv.writer(csvfile)
     fields = request.GET['fields'].split(',')
     temp = queryset.first()
     if not temp:
         csvfile.seek(0)
         yield 'Aucune données selectionnées'
     csvwriter.writerow(getColumnsDescriptions(temp, fields, 'name'))
     csvwriter.writerow(getColumnsDescriptions(temp, fields, 'description'))
     if count <= 0:
         csvfile.seek(0)
         yield csvfile.read()
     itterCount = 0
     for obj in queryset.iterator():
         percent = (int)(sent * 100 / count)
         itterCount += 1
         if percent > lastPercent or itterCount > 100:
             itterCount = 0
             lastPercent = percent
             userSelection.setQueryOption(tableId, 'downloadProgress',
                                          percent)
             userSelection.setQueryOption(tableId, 'linesTransfered', sent)
             # log("sent: %s lines. (%i %%)"%(sent, percent))
         csvwriter.writerow(getValuesAsList(obj, fields))
         csvfile.seek(0)
         data = csvfile.read()
         csvfile.seek(0)
         csvfile.truncate()
         sent += 1
         try:
             yield data
         except:
             logError("Error occured in generateCSVDownload")
     # log('completed download')
     userSelection.setQueryOption(tableId, 'downloadProgress', 100)
     userSelection.setQueryOption(tableId, 'linesTransfered', 1)
def fetch_new_youtube_channel(api, channel_username):
    try:
        response = api.channels().list(
            id=channel_username,
            part='brandingSettings,contentOwnerDetails,id,'
            'invideoPromotion,localizations,snippet,statistics,status'
        ).execute()
    except:
        logError("Unknown error while fetching new youtube channel")
        raise AddYoutubeItemHarvesterException(
            'Une erreur est survenue en tentant de trouver '
            'l\'URL spécifiée. Veuillez réessayer.')

    if 'items' not in response or not response['items']:
        raise AddYoutubeItemHarvesterException(
            'L\'url contenant {} ne semble pas correspondre à une chaine '
            'Youtube. Veuillez re-vérifier.'.format(channel_username))
    item = response['items'][0]
    new_channel = YTChannel.objects.create(_ident=item['id'])
    new_channel.update(item)
    return new_channel
 def extend(self):
     try:
         graph = facebook.GraphAPI(self._token)
         response = graph.extend_access_token(
             FACEBOOK_APP_PARAMS['app_id'],
             FACEBOOK_APP_PARAMS['app_secret'])
         pretty(response)
         if not 'access_token' in response:
             raise Exception("failed to extend access token: %s" % self)
         self._token = response['access_token']
         if 'expires_in' in response:
             self.expires = time.time() + int(response['expires_in'])
         else:
             self.expires = time.time() + 60 * 5
         self.save()
         log("%s expires in %s seconds" %
             (self, self.expires - time.time()))
     except Exception as e:
         logError("An error occured while extending the token!")
         self.userProfile.facebookApp_parameters_error = True
         self.userProfile.save()
         raise
def userRegister(request):
    data = request.POST
    errors = []
    masterAddrs = [
        user.email
        for user in User.objects.filter(is_staff=True, email__isnull=False)
        if user.email != ''
    ]
    required_fields = {
        'username': '******',
        'email': 'Email address',
        'pw': 'Password'
    }
    context = {'user': request.user, 'navigator': [('Enregistrement', '#')]}

    for field in required_fields.keys():
        if field not in data or data[field] == '':
            errors.append(
                'Le champ "%s" est vide! Veuillez y insérer une valeur.' %
                required_fields[field])

    if not errors and data['pw'] != data['pw_confirm']:
        errors.append('Les mots de passe ne coincident pas!')

    if not errors and len(data['pw']) < 6:
        errors.append('Votre mot de passe doit avoir au moins 6 caractères.')

    if not errors and User.objects.filter(email=data['email']).exists():
        errors.append('Un compte avec cette adresse email existe déjà!')

    if not errors and User.objects.filter(username=data['username']).exists():
        errors.append('Un compte avec ce nom d\'utilisateur existe déjà!')

    if not errors and not validate_userName(data['username']):
        errors.append(
            'Le nom d\'utilisateur ne peut contenir que des caractères alphanumériques.'
        )

    if not errors:
        try:
            validate_email(data['email'])
        except ValidationError:
            errors.append(
                'L\'adresse email fournie ne semble pas valide. Veuillez vérifier qu\'il '
                'ne s\'agit pas d\'une erreur.')

    if not errors:
        message = render_to_string(
            'AspiraUser/emails/newAccountInstructions.html', {
                'username': data['username'],
                'fname': data['fname'],
                'lname': data['lname'],
                'org': data['org'],
                'webmasters': masterAddrs,
            })
        send_mail('Aspira - Instructions de création de compte',
                  'message',
                  'doNotReplyMail', [data['email']],
                  html_message=message)

    if not errors:
        try:
            newUser = User.objects.create_user(data['username'],
                                               data['email'],
                                               data['pw'],
                                               first_name=data['fname'],
                                               last_name=data['lname'],
                                               is_active=False)
            newProfile = UserProfile.objects.create(user=newUser)
        except:
            logError('An error occured while creating a new AspiraUser!')
            errors.append(
                'An error occured! Please contact the webmaster directly to create your account.'
            )

    if not errors:
        message = render_to_string(
            'AspiraUser/emails/validateNewAccount.html', {
                'email':
                data['email'],
                'username':
                data['username'],
                'fname':
                data['fname'],
                'lname':
                data['lname'],
                'org':
                data['org'],
                'usageText':
                data['usageText'],
                'adminAuth':
                "https://%s/adminauth/user/%s/change/" %
                (request.get_host(), newUser.pk)
            })

        try:
            send_mail('SNH - New account creation request',
                      'message',
                      'doNotReplyMail',
                      masterAddrs,
                      html_message=message)
        except:
            logError('An error occured while sending an email to %s' %
                     masterAddrs)

    if errors:
        add_error_messages(request, errors)
        fieldKeeper = {}
        for field in [
                'fname', 'username', 'org', 'email', 'lname', 'usageText'
        ]:
            if field in data:
                fieldKeeper[field] = data[field]
        context['fieldKeeper'] = fieldKeeper
        template = 'AspiraUser/login_page.html'
    else:
        add_info_message(
            request, "Merci! Vous reçevrez un courriel aussitôt que \
        votre compte est approuvé par le webmaster.")
        template = 'AspiraUser/register_successful.html'
    return render(request, template, context)